diff --git a/alias.go b/alias.go new file mode 100644 index 0000000..379a195 --- /dev/null +++ b/alias.go @@ -0,0 +1,9 @@ +package main + +import alias "github.com/linuxsuren/go-cli-alias/pkg" + +func getAliasList() []alias.Alias { + return []alias.Alias{ + {Name: "cm", Command: "checkout master"}, + } +} diff --git a/go.mod b/go.mod index 6b274ae..26de2ca 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.15 require ( github.com/linuxsuren/cobra-extension v0.0.5 - github.com/linuxsuren/go-cli-alias v0.0.3 + github.com/linuxsuren/go-cli-alias v0.0.4 github.com/magiconair/properties v1.8.4 github.com/spf13/cobra v1.1.1 ) diff --git a/go.sum b/go.sum index b237cd0..eb6a3e1 100644 --- a/go.sum +++ b/go.sum @@ -160,10 +160,15 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/linuxsuren/cobra-extension v0.0.1 h1:e288zsm1iWUaAOjfiw42hiWJsrrXZnJ0vKApWPyTfkQ= github.com/linuxsuren/cobra-extension v0.0.1/go.mod h1:UqHi31q7Dj+46nzcVWNZz5Z1JcHBJcmCyPAZRoM8LXo= +github.com/linuxsuren/cobra-extension v0.0.3/go.mod h1:UqHi31q7Dj+46nzcVWNZz5Z1JcHBJcmCyPAZRoM8LXo= github.com/linuxsuren/cobra-extension v0.0.5 h1:3go926vpXiizRzBEwt8CddCDerRsG5hlIFr0JJvGaKc= github.com/linuxsuren/cobra-extension v0.0.5/go.mod h1:qcEJv7BbL0UpK6MbrTESP/nKf1+z1wQdMAnE1NBl3QQ= +github.com/linuxsuren/go-cli-alias v0.0.3-0.20201208135826-ad33122f181c h1:YNpS+IdkZA9fvNa9gi7xA45FrVSNsEamrzdyezJndjM= +github.com/linuxsuren/go-cli-alias v0.0.3-0.20201208135826-ad33122f181c/go.mod h1:l2/nzdvung0zorpCxI1CT+Fuuc/W1sFEWS0jt3/b2ZM= github.com/linuxsuren/go-cli-alias v0.0.3 h1:IbqC+4uOTCRM27WCII7Z35pXP4bwqck0J5jg2xmIqr4= github.com/linuxsuren/go-cli-alias v0.0.3/go.mod h1:l2/nzdvung0zorpCxI1CT+Fuuc/W1sFEWS0jt3/b2ZM= +github.com/linuxsuren/go-cli-alias v0.0.4 h1:+otarDOeSZzzbTCr9CllAOQCQNpaf/HI41iDvWWUo/w= +github.com/linuxsuren/go-cli-alias v0.0.4/go.mod h1:dfwOx8H0iVpdS9gtLC80GCC4cvDvzWejxjxXn7sFWUs= github.com/linuxsuren/go-cli-plugin v0.0.1/go.mod h1:uyO09KK8otYfDV5LVTfcWX2UbAi3kEz3PrkdfIxnDlg= github.com/linuxsuren/http-downloader v0.0.2-0.20201207132639-19888a6beaec h1:N6xw+W4IvgOV0cmhE7iUWKHNZqMUXlT3in+1ORmqIGo= github.com/linuxsuren/http-downloader v0.0.2-0.20201207132639-19888a6beaec/go.mod h1:zRZY9FCDBuYNDxbI2Ny5suasZsMk7J6q9ecQ3V3PIqI= diff --git a/main.go b/main.go index 51084a2..4127c21 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,9 @@ package main import ( - "context" "fmt" + ext "github.com/linuxsuren/cobra-extension" extver "github.com/linuxsuren/cobra-extension/version" - alias "github.com/linuxsuren/go-cli-alias/pkg" aliasCmd "github.com/linuxsuren/go-cli-alias/pkg/cmd" "github.com/spf13/cobra" "os" @@ -13,68 +12,39 @@ import ( "syscall" ) +const ( + TargetCLI = "git" + AliasCLI = "cgit" +) + func main() { cmd := &cobra.Command{ - Use: "cgit", + Use: AliasCLI, RunE: func(cmd *cobra.Command, args []string) (err error) { env := os.Environ() - preferGitHub(args) - useMirror(args) + preHook(args) var gitBinary string - if gitBinary, err = exec.LookPath("git"); err == nil { - syscall.Exec(gitBinary, append([]string{"git"}, args...), env) + if gitBinary, err = exec.LookPath(TargetCLI); err == nil { + syscall.Exec(gitBinary, append([]string{TargetCLI}, args...), env) } return }, } - cmd.AddCommand(extver.NewVersionCmd("linuxsuren", "cgit", "cgit", nil)) + cmd.AddCommand(extver.NewVersionCmd("linuxsuren", AliasCLI, AliasCLI, nil)) - var ctx context.Context - if defMgr, err := alias.GetDefaultAliasMgrWithNameAndInitialData(cmd.Name(), []alias.Alias{ - {Name: "cm", Command: "checkout master"}, - }); err == nil { - ctx = context.WithValue(context.Background(), alias.AliasKey, defMgr) + aliasCmd.AddAliasCmd(cmd, getAliasList()) - cmd.AddCommand(aliasCmd.NewRootCommand(ctx)) - } else { - cmd.Println(fmt.Errorf("cannot get default alias manager, error: %v", err)) - } + cmd.AddCommand(ext.NewCompletionCmd(cmd)) - cmd.SilenceErrors = true - a := cmd.Execute() - if a != nil && strings.Contains(a.Error(), "unknown command") { - args := os.Args[1:] - var ctx context.Context - var defMgr *alias.DefaultAliasManager - var err error - if defMgr, err = alias.GetDefaultAliasMgrWithNameAndInitialData(cmd.Name(), []alias.Alias{ - {Name: "cm", Command: "checkout master"}, - }); err == nil { - ctx = context.WithValue(context.Background(), alias.AliasKey, defMgr) - var gitBinary string - var targetCmd []string - env := os.Environ() - - if gitBinary, err = exec.LookPath("git"); err != nil { - panic("cannot find git") - } - - if ok, redirect := aliasCmd.RedirectToAlias(ctx, args); ok { - args = redirect - } - - preferGitHub(args) - useMirror(args) + aliasCmd.Execute(cmd, TargetCLI, getAliasList(), preHook) +} - targetCmd = append([]string{"git"}, args...) - _ = syscall.Exec(gitBinary, targetCmd, env) // ignore the errors due to we've no power to deal with it - } else { - err = fmt.Errorf("cannot get default alias manager, error: %v", err) - } - } +func preHook(args []string) { + preferGitHub(args) + useMirror(args) } func preferGitHub(args []string) { @@ -83,7 +53,7 @@ func preferGitHub(args []string) { } address := args[1] - if !strings.HasPrefix(address, "http") { + if !strings.HasPrefix(address, "http") || !strings.HasPrefix(address, "git@") { args[1] = fmt.Sprintf("https://github.com.cnpmjs.org/%s", address) } }