@@ -33,6 +33,8 @@ func NewInstallCmd() (cmd *cobra.Command) {
3333 "If fetch the latest config from https://github.com/LinuxSuRen/hd-home" )
3434 flags .BoolVarP (& opt .Download , "download" , "" , true ,
3535 "If download the package" )
36+ flags .BoolVarP (& opt .CleanPackage , "clean-package" , "" , true ,
37+ "Clean the package if the installation is success" )
3638 flags .IntVarP (& opt .Thread , "thread" , "t" , 4 ,
3739 `Download file with multi-threads. It only works when its value is bigger than 1` )
3840 flags .BoolVarP (& opt .KeepPart , "keep-part" , "" , false ,
@@ -45,8 +47,9 @@ func NewInstallCmd() (cmd *cobra.Command) {
4547
4648type installOption struct {
4749 downloadOption
48- Download bool
49- Mode string
50+ Download bool
51+ CleanPackage bool
52+ Mode string
5053}
5154
5255func (o * installOption ) preRunE (cmd * cobra.Command , args []string ) (err error ) {
@@ -69,12 +72,13 @@ func (o *installOption) runE(cmd *cobra.Command, args []string) (err error) {
6972
7073 var source string
7174 var target string
75+ tarFile := o .Output
7276 if o .Tar {
73- if err = o .extractFiles (o . Output , o .name ); err == nil {
74- source = fmt .Sprintf ("%s/%s" , filepath .Dir (o . Output ), o .name )
77+ if err = o .extractFiles (tarFile , o .name ); err == nil {
78+ source = fmt .Sprintf ("%s/%s" , filepath .Dir (tarFile ), o .name )
7579 target = fmt .Sprintf ("/usr/local/bin/%s" , targetBinary )
7680 } else {
77- err = fmt .Errorf ("cannot extract %s from tar file, error: %v" , o . Output , err )
81+ err = fmt .Errorf ("cannot extract %s from tar file, error: %v" , tarFile , err )
7882 }
7983 } else {
8084 source = o .downloadOption .Output
@@ -101,6 +105,12 @@ func (o *installOption) runE(cmd *cobra.Command, args []string) (err error) {
101105 if err == nil && o .Package != nil && o .Package .TestInstall != nil {
102106 err = execCommand (o .Package .TestInstall .Cmd , o .Package .TestInstall .Args ... )
103107 }
108+
109+ if err == nil && o .CleanPackage {
110+ if cleanErr := os .RemoveAll (tarFile ); cleanErr != nil {
111+ cmd .Println ("cannot remove file" , tarFile , ", error:" , cleanErr )
112+ }
113+ }
104114 }
105115 return
106116}
@@ -118,15 +128,19 @@ func (o *installOption) overWriteBinary(sourceFile, targetPath string) (err erro
118128 }
119129
120130 var cp string
121- if cp , err = exec .LookPath ("cp " ); err == nil {
122- err = syscall .Exec (cp , []string {"cp " , sourceFile , targetPath }, os .Environ ())
131+ if cp , err = exec .LookPath ("mv " ); err == nil {
132+ err = syscall .Exec (cp , []string {"mv " , sourceFile , targetPath }, os .Environ ())
123133 }
124134 default :
125135 sourceF , _ := os .Open (sourceFile )
126136 targetF , _ := os .OpenFile (targetPath , os .O_CREATE | os .O_RDWR , 0600 )
127137 if _ , err = io .Copy (targetF , sourceF ); err != nil {
128138 err = fmt .Errorf ("cannot copy %s from %s to %v, error: %v" , o .name , sourceFile , targetPath , err )
129139 }
140+
141+ if err == nil {
142+ _ = os .RemoveAll (sourceFile )
143+ }
130144 }
131145 return
132146}
0 commit comments