This repository has been archived by the owner on Nov 6, 2022. It is now read-only.
forked from hashicorp/hc-install
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
138 lines (116 loc) · 2.73 KB
/
main.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"context"
"fmt"
"io"
"log"
"os"
"path/filepath"
"github.com/hashicorp/go-version"
"github.com/hashicorp/logutils"
"github.com/mitchellh/cli"
hci "gophers.dev/cmds/hc-install"
"gophers.dev/cmds/hc-install/product"
"gophers.dev/cmds/hc-install/releases"
"gophers.dev/cmds/hc-install/src"
"gophers.dev/pkgs/extractors/env"
"gophers.dev/pkgs/ignore"
)
func main() {
filter := &logutils.LevelFilter{
Levels: []logutils.LogLevel{"DEBUG", "WARN", "ERROR"},
MinLevel: logutils.LogLevel("WARN"),
Writer: os.Stderr,
}
log.SetOutput(filter)
ui := &cli.ColoredUi{
ErrorColor: cli.UiColorRed,
WarnColor: cli.UiColorYellow,
Ui: &cli.BasicUi{
Reader: os.Stdin,
Writer: os.Stdout,
ErrorWriter: os.Stderr,
},
}
exitStatus := run(ui, os.Args[1:])
os.Exit(exitStatus)
}
func run(ui cli.Ui, args []string) int {
if len(args) != 2 {
ui.Error("usage: hc-install PRODUCT VERSION")
return 1
}
if err := install(ui, args[0], args[1]); err != nil {
msg := fmt.Sprintf("failed to install %s@%s: %v", args[0], args[1], err)
ui.Error(msg)
return 1
}
return 0
}
func install(ui cli.Ui, project, tag string) error {
msg := fmt.Sprintf("hc-install: will install %s@%s", project, tag)
ui.Info(msg)
v := version.Must(version.NewVersion(tag))
i := hci.NewInstaller()
var source src.Source
switch project {
case "consul":
source = &releases.ExactVersion{
Product: product.Consul,
Version: v,
}
case "vault":
source = &releases.ExactVersion{
Product: product.Vault,
Version: v,
}
default:
return fmt.Errorf("project %s cannot be downloaded", project)
}
ctx := context.Background()
executable, instErr := i.Ensure(ctx, []src.Source{source})
if instErr != nil {
return instErr
}
if err := copyProgram(ui, executable); err != nil {
return err
}
return nil
}
func copyProgram(ui cli.Ui, programPath string) error {
var (
gobin string
gopath string
program = filepath.Base(programPath)
)
if err := env.ParseOS(env.Schema{
"GOBIN": env.String(&gobin, false),
"GOPATH": env.String(&gopath, false),
}); err != nil {
return err
}
var destination string
switch {
case gobin != "":
destination = filepath.Join(gobin, program)
case gopath != "":
destination = filepath.Join(gopath, "bin", program)
}
msg := fmt.Sprintf("hc-install: copy executable to %s", destination)
ui.Info(msg)
return clone(programPath, destination)
}
func clone(source, destination string) error {
sFile, err := os.Open(source)
if err != nil {
return err
}
defer ignore.Close(sFile)
dFile, err := os.OpenFile(destination, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755)
if err != nil {
return err
}
defer ignore.Close(dFile)
_, err = io.Copy(dFile, sFile)
return err
}