This repository was archived by the owner on Sep 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathupgrade.go
More file actions
105 lines (80 loc) · 2.33 KB
/
upgrade.go
File metadata and controls
105 lines (80 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package software
import (
"fmt"
"os"
"runtime"
"strings"
"github.com/nhost/cli/clienv"
"github.com/nhost/cli/software"
"github.com/urfave/cli/v2"
)
func CommandUpgrade() *cli.Command {
return &cli.Command{ //nolint:exhaustruct
Name: "upgrade",
Aliases: []string{},
Usage: "Upgrade the CLI to the latest version",
Action: commandUpgrade,
}
}
func commandUpgrade(cCtx *cli.Context) error {
ce := clienv.FromCLI(cCtx)
mgr := software.NewManager()
releases, err := mgr.GetReleases(cCtx.Context, cCtx.App.Version)
if err != nil {
return fmt.Errorf("failed to get releases: %w", err)
}
if len(releases) == 0 {
ce.Infoln("You have the latest version. Hurray!")
return nil
}
latest := releases[0]
if latest.TagName == cCtx.App.Version {
ce.Infoln("You have the latest version. Hurray!")
return nil
}
ce.Infoln("Upgrading to %s...", latest.TagName)
version := latest.TagName
s := strings.Split(latest.TagName, "@")
if len(s) == 2 { //nolint:mnd
version = s[1]
}
want := fmt.Sprintf("cli-%s-%s-%s.tar.gz", version, runtime.GOOS, runtime.GOARCH)
var url string
for _, asset := range latest.Assets {
if asset.Name == want {
url = asset.BrowserDownloadURL
}
}
if url == "" {
return fmt.Errorf("failed to find asset for %s", want) //nolint:err113
}
tmpFile, err := os.CreateTemp(os.TempDir(), "nhost-cli-")
if err != nil {
return fmt.Errorf("failed to create temporary file: %w", err)
}
defer os.Remove(tmpFile.Name())
defer tmpFile.Close()
if err := mgr.DownloadAsset(cCtx.Context, url, tmpFile); err != nil {
return fmt.Errorf("failed to download asset: %w", err)
}
return install(cCtx, ce, tmpFile.Name())
}
func install(cCtx *cli.Context, ce *clienv.CliEnv, tmpFile string) error {
curBin, err := os.Executable()
if err != nil {
return fmt.Errorf("failed to find installed CLI: %w", err)
}
if cCtx.App.Version == devVersion || cCtx.App.Version == "" {
// we are in dev mode, we fake curBin for testing
curBin = "/tmp/nhost"
}
ce.Infoln("Copying to %s...", curBin)
if err := os.Rename(tmpFile, curBin); err != nil {
return fmt.Errorf("failed to rename %s to %s: %w", tmpFile, curBin, err)
}
ce.Infoln("Setting permissions...")
if err := os.Chmod(curBin, 0o755); err != nil { //nolint:mnd
return fmt.Errorf("failed to set permissions on %s: %w", curBin, err)
}
return nil
}