-
Notifications
You must be signed in to change notification settings - Fork 9
/
fetch.go
84 lines (68 loc) · 2.6 KB
/
fetch.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
// 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 versioncheck
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/fnerrors"
"namespacelabs.dev/foundation/internal/frontend/cuefrontend"
"namespacelabs.dev/foundation/schema"
)
type Status 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 CheckRemote(ctx context.Context, computeRequirements func(context.Context) (*schema.Workspace_FoundationRequirements, error)) (*Status, error) {
ver, err := version.Current()
if err != nil {
return nil, fnerrors.InternalError("failed to obtain version information: %w", err)
}
if ver.BuildTime == nil || ver.Version == version.DevelopmentBuildVersion {
return nil, nil // Nothing to check.
}
var fnReqs *schema.Workspace_FoundationRequirements
if computeRequirements != nil {
reqs, err := computeRequirements(ctx)
if err != nil {
return nil, fnerrors.InternalError("failed to compute workspace requirements: %w", err)
}
fnReqs = reqs
}
fmt.Fprintf(console.Debug(ctx), "version check: current %s, build time %v, min API %d\n",
ver.Version, ver.BuildTime, fnReqs.GetMinimumApi())
resp, err := fnapi.GetLatestVersion(ctx, fnReqs)
if err != nil {
return nil, fnerrors.InternalError("version check failed: %w", 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)
return &Status{
Version: resp.Version,
BuildTime: resp.BuildTime,
NewVersion: newVersion,
}, nil
}
// XXX this method is not correct; it does not take into account the API requirements of the module's dependencies.
func FetchWorkspaceRequirements(ctx context.Context) (*schema.Workspace_FoundationRequirements, error) {
moduleRoot, err := cuefrontend.ModuleLoader.FindModuleRoot(".")
if err != nil {
// The user is not inside of a workspace. This is normal.
return nil, 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, err
}
return wsData.Proto().Foundation, nil
}