This repository was archived by the owner on Jun 7, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +80
-0
lines changed
Expand file tree Collapse file tree 4 files changed +80
-0
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ const (
1212 " fgit get [URL<string>] [Path<string>] [--help|-h]\n " +
1313 " fgit conv [Target<string>] [--help|-h]\n " +
1414 " fgit jsdget [URL<string>] [Path<string>]\n " +
15+ " fgit host [show]\n " +
1516 " If you want to known more about extra-syntax, try to use --help"
1617 jsdHelpMsg = "FastGit JsdGet Command Line Tool\n " +
1718 "=============================\n " +
@@ -63,4 +64,17 @@ const (
6364 " fgit convert\n " +
6465 "EXAMPLE\n " +
6566 " fgit conv gh"
67+ hostHelpMsg = "FastGit Host Command Line Tool\n " +
68+ "==============================\n " +
69+ "REMARKS\n " +
70+ " Get hosts from FastGit Emergency Network Framework Api\n " +
71+ "SYNTAX\n " +
72+ " fgit host [show]\n " +
73+ "EXAMPLE\n " +
74+ " fgit host show"
75+ )
76+
77+ const (
78+ // FastGit UK Emergency Network Framework Host Api
79+ fgEnfHostApi = "https://api.fastgit.org/enf/host"
6680)
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import (
4+ "fmt"
5+ "strings"
6+ )
7+
8+ func getNewHosts () map [string ][]string {
9+ str := getResponseString (fgEnfHostApi )
10+ strs := strings .Split (str , "\n " )
11+ m := make (map [string ][]string )
12+ for _ , s := range strs {
13+ if s == "" {
14+ continue
15+ }
16+ ss := strings .Split (s , ";" )
17+ m [ss [0 ]] = strings .Split (ss [1 ], "," )
18+ }
19+ return m
20+ }
21+
22+ func createHostsContent (m map [string ][]string ) string {
23+ var sb strings.Builder
24+ for ip , prefixes := range m {
25+ for _ , prefix := range prefixes {
26+ sb .WriteString (ip + " " + prefix + "fastgit.org\n " )
27+ }
28+ }
29+ return strings .Trim (sb .String (), "\n " )
30+ }
31+
32+ type HostFunc struct {
33+ }
34+
35+ func (h * HostFunc ) Run (args []string ) {
36+ fmt .Print ("Fetching hosts from FastGit UK Emergency Network Framework..." )
37+ m := getNewHosts ()
38+ fmt .Println ("Success!" )
39+ fmt .Println ("Please modify your host by adding following content." )
40+ content := createHostsContent (m )
41+ fmt .Println (content )
42+ }
Original file line number Diff line number Diff line change @@ -34,6 +34,9 @@ func main() {
3434 case "conv" , "convert" :
3535 runByArgs (& ConvFunc {})
3636
37+ case "host" :
38+ runByArgs (& HostFunc {})
39+
3740 case "-v" , "--version" , "version" :
3841 showVersion ()
3942 }
Original file line number Diff line number Diff line change @@ -27,6 +27,27 @@ func downloadFile(url, path string) {
2727 fmt .Println ("Finished." )
2828}
2929
30+ func getResponseString (url string ) string {
31+ var client http.Client
32+ req , err := http .NewRequest ("GET" , url , nil )
33+ checkErr (err , "Http.Get create failed!" , 1 )
34+ req .Header .Set ("User-Agent" , "fgit/" + version )
35+ checkErr (err , "Http.Get create failed!" , 1 )
36+
37+ resp , err := client .Do (req )
38+ defer resp .Body .Close ()
39+ checkErr (err , "Http request failed!" , 1 )
40+
41+ if resp .StatusCode != http .StatusOK {
42+ fmt .Printf ("Error: response is not OK is %v!\n " , resp .StatusCode )
43+ os .Exit (1 )
44+ }
45+
46+ bodyBytes , err := io .ReadAll (resp .Body )
47+ checkErr (err , "io.ReadAll failed!" , 1 )
48+ return string (bodyBytes )
49+ }
50+
3051func isDir (path string ) bool {
3152 s , err := os .Stat (path )
3253 if err != nil {
You can’t perform that action at this time.
0 commit comments