@@ -33,6 +33,8 @@ func NewInstallCmd() (cmd *cobra.Command) {
33
33
"If fetch the latest config from https://github.com/LinuxSuRen/hd-home" )
34
34
flags .BoolVarP (& opt .Download , "download" , "" , true ,
35
35
"If download the package" )
36
+ flags .BoolVarP (& opt .CleanPackage , "clean-package" , "" , true ,
37
+ "Clean the package if the installation is success" )
36
38
flags .IntVarP (& opt .Thread , "thread" , "t" , 4 ,
37
39
`Download file with multi-threads. It only works when its value is bigger than 1` )
38
40
flags .BoolVarP (& opt .KeepPart , "keep-part" , "" , false ,
@@ -45,8 +47,9 @@ func NewInstallCmd() (cmd *cobra.Command) {
45
47
46
48
type installOption struct {
47
49
downloadOption
48
- Download bool
49
- Mode string
50
+ Download bool
51
+ CleanPackage bool
52
+ Mode string
50
53
}
51
54
52
55
func (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) {
69
72
70
73
var source string
71
74
var target string
75
+ tarFile := o .Output
72
76
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 )
75
79
target = fmt .Sprintf ("/usr/local/bin/%s" , targetBinary )
76
80
} 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 )
78
82
}
79
83
} else {
80
84
source = o .downloadOption .Output
@@ -101,6 +105,12 @@ func (o *installOption) runE(cmd *cobra.Command, args []string) (err error) {
101
105
if err == nil && o .Package != nil && o .Package .TestInstall != nil {
102
106
err = execCommand (o .Package .TestInstall .Cmd , o .Package .TestInstall .Args ... )
103
107
}
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
+ }
104
114
}
105
115
return
106
116
}
@@ -118,15 +128,19 @@ func (o *installOption) overWriteBinary(sourceFile, targetPath string) (err erro
118
128
}
119
129
120
130
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 ())
123
133
}
124
134
default :
125
135
sourceF , _ := os .Open (sourceFile )
126
136
targetF , _ := os .OpenFile (targetPath , os .O_CREATE | os .O_RDWR , 0600 )
127
137
if _ , err = io .Copy (targetF , sourceF ); err != nil {
128
138
err = fmt .Errorf ("cannot copy %s from %s to %v, error: %v" , o .name , sourceFile , targetPath , err )
129
139
}
140
+
141
+ if err == nil {
142
+ _ = os .RemoveAll (sourceFile )
143
+ }
130
144
}
131
145
return
132
146
}
0 commit comments