-
Notifications
You must be signed in to change notification settings - Fork 8
/
versioncheck.go
81 lines (65 loc) · 2.2 KB
/
versioncheck.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package fncobra
import (
"context"
"fmt"
"time"
"golang.org/x/mod/semver"
"namespacelabs.dev/foundation/internal/cli/version"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnapi"
"namespacelabs.dev/foundation/internal/frontend/cuefrontend"
"namespacelabs.dev/foundation/schema"
)
type remoteStatus struct {
Version string
NewVersion bool
BuildTime time.Time
}
// Checks for updates and messages from Namespace developers.
// Does nothing if a check for remote status failed
func checkRemoteStatus(ctx context.Context, channel chan remoteStatus) {
defer close(channel)
ver, err := version.Current()
if err != nil {
fmt.Fprintln(console.Debug(ctx), "failed to obtain version information", err)
return
}
if ver.BuildTime == nil || ver.Version == version.DevelopmentBuildVersion {
return // Nothing to check.
}
fnReqs := getWorkspaceRequirements(ctx)
minimumApi := 0
if fnReqs != nil {
minimumApi = int(fnReqs.MinimumApi)
}
fmt.Fprintf(console.Debug(ctx), "version check: current %s, build time %v, min API %d\n",
ver.Version, ver.BuildTime, minimumApi)
resp, err := fnapi.GetLatestVersion(ctx, fnReqs)
if err != nil {
fmt.Fprintln(console.Debug(ctx), "version check failed:", err)
}
newVersion := semver.Compare(resp.Version, ver.Version) > 0
fmt.Fprintf(console.Debug(ctx), "version check: got %s, build time: %v, new: %v\n",
resp.Version, resp.BuildTime, newVersion)
channel <- remoteStatus{
Version: resp.Version,
BuildTime: resp.BuildTime,
NewVersion: newVersion,
}
}
func getWorkspaceRequirements(ctx context.Context) *schema.Workspace_FoundationRequirements {
moduleRoot, err := cuefrontend.ModuleLoader.FindModuleRoot(".")
if err != nil {
// The user is not inside of a workspace. This is normal.
return nil
}
wsData, err := cuefrontend.ModuleLoader.ModuleAt(ctx, moduleRoot)
if err != nil {
// Failed to parse workspace. For the purposes of version check it's okay to proceed,
return nil
}
return wsData.Proto().Foundation
}