Skip to content

Commit f8b54d0

Browse files
authored
Support to clone code to customize directory (#31)
* Support to clone code to customize directory * Add goreleaser command in the readme file * Bump goreleaser action from v2.5.0 to v2.8.0
1 parent c636394 commit f8b54d0

23 files changed

+294
-34
lines changed

.github/workflows/pull-request.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ jobs:
1010
name: Build
1111
runs-on: macos-10.15
1212
steps:
13-
- name: Set up Go 1.13
13+
- name: Set up Go 1.16
1414
uses: actions/setup-go@v2.1.3
1515
with:
16-
go-version: 1.13
16+
go-version: 1.16
1717
id: go
1818
- name: Check out code into the Go module directory
1919
uses: actions/checkout@v2.3.4
2020
- name: Run GoReleaser
21-
uses: goreleaser/goreleaser-action@v2.5.0
21+
uses: goreleaser/goreleaser-action@v2.8.0
2222
with:
2323
version: latest
2424
args: check
@@ -29,10 +29,10 @@ jobs:
2929
name: Lint
3030
runs-on: ubuntu-latest
3131
steps:
32-
- name: Set up Go 1.13
32+
- name: Set up Go 1.16
3333
uses: actions/setup-go@v2.1.3
3434
with:
35-
go-version: 1.13
35+
go-version: 1.16
3636
id: go
3737
- name: Check out code into the Go module directory
3838
uses: actions/checkout@v2.3.4

.github/workflows/release.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ jobs:
1616
- name: Set up Go
1717
uses: actions/setup-go@v2.1.3
1818
with:
19-
go-version: 1.13.x
19+
go-version: 1.16
2020
- name: Run GoReleaser
21-
uses: goreleaser/goreleaser-action@v2.5.0
21+
uses: goreleaser/goreleaser-action@v2.8.0
2222
with:
2323
version: latest
2424
args: release --rm-dist

Makefile

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

10+
goreleaser:
11+
goreleaser release --rm-dist --snapshot
12+
1013
copy: build
1114
sudo cp bin/cgit /usr/local/bin/cgit

go.mod

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module github.com/linuxsuren/cgit
22

3-
go 1.15
3+
go 1.16
44

55
require (
6-
github.com/linuxsuren/cobra-extension v0.0.10
6+
github.com/linuxsuren/cobra-extension v0.0.11
7+
github.com/linuxsuren/go-cli-alias v0.0.9
78
github.com/magiconair/properties v1.8.5
8-
github.com/linuxsuren/go-cli-alias v0.0.6
99
github.com/spf13/cobra v1.1.3
10+
github.com/spf13/viper v1.7.0
1011
)

go.sum

Lines changed: 41 additions & 16 deletions
Large diffs are not rendered by default.

main.go

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,28 @@ import (
55
"fmt"
66
"github.com/linuxsuren/cgit/cmd"
77
"github.com/linuxsuren/cgit/pkg"
8-
ext "github.com/linuxsuren/cobra-extension"
8+
ext "github.com/linuxsuren/cobra-extension/pkg"
99
extver "github.com/linuxsuren/cobra-extension/version"
1010
aliasCmd "github.com/linuxsuren/go-cli-alias/pkg/cmd"
1111
"github.com/spf13/cobra"
12+
"github.com/spf13/viper"
13+
"os"
1214
"os/exec"
15+
"path"
1316
"strings"
1417
)
1518

1619
const (
20+
// TargetCLI is the target command for alias
1721
TargetCLI = "git"
22+
// AliasCLI is the alias command
1823
AliasCLI = "cgit"
1924
)
2025

2126
func main() {
2227
command := &cobra.Command{
2328
Use: AliasCLI,
2429
RunE: func(command *cobra.Command, args []string) (err error) {
25-
fmt.Println(args, "sdfs")
2630
preHook(args)
2731

2832
command.Println(args)
@@ -39,26 +43,33 @@ func main() {
3943

4044
aliasCmd.AddAliasCmd(command, getAliasList())
4145

46+
ctx := context.TODO()
4247
command.AddCommand(ext.NewCompletionCmd(command),
43-
cmd.NewMirrorCmd(context.TODO()))
48+
cmd.NewMirrorCmd(ctx))
4449

45-
aliasCmd.Execute(command, TargetCLI, getAliasList(), preHook)
50+
aliasCmd.ExecuteContextV2(command, context.TODO(), TargetCLI, getAliasList(), preHook)
4651
}
4752

48-
func preHook(args []string) {
49-
preferGitHub(args)
53+
func preHook(args []string) []string {
54+
args = preferGitHub(args)
5055
useMirror(args)
56+
return args
5157
}
5258

53-
func preferGitHub(args []string) {
59+
func preferGitHub(args []string) []string {
5460
if len(args) <= 1 || args[0] != "clone" {
55-
return
61+
return args
5662
}
5763

5864
address := args[1]
5965
if !strings.HasPrefix(address, "http") && !strings.HasPrefix(address, "git@") {
6066
args[1] = fmt.Sprintf("https://github.com.cnpmjs.org/%s", address)
67+
68+
if len(args) == 2 {
69+
args = append(args, path.Join(viper.GetString("ws"), address))
70+
}
6171
}
72+
return args
6273
}
6374

6475
func useMirror(args []string) {
@@ -73,3 +84,21 @@ func useMirror(args []string) {
7384
}
7485
}
7586
}
87+
88+
func init() {
89+
viper.SetConfigName("cgit")
90+
viper.SetConfigType("yaml")
91+
viper.AddConfigPath("$HOME/.config")
92+
viper.AddConfigPath(".")
93+
if err := viper.ReadInConfig(); err != nil {
94+
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
95+
panic(err)
96+
}
97+
}
98+
loadDefaults()
99+
return
100+
}
101+
102+
func loadDefaults() {
103+
viper.SetDefault("ws", os.ExpandEnv("$HOME/ws/github/"))
104+
}

release/cgit-darwin-amd64.tar.gz

3.03 MB
Binary file not shown.

release/cgit-darwin-arm64.tar.gz

2.81 MB
Binary file not shown.

release/cgit-linux-64bit.deb

2.93 MB
Binary file not shown.

release/cgit-linux-64bit.rpm

2.93 MB
Binary file not shown.

0 commit comments

Comments
 (0)