Skip to content

Commit 2cf6cd4

Browse files
authored
Support to install package from source via goget (#175)
* Use goget to install a package from source * Fix the packing format error * Fix the wrong package parsing
1 parent fc8b905 commit 2cf6cd4

File tree

2 files changed

+54
-14
lines changed

2 files changed

+54
-14
lines changed

cmd/install.go

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
4749
Available 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) {
194211
func (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+
}

pkg/installer/check.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ func (o *Installer) ProviderURLParse(path string, acceptPreRelease bool) (packag
192192
o.Package = &cfg
193193
o.AdditionBinaries = cfg.AdditionBinaries
194194
o.Tar = cfg.Tar != "false"
195+
packagingFormat = getPackagingFormat(o) // rewrite the packing format due to the package config might be read from git repository
195196

196197
if cfg.LatestVersion != "" {
197198
version = getVersionOrDefault(cfg.LatestVersion, version)
@@ -241,10 +242,11 @@ func (o *Installer) ProviderURLParse(path string, acceptPreRelease bool) (packag
241242
if err = tmp.Execute(&buf, hdPkg); err == nil {
242243
packageURL = fmt.Sprintf("https://github.com/%s/%s/releases/download/%s/%s",
243244
o.Org, o.Repo, version, buf.String())
245+
o.Output = buf.String()
244246
if o.Tar && !hasPackageSuffix(packageURL) {
245247
packageURL = fmt.Sprintf("%s.%s", packageURL, packagingFormat)
248+
o.Output = fmt.Sprintf("%s.%s", o.Output, packagingFormat)
246249
}
247-
o.Output = buf.String()
248250
} else {
249251
return
250252
}

0 commit comments

Comments
 (0)