Skip to content

Commit 0648fd6

Browse files
fix(cmd/gf): add extra option to controller the behavior downloading … (gogf#4435)
gogf#3938
1 parent d0cfcce commit 0648fd6

1 file changed

Lines changed: 64 additions & 6 deletions

File tree

cmd/gf/internal/cmd/cmd_up.go

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/gogf/gf/v2/container/gset"
1717
"github.com/gogf/gf/v2/frame/g"
18+
"github.com/gogf/gf/v2/os/genv"
1819
"github.com/gogf/gf/v2/os/gfile"
1920
"github.com/gogf/gf/v2/os/gproc"
2021
"github.com/gogf/gf/v2/text/gstr"
@@ -39,7 +40,11 @@ gf up
3940
gf up -a
4041
gf up -c
4142
gf up -cf
43+
gf up -a -m=install
44+
gf up -a -m=install -p=github.com/gogf/gf/cmd/gf/v2@latest
4245
`
46+
cliMethodHttpDownload = "http"
47+
cliMethodGoInstall = "install"
4348
)
4449

4550
func init() {
@@ -49,10 +54,14 @@ func init() {
4954
}
5055

5156
type cUpInput struct {
52-
g.Meta `name:"up" config:"gfcli.up"`
53-
All bool `name:"all" short:"a" brief:"upgrade both version and cli, auto fix codes" orphan:"true"`
54-
Cli bool `name:"cli" short:"c" brief:"also upgrade CLI tool" orphan:"true"`
55-
Fix bool `name:"fix" short:"f" brief:"auto fix codes(it only make sense if cli is to be upgraded)" orphan:"true"`
57+
g.Meta `name:"up" config:"gfcli.up"`
58+
All bool `name:"all" short:"a" brief:"upgrade both version and cli, auto fix codes" orphan:"true"`
59+
Cli bool `name:"cli" short:"c" brief:"also upgrade CLI tool" orphan:"true"`
60+
Fix bool `name:"fix" short:"f" brief:"auto fix codes(it only make sense if cli is to be upgraded)" orphan:"true"`
61+
CliDownloadingMethod string `name:"cli-download-method" short:"m" brief:"cli upgrade method: http=download binary via HTTP GET, install=upgrade via go install" d:"http"`
62+
// CliModulePath specifies the module path for CLI installation via go install.
63+
// This is used when CliDownloadingMethod is set to "install".
64+
CliModulePath string `name:"cli-module-path" short:"p" brief:"custom cli module path for upgrade CLI tool with go install method" d:"github.com/gogf/gf/cmd/gf/v2@latest"`
5665
}
5766

5867
type cUpOutput struct{}
@@ -76,7 +85,7 @@ func (c cUp) Index(ctx context.Context, in cUpInput) (out *cUpOutput, err error)
7685
}
7786

7887
if in.Cli {
79-
if err = c.doUpgradeCLI(ctx); err != nil {
88+
if err = c.doUpgradeCLI(ctx, in); err != nil {
8089
return nil, err
8190
}
8291
}
@@ -170,8 +179,22 @@ func (c cUp) doUpgradeVersion(ctx context.Context, in cUpInput) (out *doUpgradeV
170179
}
171180

172181
// doUpgradeCLI downloads the new version binary with process.
173-
func (c cUp) doUpgradeCLI(ctx context.Context) (err error) {
182+
func (c cUp) doUpgradeCLI(ctx context.Context, in cUpInput) (err error) {
174183
mlog.Print(`start upgrading cli...`)
184+
fmt.Println(` cli upgrade method:`, in.CliDownloadingMethod)
185+
switch in.CliDownloadingMethod {
186+
case cliMethodHttpDownload:
187+
return c.doUpgradeCLIWithHttpDownload(ctx)
188+
case cliMethodGoInstall:
189+
return c.doUpgradeCLIWithGoInstall(ctx, in)
190+
default:
191+
mlog.Fatalf(`invalid cli upgrade method: "%s", please use "http" or "install"`, in.CliDownloadingMethod)
192+
}
193+
return
194+
}
195+
196+
func (c cUp) doUpgradeCLIWithHttpDownload(ctx context.Context) (err error) {
197+
mlog.Print(`start upgrading cli with http get download...`)
175198
var (
176199
downloadUrl = fmt.Sprintf(
177200
`https://github.com/gogf/gf/releases/latest/download/gf_%s_%s`,
@@ -213,6 +236,41 @@ func (c cUp) doUpgradeCLI(ctx context.Context) (err error) {
213236
return
214237
}
215238

239+
func (c cUp) doUpgradeCLIWithGoInstall(ctx context.Context, in cUpInput) (err error) {
240+
mlog.Print(`upgrading cli with go install...`)
241+
if !genv.Contains("GOPATH") {
242+
mlog.Fatal(`"GOPATH" environment variable does not exist, please check your go installation`)
243+
}
244+
245+
command := fmt.Sprintf(`go install %s`, in.CliModulePath)
246+
mlog.Printf(`running command: %s`, command)
247+
err = gproc.ShellRun(ctx, command)
248+
if err != nil {
249+
return err
250+
}
251+
252+
cliFilePath := gfile.Join(genv.Get("GOPATH").String(), "bin/gf")
253+
if runtime.GOOS == "windows" {
254+
cliFilePath += ".exe"
255+
}
256+
257+
// It fails if file not exist or its size is less than 1MB.
258+
if !gfile.Exists(cliFilePath) || gfile.Size(cliFilePath) < 1024*1024 {
259+
mlog.Fatalf(`go install %s failed, "%s" does not exist or its size is less than 1MB`, in.CliModulePath, cliFilePath)
260+
}
261+
262+
newFile, err := gfile.Open(cliFilePath)
263+
if err != nil {
264+
return err
265+
}
266+
// selfupdate
267+
err = selfupdate.Apply(newFile, selfupdate.Options{})
268+
if err != nil {
269+
return err
270+
}
271+
return
272+
}
273+
216274
func (c cUp) doAutoFixing(ctx context.Context, dirPath string, version string) (err error) {
217275
mlog.Printf(`auto fixing directory path "%s" from version "%s" ...`, dirPath, version)
218276
command := fmt.Sprintf(`gf fix -p %s`, dirPath)

0 commit comments

Comments
 (0)