@@ -42,6 +42,8 @@ func newInstallCmd(ctx context.Context) (cmd *cobra.Command) {
4242 "Indicate if install it via go install github.com/xxx/xxx" )
4343 flags .StringVarP (& opt .fromBranch , "from-branch" , "" , "master" ,
4444 "Only works if the flag --from-source is true" )
45+ flags .BoolVarP (& opt .goget , "goget" , "" , false ,
46+ "Use command goget to download the binary, only works if the flag --from-source is true" )
4547 flags .StringVarP (& opt .ProxyGitHub , "proxy-github" , "" , "" ,
4648 `The proxy address of github.com, the proxy address will be the prefix of the final address.
4749Available proxy: gh.api.99988866.xyz
@@ -71,6 +73,7 @@ type installOption struct {
7173 CleanPackage bool
7274 fromSource bool
7375 fromBranch string
76+ goget bool
7477 force bool
7578
7679 // inner fields
@@ -161,31 +164,45 @@ func (o *installOption) installFromSource() (err error) {
161164 return
162165 }
163166
164- gopath := sysos .Getenv ("GOPATH" )
165- if gopath == "" {
166- err = fmt .Errorf ("GOPATH is required" )
167- return
168- }
169-
170167 if o .org == "" || o .repo == "" {
171168 err = fmt .Errorf ("org: '%s' or repo: '%s' is empty" , o .org , o .repo )
172169 return
173170 }
174171
175- if err = exec .RunCommandInDir ("go" , sysos .TempDir (), strings .Split (o .buildGoInstallCmd (), " " )[1 :]... ); err != nil {
176- err = fmt .Errorf ("faield to run go install command, error: %v" , err )
177- return
172+ var binaryPath string
173+ if o .goget {
174+ binaryPath , err = o .runGogetCommand (fmt .Sprintf ("github.com/%s/%s" , o .org , o .repo ), o .repo )
175+ } else {
176+ binaryPath , err = o .buildGoSource ()
178177 }
179178
180- sourcePath := path .Join (gopath , fmt .Sprintf ("bin/%s" , o .name ))
181- if common .Exist (sourcePath ) {
179+ if err == nil && binaryPath != "" {
182180 is := & installer.Installer {}
183181 targetName := o .name
184182 if o .Package != nil && o .Package .TargetBinary != "" {
185183 targetName = o .Package .TargetBinary
186184 }
187- err = is .OverWriteBinary (sourcePath , fmt .Sprintf ("/usr/local/bin/%s" , targetName ))
188- } else {
185+ err = is .OverWriteBinary (binaryPath , fmt .Sprintf ("/usr/local/bin/%s" , targetName ))
186+ }
187+ return
188+ }
189+
190+
191+
192+ func (o * installOption ) buildGoSource () (binaryPath string , err error ) {
193+ gopath := sysos .Getenv ("GOPATH" )
194+ if gopath == "" {
195+ err = fmt .Errorf ("GOPATH is required" )
196+ return
197+ }
198+
199+ if err = exec .RunCommandInDir ("go" , sysos .TempDir (), strings .Split (o .buildGoInstallCmd (), " " )[1 :]... ); err != nil {
200+ err = fmt .Errorf ("faield to run go install command, error: %v" , err )
201+ return
202+ }
203+
204+ binaryPath = path .Join (gopath , fmt .Sprintf ("bin/%s" , o .name ))
205+ if ! common .Exist (binaryPath ) {
189206 err = fmt .Errorf ("no found %s from GOPATH" , o .name )
190207 }
191208 return
@@ -194,3 +211,24 @@ func (o *installOption) installFromSource() (err error) {
194211func (o * installOption ) buildGoInstallCmd () string {
195212 return fmt .Sprintf ("go install github.com/%s/%s@%s" , o .org , o .repo , o .fromBranch )
196213}
214+
215+ func (o * installOption ) runGogetCommand (repo , name string ) (binaryPath string , err error ) {
216+ // make sure goget command exists
217+ is := installer.Installer {
218+ Provider : "github" ,
219+ }
220+ if err = is .CheckDepAndInstall (map [string ]string {
221+ "goget" : "linuxsuren/goget" ,
222+ }); err != nil {
223+ err = fmt .Errorf ("cannot download goget, error: %v" , err )
224+ return
225+ }
226+
227+ // run goget command
228+ tmpPath := sysos .TempDir ()
229+ binaryPath = path .Join (tmpPath , name )
230+ if err = exec .RunCommandInDir ("goget" , tmpPath , repo ); err != nil {
231+ err = fmt .Errorf ("faield to run go install command, error: %v" , err )
232+ }
233+ return
234+ }
0 commit comments