-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_update.go
53 lines (46 loc) · 1.43 KB
/
command_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
package main
import (
"github.com/perrito666/goworkon/actions"
"github.com/perrito666/goworkon/goinstalls"
"github.com/pkg/errors"
)
// Update command allows the user to change the go version of one or multiple
// environments to the latest.
type Update struct {
environmentName string
goVersion string
}
// Usage implements Command.
func (u Update) Usage() string {
return "the expected format is: goworkon [Options] switch <envname>"
}
// Validate implements Command.
func (u Update) Validate() error {
if u.environmentName == "" && u.goVersion == "" {
return errors.New("specify either a go version or an environment name")
}
v, err := goinstalls.VersionFromString(u.goVersion)
if err != nil {
return errors.WithStack(err)
}
// if only version is passed, version should be just a minor, this implies that
// we will update all x.y installs to the latest x.y
if u.environmentName == "" && v.Patch != 0 {
return errors.New("when passing only go version, ommit the patch version (major.minor.patch)")
}
return nil
}
// Run implements Command.
func (u Update) Run() error {
v, err := goinstalls.VersionFromString(u.goVersion)
if err != nil {
return errors.WithStack(err)
}
if u.environmentName == "" {
return errors.WithStack(actions.UpdateAllTo(v))
}
if u.goVersion != "" {
return errors.WithStack(actions.UpdateToVersion(u.environmentName, v))
}
return errors.WithStack(actions.UpdateToLatest(u.environmentName))
}