Skip to content
This repository was archived by the owner on Jun 7, 2024. It is now read-only.

Commit 6c0999c

Browse files
committed
reorganise code
1 parent 6cebb91 commit 6c0999c

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

src/debug.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ func debug(url string) bool {
5656
}
5757
fmt.Println("Remote Address:", url)
5858
fmt.Print("IP Address: ")
59-
addr, err := net.LookupIP(strings.Replace(strings.Replace(url, "https://", "", -1), "http://", "", -1))
59+
60+
addr, err := net.LookupIP(removeHttpAndHttps(url))
61+
6062
if err != nil {
6163
fmt.Println("Unknown")
6264
} else {

src/get.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ func parseToGetUrl(url string) string {
8484
if !strings.HasPrefix(url, "https://github.com/") {
8585
return url
8686
}
87-
query := strings.Replace(url, "https://github.com/", "", -1)
87+
query := replacePrefix(url, "https://github.com/", "")
88+
8889
querySplit := strings.Split(query, "/")
90+
8991
if len(querySplit) > 3 {
9092
// Source -> fastgitorg/fgit-go/blob/master/fgit.go
9193
// Target -> fastgitorg/fgit-go/master/fgit.go

src/jsdget.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,7 @@ func jsdget(url string, path string) {
3030
}
3131

3232
func parseToJsdUrl(url string) string {
33-
u := strings.Split(url, "//")
34-
switch len(u) {
35-
case 2:
36-
url = u[1]
37-
case 3:
38-
fmt.Print("Cannot parse url")
39-
os.Exit(1)
40-
}
33+
url = removeHttpAndHttps(url)
4134
if !strings.HasPrefix(url, "raw.githubusercontent.com") {
4235
fmt.Print("Url is not supported!")
4336
os.Exit(1)

src/tools.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,27 @@ func replaceNth(s, old, new string, n int) string {
6868
i += len(old)
6969
}
7070
return s
71-
}
71+
}
72+
73+
func removeHttpAndHttps(url string) string {
74+
if strings.HasPrefix(url, "http://") {
75+
return url[7:]
76+
}
77+
if strings.HasPrefix(url, "https://") {
78+
return url[8:]
79+
}
80+
return url
81+
}
82+
83+
func replacePrefix(str, prefix, after string) string {
84+
if len(str) < len(after) {
85+
return str
86+
}
87+
if str == prefix {
88+
return after
89+
}
90+
91+
if strings.HasPrefix(str, prefix) {
92+
return after + str[len(prefix)+1:]
93+
}
94+
}

0 commit comments

Comments
 (0)