-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
upgrade.go
92 lines (75 loc) · 3.08 KB
/
upgrade.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
package keygen
import "context"
type UpgradeOptions struct {
// CurrentVersion is the current version of the program. This will be used by
// Keygen to determine if an upgrade is available.
CurrentVersion string
// Product is the product ID to scope the upgrade to. This defaults to keygen.Product,
// but overriding it may be useful if you're requesting an upgrade for another
// accessible product, e.g. a product with an OPEN distribution strategy.
Product string
// Package is the package ID to scope the upgrade to. This defaults to keygen.Package,
// but overriding it may be useful if you're requesting an upgrade for another
// accessible package of the product.
Package string
// Constraint is a version constraint to use when checking for upgrades. For
// example, to pin upgrades to v1, you would pass a "1.0" constraint.
Constraint string
// Channel is the release channel. One of: stable, rc, beta, alpha or dev.
Channel string
// PublicKey is your personal Ed25519ph public key, generated using Keygen's CLI
// or using ssh-keygen. This will be used to verify the release's signature
// before install. This MUST NOT be your Keygen account's public key.
PublicKey string
// Filename is the template string used when retrieving an artifact during
// install. This should compile to a valid artifact identifier, e.g. a
// filename for the current platform and arch.
//
// The default template is below:
//
// {{.program}}_{{.platform}}_{{.arch}}{{if .ext}}.{{.ext}}{{end}}
//
// Available template variables:
//
// program // the name of the currently running program (i.e. basename of os.Args[0])
// ext // the extension based on current platform (i.e. exe on Windows)
// platform // the current platform (i.e. GOOS)
// arch // the current architecture (i.e. GOARCH)
// channel // the release channel (e.g. stable)
// version // the release version (e.g. 1.0.0-beta.3)
//
// If more control is needed, provide a string.
Filename string
}
// Upgrade checks if an upgrade is available for the provided version. Returns a
// Release and any errors that occurred, e.g. ErrUpgradeNotAvailable.
func Upgrade(ctx context.Context, options UpgradeOptions) (*Release, error) {
if options.PublicKey == PublicKey {
panic("You MUST use a personal public key. This MUST NOT be your Keygen account's public key.")
}
if options.Filename == "" {
options.Filename = `{{.program}}_{{.platform}}_{{.arch}}{{if .ext}}.{{.ext}}{{end}}`
}
if options.Product == "" {
options.Product = Product
}
if options.Package == "" {
options.Package = Package
}
if options.Channel == "" {
options.Channel = "stable"
}
client := NewClient()
params := querystring{Product: options.Product, Package: options.Package, Constraint: options.Constraint, Channel: options.Channel}
release := &Release{}
if _, err := client.Get(ctx, "releases/"+options.CurrentVersion+"/upgrade", params, release); err != nil {
switch err.(type) {
case *NotFoundError:
return nil, ErrUpgradeNotAvailable
default:
return nil, err
}
}
release.opts = options
return release, nil
}