11package main
22
33import (
4- "context"
54 "fmt"
5+ ext "github.com/linuxsuren/cobra-extension"
66 extver "github.com/linuxsuren/cobra-extension/version"
7- alias "github.com/linuxsuren/go-cli-alias/pkg"
87 aliasCmd "github.com/linuxsuren/go-cli-alias/pkg/cmd"
98 "github.com/spf13/cobra"
109 "os"
@@ -13,68 +12,39 @@ import (
1312 "syscall"
1413)
1514
15+ const (
16+ TargetCLI = "git"
17+ AliasCLI = "cgit"
18+ )
19+
1620func main () {
1721 cmd := & cobra.Command {
18- Use : "cgit" ,
22+ Use : AliasCLI ,
1923 RunE : func (cmd * cobra.Command , args []string ) (err error ) {
2024 env := os .Environ ()
2125
22- preferGitHub (args )
23- useMirror (args )
26+ preHook (args )
2427
2528 var gitBinary string
26- if gitBinary , err = exec .LookPath ("git" ); err == nil {
27- syscall .Exec (gitBinary , append ([]string {"git" }, args ... ), env )
29+ if gitBinary , err = exec .LookPath (TargetCLI ); err == nil {
30+ syscall .Exec (gitBinary , append ([]string {TargetCLI }, args ... ), env )
2831 }
2932 return
3033 },
3134 }
3235
33- cmd .AddCommand (extver .NewVersionCmd ("linuxsuren" , "cgit" , "cgit" , nil ))
36+ cmd .AddCommand (extver .NewVersionCmd ("linuxsuren" , AliasCLI , AliasCLI , nil ))
3437
35- var ctx context.Context
36- if defMgr , err := alias .GetDefaultAliasMgrWithNameAndInitialData (cmd .Name (), []alias.Alias {
37- {Name : "cm" , Command : "checkout master" },
38- }); err == nil {
39- ctx = context .WithValue (context .Background (), alias .AliasKey , defMgr )
38+ aliasCmd .AddAliasCmd (cmd , getAliasList ())
4039
41- cmd .AddCommand (aliasCmd .NewRootCommand (ctx ))
42- } else {
43- cmd .Println (fmt .Errorf ("cannot get default alias manager, error: %v" , err ))
44- }
40+ cmd .AddCommand (ext .NewCompletionCmd (cmd ))
4541
46- cmd .SilenceErrors = true
47- a := cmd .Execute ()
48- if a != nil && strings .Contains (a .Error (), "unknown command" ) {
49- args := os .Args [1 :]
50- var ctx context.Context
51- var defMgr * alias.DefaultAliasManager
52- var err error
53- if defMgr , err = alias .GetDefaultAliasMgrWithNameAndInitialData (cmd .Name (), []alias.Alias {
54- {Name : "cm" , Command : "checkout master" },
55- }); err == nil {
56- ctx = context .WithValue (context .Background (), alias .AliasKey , defMgr )
57- var gitBinary string
58- var targetCmd []string
59- env := os .Environ ()
60-
61- if gitBinary , err = exec .LookPath ("git" ); err != nil {
62- panic ("cannot find git" )
63- }
64-
65- if ok , redirect := aliasCmd .RedirectToAlias (ctx , args ); ok {
66- args = redirect
67- }
68-
69- preferGitHub (args )
70- useMirror (args )
42+ aliasCmd .Execute (cmd , TargetCLI , getAliasList (), preHook )
43+ }
7144
72- targetCmd = append ([]string {"git" }, args ... )
73- _ = syscall .Exec (gitBinary , targetCmd , env ) // ignore the errors due to we've no power to deal with it
74- } else {
75- err = fmt .Errorf ("cannot get default alias manager, error: %v" , err )
76- }
77- }
45+ func preHook (args []string ) {
46+ preferGitHub (args )
47+ useMirror (args )
7848}
7949
8050func preferGitHub (args []string ) {
@@ -83,7 +53,7 @@ func preferGitHub(args []string) {
8353 }
8454
8555 address := args [1 ]
86- if ! strings .HasPrefix (address , "http" ) {
56+ if ! strings .HasPrefix (address , "http" ) || ! strings . HasPrefix ( address , "git@" ) {
8757 args [1 ] = fmt .Sprintf ("https://github.com.cnpmjs.org/%s" , address )
8858 }
8959}
0 commit comments