-
Notifications
You must be signed in to change notification settings - Fork 220
/
update.go
110 lines (91 loc) · 3.23 KB
/
update.go
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
106
107
108
109
110
package version
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"github.com/blang/semver"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/internal/buildinfo"
"github.com/superfly/flyctl/internal/cache"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/update"
"github.com/superfly/flyctl/iostreams"
)
func newUpdate() *cobra.Command {
const (
short = "Checks for available updates and automatically updates"
long = `Checks for update and if one is available, runs the appropriate
command to update the application.`
)
return command.New("update", short, long, runUpdate)
}
func runUpdate(ctx context.Context) error {
release, err := update.LatestRelease(ctx, cache.FromContext(ctx).Channel())
switch {
case err != nil:
return fmt.Errorf("failed determining latest release: %w", err)
case release == nil:
return fmt.Errorf("failed querying latest release information: %w", err)
}
latest, err := semver.ParseTolerant(release.Version)
if err != nil {
return fmt.Errorf("error parsing latest release version number %q: %w",
release.Version, err)
}
if buildinfo.Version().GTE(latest) {
return errors.New("no available update")
}
io := iostreams.FromContext(ctx)
if err = update.UpgradeInPlace(ctx, io, release.Prerelease); err != nil {
return err
}
return printVersionUpdate(ctx, buildinfo.Version())
}
// printVersionUpdate prints "Updated flyctl [oldVersion] -> [newVersion]"
func printVersionUpdate(ctx context.Context, oldVersion semver.Version) error {
io := iostreams.FromContext(ctx)
currentVer, err := getNewVersion(ctx)
if err != nil {
if strings.Contains(err.Error(), "failed to parse version") {
// This is probably fine, likely a change between the two versions makes
// flyctl <-> flyctl communication incompatible
return nil
} else {
return err
}
}
if currentVer.EQ(oldVersion) {
fmt.Fprintf(io.ErrOut, "Flyctl was updated, but the flyctl pointed to by '%s' is still version %s.\n", os.Args[0], currentVer.String())
fmt.Fprintf(io.ErrOut, "Please ensure that your PATH is set correctly!")
return nil
}
fmt.Fprintf(io.Out, "Updated flyctl v%s -> v%s\n", oldVersion.String(), currentVer.String())
return nil
}
// getNewVersion executes [os.Args[0], "version", "--json"] and parses the output into a semver.Version
func getNewVersion(ctx context.Context) (semver.Version, error) {
var ver semver.Version
newVersionJson, err := exec.CommandContext(ctx, os.Args[0], "version", "--json").CombinedOutput()
if err != nil {
return ver, fmt.Errorf("failed to execute new flyctl binary: %w", err)
}
// Parsing into a map instead of the struct directly so that
// small changes in the version struct don't break this.
parsed := map[string]string{}
if err = json.Unmarshal(newVersionJson, &parsed); err != nil {
return ver, fmt.Errorf("failed to parse version of new flyctl binary: %w", err)
}
semverStr, ok := parsed["Version"]
if !ok {
return ver, errors.New("failed to parse version of new flyctl binary: field 'Version' not in output of 'fly version --json'")
}
ver, err = semver.ParseTolerant(semverStr)
if err != nil {
return ver, fmt.Errorf("failed to parse version of new flyctl binary: %w", err)
}
return ver, nil
}