-
Notifications
You must be signed in to change notification settings - Fork 230
/
update.go
50 lines (40 loc) · 1.26 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
package version
import (
"context"
"errors"
"fmt"
"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/pkg/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)
return update.UpgradeInPlace(ctx, io, release.Prerelease)
}