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

Commit d36aa9e

Browse files
committed
feat: new structure
1 parent 232a656 commit d36aa9e

File tree

12 files changed

+70
-127
lines changed

12 files changed

+70
-127
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ version: 2
55
jobs:
66
build:
77
docker:
8-
- image: circleci/golang:1.16
8+
- image: circleci/golang:1.18
99
working_directory: /go/src/github.com/fastgitorg/fgit-go
1010
environment:
1111
CGO_ENABLED: "0"

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Set up Go
1717
uses: actions/setup-go@v2
1818
with:
19-
go-version: 1.16
19+
go-version: 1.18
2020

2121
- name: Build AMD64
2222
run: |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module fgit-go
22

3-
go 1.16
3+
go 1.19

src/jsdget.go

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/main.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package main
22

33
import (
4+
"fgit-go/oper"
5+
"fgit-go/shared"
46
"fmt"
57
"os"
68
"os/exec"
79
"strings"
810
)
911

12+
var (
13+
timestamp = "unknown"
14+
version = "unknown"
15+
)
16+
1017
func showVersion() {
1118
fmt.Println("Version:", version)
1219
fmt.Println("Build Time:", timestamp)
@@ -15,49 +22,47 @@ func showVersion() {
1522

1623
func main() {
1724
if len(os.Args) == 1 || (len(os.Args) == 2 && (os.Args[1] == "--help" || os.Args[1] == "-h")) {
18-
fmt.Println(mainHelpMsg)
25+
fmt.Println(shared.MainHelpMsg)
1926
os.Exit(0)
2027
}
2128

2229
isConvertToFastGit := false
2330

2431
switch strings.ToLower(os.Args[1]) {
2532
case "debug":
26-
runByArgs(&DebugFunc{})
33+
runByArgs(&oper.DebugFunc{})
2734

2835
case "get", "dl", "download":
29-
runByArgs(&GetFunc{})
30-
31-
case "jdl", "jsdget", "jsd":
32-
runByArgs(&JsdFunc{})
36+
runByArgs(&oper.GetFunc{})
3337

3438
case "conv", "convert":
35-
runByArgs(&ConvFunc{})
39+
runByArgs(&oper.ConvFunc{})
3640

3741
case "-v", "--version", "version":
3842
showVersion()
3943
}
4044

4145
if os.Args[2] == "push" || os.Args[2] == "pull" {
42-
isConvertToFastGit = convToFastGit()
46+
isConvertToFastGit = oper.ConvToFastGit()
4347
}
4448

4549
cmd := exec.Command("git")
4650

4751
// Combine to new command
4852
for i := range os.Args[1:] {
49-
cmd.Args = append(cmd.Args, strings.Replace(os.Args[i], "https://github.com", "https://hub.fastgit.org", -1))
53+
cmd.Args = append(cmd.Args,
54+
strings.Replace(os.Args[i], "https://github.com", shared.GitMirror, -1))
5055
}
5156

5257
cmd.Stderr = os.Stderr
5358
cmd.Stdout = os.Stdout
5459

5560
err := cmd.Start()
56-
checkErr(err, "Command Start Failed!", 4)
61+
shared.CheckErr(err, "Command Start Failed!", 4)
5762

5863
cmd.Wait()
5964
if isConvertToFastGit {
60-
convToGitHub()
65+
oper.ConvToGitHub()
6166
}
6267
}
6368

src/conv.go renamed to src/oper/conv.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
package main
1+
package oper
22

33
import (
4+
"fgit-go/shared"
45
"fmt"
56
"os"
67
"os/exec"
@@ -21,22 +22,22 @@ func (c *ConvFunc) Run(args []string) {
2122
}
2223
os.Exit(0)
2324
}
24-
func convToFastGit() bool {
25-
return convHelper("https://github.com", gitMirror)
25+
func ConvToFastGit() bool {
26+
return convHelper("https://github.com", shared.GitMirror)
2627
}
2728

28-
func convToGitHub() bool {
29-
return convHelper(gitMirror, "https://github.com")
29+
func ConvToGitHub() bool {
30+
return convHelper(shared.GitMirror, "https://github.com")
3031
}
3132

3233
func conv(target string) {
3334
switch target {
3435
case "gh", "github":
35-
convToGitHub()
36+
ConvToGitHub()
3637
case "fg", "fastgit":
37-
convToFastGit()
38+
ConvToFastGit()
3839
case "-h", "--help":
39-
fmt.Println(convHelpMsg)
40+
fmt.Println(shared.ConvHelpMsg)
4041
default:
4142
fmt.Println("Invalid args for conv. Use --help to get more information.")
4243
}
@@ -47,7 +48,7 @@ func convHelper(oldPrefixValue, newPrefixValue string) bool {
4748
buf, err := cmd.Output()
4849
sBuf := string(buf)
4950
originUrl := ""
50-
checkErr(err, "Convert failed.", 8)
51+
shared.CheckErr(err, "Convert failed.", 8)
5152
tmp := strings.Split(
5253
strings.Replace(
5354
strings.Replace(sBuf, "\t", " ", -1),
@@ -66,6 +67,6 @@ func convHelper(oldPrefixValue, newPrefixValue string) bool {
6667
fmt.Println(originUrl)
6768
cmd = exec.Command("git", "remote", "set-url", "origin", strings.Replace(originUrl, oldPrefixValue, newPrefixValue, 1))
6869
_, err = cmd.Output()
69-
checkErr(err, "Convert failed.", 8)
70+
shared.CheckErr(err, "Convert failed.", 8)
7071
return true
7172
}

src/debug.go renamed to src/oper/debug.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
package main
1+
package oper
22

33
import (
4+
"fgit-go/shared"
45
"fmt"
56
"io/ioutil"
67
"net"
@@ -57,7 +58,7 @@ func debug(url string) bool {
5758
fmt.Println("Remote Address:", url)
5859
fmt.Print("IP Address: ")
5960

60-
addr, err := net.LookupIP(removeHttpAndHttps(url))
61+
addr, err := net.LookupIP(shared.RemoveHttpAndHttps(url))
6162

6263
if err != nil {
6364
fmt.Println("Unknown")
@@ -80,7 +81,7 @@ func debug(url string) bool {
8081
}
8182
return debugConnection(url)
8283
} else {
83-
fmt.Println(debugHelpMsg)
84+
fmt.Println(shared.DebugHelpMsg)
8485
return true
8586
}
8687
}

src/get.go renamed to src/oper/get.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
package main
1+
package oper
22

33
import (
4+
"fgit-go/shared"
45
"fmt"
56
"os"
67
"path"
@@ -24,7 +25,7 @@ func (g *GetFunc) Run(args []string) {
2425

2526
func get(url, fpath string) {
2627
if url == "" || url == "--help" || url == "-h" {
27-
fmt.Println(getHelpMsg)
28+
fmt.Println(shared.GetHelpMsg)
2829
} else {
2930
getFile(url, fpath)
3031
}
@@ -34,13 +35,13 @@ func getFile(url, fpath string) {
3435
urlSplit := strings.Split(url, "/")
3536
filename := urlSplit[len(urlSplit)-1]
3637
if fpath == "" {
37-
downloadFile(url, filename)
38+
shared.DownloadFile(url, filename)
3839
}
3940

40-
if isExists(fpath) {
41-
if isDir(fpath) {
41+
if shared.IsExists(fpath) {
42+
if shared.IsDir(fpath) {
4243
fpath = path.Join(fpath, filename)
43-
downloadFile(url, fpath)
44+
shared.DownloadFile(url, fpath)
4445
} else {
4546
isContinue := ' '
4647
fmt.Print(
@@ -68,14 +69,14 @@ func getFile(url, fpath string) {
6869

6970
fmt.Println("Redirect ->", url)
7071

71-
newURL := strings.Replace(url, "https://github.com", downloadMirror, -1)
72+
newURL := strings.Replace(url, "https://github.com", shared.DownloadMirror, -1)
7273
if newURL != url {
7374
fmt.Println("Redirect ->", newURL)
7475
}
7576
fmt.Println("File ->", fpath)
7677
fmt.Println("Downloading...")
7778

78-
downloadFile(newURL, fpath)
79+
shared.DownloadFile(newURL, fpath)
7980

8081
fmt.Println("Finished.")
8182
}
@@ -84,15 +85,15 @@ func parseToGetUrl(url string) string {
8485
if !strings.HasPrefix(url, "https://github.com/") {
8586
return url
8687
}
87-
query := replacePrefix(url, "https://github.com/", "")
88+
query := shared.ReplacePrefix(url, "https://github.com/", "")
8889

8990
querySplit := strings.Split(query, "/")
9091

9192
if len(querySplit) > 3 {
9293
// Source -> fastgitorg/fgit-go/blob/master/fgit.go
9394
// Target -> fastgitorg/fgit-go/master/fgit.go
9495
if querySplit[2] == "blob" {
95-
url = rawMirror
96+
url = shared.RawMirror
9697
for _i, _s := range querySplit {
9798
if _i != 2 {
9899
url += "/" + _s

src/shared.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/const.go renamed to src/shared/const.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package main
1+
package shared
22

33
const (
4-
mainHelpMsg = "FastGit Command Line Tool\n" +
4+
MainHelpMsg = "FastGit Command Line Tool\n" +
55
"=========================\n" +
66
"REMARKS\n" +
77
" We will convert GitHub to FastGit automatically\n" +
@@ -13,20 +13,7 @@ const (
1313
" fgit conv [Target<string>] [--help|-h]\n" +
1414
" fgit jsdget [URL<string>] [Path<string>]\n" +
1515
" If you want to known more about extra-syntax, try to use --help"
16-
jsdHelpMsg = "FastGit JsdGet Command Line Tool\n" +
17-
"=============================\n" +
18-
"REMARKS\n" +
19-
" Download with jsDelivr automatically\n" +
20-
"SYNTAX\n" +
21-
" fgit jsdget [--help|-h]\n" +
22-
" fgit jsdget [URL<string>]\n" +
23-
" fgit jsdget [URL<string>] [Path<string>]\n" +
24-
"ALIASES\n" +
25-
" fgit jdl\n" +
26-
" fgit jdl\n" +
27-
"EXAMPLE\n" +
28-
" fgit jsdget https://github.com/fastgitorg/fgit-go/archive/master.zip"
29-
getHelpMsg = "FastGit Get Command Line Tool\n" +
16+
GetHelpMsg = "FastGit Get Command Line Tool\n" +
3017
"=============================\n" +
3118
"REMARKS\n" +
3219
" Download with FastGit automatically\n" +
@@ -39,7 +26,7 @@ const (
3926
" fgit download\n" +
4027
"EXAMPLE\n" +
4128
" fgit get https://github.com/fastgitorg/fgit-go/archive/master.zip"
42-
debugHelpMsg = "FastGit Debug Command Line Tool\n" +
29+
DebugHelpMsg = "FastGit Debug Command Line Tool\n" +
4330
"===============================\n" +
4431
"SYNTAX\n" +
4532
" fgit debug [--help|-h]\n" +
@@ -51,7 +38,7 @@ const (
5138
"EXAMPLE\n" +
5239
" fgit debug\n" +
5340
" fgit debug https://fastgit.org"
54-
convHelpMsg = "FastGit Conv Command Line Tool\n" +
41+
ConvHelpMsg = "FastGit Conv Command Line Tool\n" +
5542
"==============================\n" +
5643
"REMARKS\n" +
5744
" Convert upstream between GitHub or FastGit UK automatically\n" +

0 commit comments

Comments
 (0)