-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathupdate.go
More file actions
66 lines (54 loc) · 1.66 KB
/
update.go
File metadata and controls
66 lines (54 loc) · 1.66 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
package update
import (
"fmt"
"net/http"
"runtime"
"time"
"github.com/deepsourcelabs/cli/buildinfo"
"github.com/deepsourcelabs/cli/internal/update"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
func NewCmdUpdate() *cobra.Command {
return &cobra.Command{
Use: "update",
Short: "Update DeepSource CLI to the latest version",
RunE: func(cmd *cobra.Command, _ []string) error {
return runUpdate(cmd)
},
}
}
func runUpdate(cmd *cobra.Command) error {
w := cmd.ErrOrStderr()
// Check for the latest version
checkClient := &http.Client{Timeout: 10 * time.Second}
if err := update.CheckForUpdate(checkClient); err != nil {
return fmt.Errorf("checking for updates: %w", err)
}
state, err := update.PrepareUpdate()
if err != nil {
return fmt.Errorf("reading update state: %w", err)
}
if state == nil {
bi := buildinfo.GetBuildInfo()
fmt.Fprintf(w, "Already up to date (v%s)\n", bi.Version)
return nil
}
fmt.Fprintln(w, pterm.Green("✓")+" Platform: "+runtime.GOOS+"/"+runtime.GOARCH)
fmt.Fprintln(w, pterm.Green("✓")+" Version: v"+state.Version)
applyClient := &http.Client{Timeout: 30 * time.Second}
data, err := update.DownloadUpdate(applyClient, state)
if err != nil {
return fmt.Errorf("downloading update: %w", err)
}
fmt.Fprintln(w, pterm.Green("✓")+" Downloaded")
if err := update.VerifyUpdate(data, state); err != nil {
return fmt.Errorf("verifying update: %w", err)
}
fmt.Fprintln(w, pterm.Green("✓")+" Checksum verified")
if err := update.ExtractAndInstall(data, state.ArchiveURL); err != nil {
return fmt.Errorf("installing update: %w", err)
}
fmt.Fprintln(w, pterm.Green("✓")+" Installed")
return nil
}