-
Notifications
You must be signed in to change notification settings - Fork 19
/
cmd_install.go
120 lines (100 loc) · 2.89 KB
/
cmd_install.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"context"
"flag"
"fmt"
"os"
"runtime"
"strings"
"github.com/hashicorp/go-version"
"github.com/mitchellh/cli"
hci "github.com/hashicorp/hc-install"
"github.com/hashicorp/hc-install/product"
"github.com/hashicorp/hc-install/releases"
"github.com/hashicorp/hc-install/src"
)
type InstallCommand struct {
Ui cli.Ui
}
func (c *InstallCommand) Name() string { return "install" }
func (c *InstallCommand) Synopsis() string {
return "Install a HashiCorp product"
}
func (c *InstallCommand) Help() string {
helpText := `
Usage: hc-install install [options] -version <version> <product>
This command installs a HashiCorp product.
Options:
-version [REQUIRED] Version of product to install.
-path Path to directory where the product will be installed. Defaults
to current working directory.
`
return strings.TrimSpace(helpText)
}
func (c *InstallCommand) Run(args []string) int {
var (
version string
installDirPath string
)
fs := flag.NewFlagSet("install", flag.ExitOnError)
fs.Usage = func() { c.Ui.Output(c.Help()) }
fs.StringVar(&version, "version", "", "version of product to install")
fs.StringVar(&installDirPath, "path", "", "path to directory where production will be installed")
if err := fs.Parse(args); err != nil {
return 1
}
// golang's arg parser is Posix-compliant but doesn't match the
// common GNU flag parsing argument, so force an error rather than
// silently dropping the options
args = fs.Args()
if len(args) != 1 {
c.Ui.Error(`This command requires one positional argument: <product>
Option flags must be provided before the positional argument`)
return 1
}
product := fs.Args()[0]
if version == "" {
c.Ui.Error("-version flag is required")
return 1
}
if installDirPath == "" {
cwd, err := os.Getwd()
if err != nil {
c.Ui.Error(fmt.Sprintf("Could not get current working directory for default installation path: %v", err))
return 1
}
installDirPath = cwd
}
installedPath, err := c.install(product, version, installDirPath)
if err != nil {
msg := fmt.Sprintf("failed to install %s@%s: %v", product, version, err)
c.Ui.Error(msg)
return 1
}
c.Ui.Info(fmt.Sprintf("installed %s@%s to %s", product, version, installedPath))
return 0
}
func (c *InstallCommand) install(project, tag, installDirPath string) (string, error) {
msg := fmt.Sprintf("hc-install: will install %s@%s", project, tag)
c.Ui.Info(msg)
v, err := version.NewVersion(tag)
if err != nil {
return "", fmt.Errorf("invalid version: %w", err)
}
i := hci.NewInstaller()
source := &releases.ExactVersion{
Product: product.Product{
Name: project,
BinaryName: func() string {
if runtime.GOOS == "windows" {
return fmt.Sprintf("%s.exe", project)
}
return project
},
},
Version: v,
InstallDir: installDirPath,
}
ctx := context.Background()
return i.Install(ctx, []src.Installable{source})
}