@@ -27,7 +27,7 @@ func NewInstallCmd() (cmd *cobra.Command) {
2727 //flags.StringVarP(&opt.Mode, "mode", "m", "package",
2828 // "If you want to install it via platform package manager")
2929 flags .BoolVarP (& opt .ShowProgress , "show-progress" , "" , true , "If show the progress of download" )
30- flags .IntVarP (& opt .Thread , "thread" , "t" , 0 ,
30+ flags .IntVarP (& opt .Thread , "thread" , "t" , 4 ,
3131 `Download file with multi-threads. It only works when its value is bigger than 1` )
3232 flags .BoolVarP (& opt .KeepPart , "keep-part" , "" , false ,
3333 "If you want to keep the part files instead of deleting them" )
@@ -42,22 +42,52 @@ type installOption struct {
4242 Mode string
4343}
4444
45+ func (o * installOption ) preRunE (cmd * cobra.Command , args []string ) (err error ) {
46+ if err = o .fetchHomeConfig (); err != nil {
47+ // this is not a fatal, don't block the process
48+ cmd .Printf ("Failed with fetching home config: %v\n " , err )
49+ }
50+ err = o .downloadOption .preRunE (cmd , args )
51+ return
52+ }
53+
4554func (o * installOption ) runE (cmd * cobra.Command , args []string ) (err error ) {
4655 if err = o .downloadOption .runE (cmd , args ); err != nil {
4756 return
4857 }
4958
50- if err = o .extractFiles (o .Output , o .name ); err == nil {
51- err = o .overWriteBinary (fmt .Sprintf ("%s/%s" , filepath .Dir (o .Output ), o .name ), fmt .Sprintf ("/usr/local/bin/%s" , o .name ))
59+ var source string
60+ var target string
61+ if o .Tar {
62+ if err = o .extractFiles (o .Output , o .name ); err == nil {
63+ source = fmt .Sprintf ("%s/%s" , filepath .Dir (o .Output ), o .name )
64+ target = fmt .Sprintf ("/usr/local/bin/%s" , o .name )
65+ } else {
66+ err = fmt .Errorf ("cannot extract %s from tar file, error: %v" , o .Output , err )
67+ }
5268 } else {
53- err = fmt .Errorf ("cannot extract %s from tar file, error: %v" , o .Output , err )
69+ source = o .downloadOption .Output
70+ target = fmt .Sprintf ("/usr/local/bin/%s" , o .name )
71+ }
72+
73+ if err == nil {
74+ fmt .Println ("install" , source , "to" , target )
75+ err = o .overWriteBinary (source , target )
5476 }
5577 return
5678}
5779
5880func (o * installOption ) overWriteBinary (sourceFile , targetPath string ) (err error ) {
5981 switch runtime .GOOS {
60- case "linux" :
82+ case "linux" , "darwin" :
83+ if err = execCommand ("chmod" , "u+x" , sourceFile ); err != nil {
84+ return
85+ }
86+
87+ if err = execCommand ("rm" , "-rf" , targetPath ); err != nil {
88+ return
89+ }
90+
6191 var cp string
6292 if cp , err = exec .LookPath ("cp" ); err == nil {
6393 err = syscall .Exec (cp , []string {"cp" , sourceFile , targetPath }, os .Environ ())
0 commit comments