Skip to content

Commit 00dd84f

Browse files
authored
Add shortcode support for gitee (#34)
1 parent f8b54d0 commit 00dd84f

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ BUILDFLAGS = -ldflags "-X github.com/linuxsuren/cobra-extension/version.version=
77
build:
88
CGO_ENABLE=0 go build $(BUILDFLAGS) -o bin/cgit
99

10+
build-linux:
11+
CGO_ENABLE=0 GOOS=linux go build $(BUILDFLAGS) -o bin/linux/cgit
12+
upx bin/linux/cgit
13+
1014
goreleaser:
1115
goreleaser release --rm-dist --snapshot
1216

main.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const (
2020
// TargetCLI is the target command for alias
2121
TargetCLI = "git"
2222
// AliasCLI is the alias command
23-
AliasCLI = "cgit"
23+
AliasCLI = "cgit"
2424
)
2525

2626
func main() {
@@ -51,18 +51,25 @@ func main() {
5151
}
5252

5353
func preHook(args []string) []string {
54-
args = preferGitHub(args)
54+
args = parseShortCode(args)
5555
useMirror(args)
5656
return args
5757
}
5858

59-
func preferGitHub(args []string) []string {
59+
func parseShortCode(args []string) []string {
6060
if len(args) <= 1 || args[0] != "clone" {
6161
return args
6262
}
6363

6464
address := args[1]
65-
if !strings.HasPrefix(address, "http") && !strings.HasPrefix(address, "git@") {
65+
if strings.HasPrefix(address, "gitee.com") {
66+
args[1] = fmt.Sprintf("https://%s", address)
67+
68+
if len(args) == 2 {
69+
args = append(args, path.Join(strings.ReplaceAll(viper.GetString("ws"), "github", "gitee"),
70+
strings.ReplaceAll(address, "gitee.com", "")))
71+
}
72+
} else if !strings.HasPrefix(address, "http") && !strings.HasPrefix(address, "git@") {
6673
args[1] = fmt.Sprintf("https://github.com.cnpmjs.org/%s", address)
6774

6875
if len(args) == 2 {

mirror_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ func TestMirror(t *testing.T) {
2222

2323
func TestPreferGitHub(t *testing.T) {
2424
args := []string{"clone", "a/b"}
25-
preferGitHub(args)
25+
parseShortCode(args)
2626
assert.Equal(t, args[1], "https://github.com.cnpmjs.org/a/b")
2727
}

0 commit comments

Comments
 (0)