-
Notifications
You must be signed in to change notification settings - Fork 24
/
use.go
75 lines (63 loc) · 1.88 KB
/
use.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
package main
import (
"fmt"
"path/filepath"
kingpin "gopkg.in/alecthomas/kingpin.v2"
"github.com/andrewkroh/gvm"
"github.com/andrewkroh/gvm/cmd/gvm/internal/shellfmt"
)
type useCmd struct {
version string // Go version.
build bool // Build from source only.
noInstall bool // If the version is not found locally then don't install it.
format string // Shell command format to output.
}
func useCommand(cmd *kingpin.CmdClause) func(*gvm.Manager) error {
ctx := &useCmd{}
cmd.Arg("version", "Go version to install (e.g. 1.21.0).").StringVar(&ctx.version)
cmd.Flag("build", "Build go version from source").Short('b').BoolVar(&ctx.build)
cmd.Flag("no-install", "Don't install if missing").Short('n').BoolVar(&ctx.noInstall)
cmd.Flag("format", "Format to use for the shell commands. Options: bash, batch, powershell").
Short('f').
Default(shellfmt.DefaultFormat()).
EnumVar(&ctx.format, shellfmt.BashFormat, shellfmt.BatchFormat, shellfmt.PowershellFormat)
return ctx.Run
}
func (cmd *useCmd) Run(manager *gvm.Manager) error {
if cmd.version == "" {
return fmt.Errorf("no version specified")
}
ver, err := gvm.ParseVersion(cmd.version)
if err != nil {
return err
}
log.Debugf("Using Go version %v", ver)
shellFmt, err := shellfmt.New(cmd.format)
if err != nil {
return err
}
var goroot string
if cmd.build {
goroot, err = manager.Build(ver)
} else if cmd.noInstall {
has, err := manager.HasVersion(ver)
if err != nil {
return err
}
if !has {
return fmt.Errorf("version %s not installed and --no-install enabled", ver)
}
goroot = manager.VersionGoROOT(ver)
} else {
goroot, err = manager.Install(ver)
}
if err != nil {
return err
}
shellFmt.Set("GOROOT", goroot)
shellFmt.Prepend("PATH", filepath.Join(goroot, "bin"))
if _, experimental := ver.VendorSupport(); experimental {
shellFmt.Set("GO15VENDOREXPERIMENT", "1")
}
return nil
}