This repository has been archived by the owner on Jul 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.go
149 lines (127 loc) · 3.31 KB
/
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"archive/zip"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"os/user"
"path/filepath"
"runtime"
"github.com/mitchellh/cli"
"github.com/sudomateo/hashicorp-releases/pkg/hcrelease"
)
// installCommandFactory is the factory that produces the install command.
func installCommandFactory() (cli.Command, error) {
var i installCommand
return &i, nil
}
// installCommand is a blank struct that satisfies the cli.Command interface.
type installCommand struct{}
// Help prints help text for the install command.
func (i *installCommand) Help() string {
help := `Usage: hashicorp-releases install <product> <version>`
return help
}
// Run runs the install command.
func (i *installCommand) Run(args []string) int {
if len(args) < 2 {
fmt.Println("The install command expects exactly two arguments.")
fmt.Printf("%s\n", i.Help())
return 1
}
product := args[0]
version := args[1]
user, err := user.Current()
if err != nil {
fmt.Printf("failed to retrieve home directory: %v", err)
return 1
}
homeDir := user.HomeDir
dataDir := filepath.Join(homeDir, ".local/share/hashicorp-releases")
if err := os.MkdirAll(dataDir, 0755); err != nil {
fmt.Printf("failed to create data directory: %v", err)
return 1
}
productURL, err := url.Parse(hcrelease.ReleasesURL)
if err != nil {
return 1
}
productURL.Path = "index.json"
products, err := hcrelease.GetProducts(productURL.String())
if err != nil {
fmt.Printf("failed to retrieve product details: %v", err)
return 1
}
release, err := products.GetRelease(product)
if err != nil {
fmt.Printf("failed to retrieve release details: %v", err)
return 1
}
ver, err := release.GetVersion(version)
if err != nil {
fmt.Printf("failed to retrieve version details: %v", err)
return 1
}
build, err := ver.GetBuild(runtime.GOOS, runtime.GOARCH)
if err != nil {
fmt.Printf("failed to retrieve build details: %v", err)
return 1
}
tmpfile, err := ioutil.TempFile(dataDir, "terraform")
if err != nil {
fmt.Printf("failed to create temporary directory: %v", err)
return 1
}
defer os.Remove(tmpfile.Name())
err = build.Download(tmpfile)
if err != nil {
fmt.Printf("failed to download build: %v", err)
return 1
}
ext := filepath.Ext(build.Filename)
if ext != ".zip" {
fmt.Printf("invalid file extenstion %s: %v", ext, err)
return 1
}
zipReader, err := zip.OpenReader(tmpfile.Name())
if err != nil {
fmt.Printf("failed to open zip reader: %v", err)
return 1
}
defer zipReader.Close()
for _, f := range zipReader.File {
if f.Name == build.Name {
rc, err := f.Open()
if err != nil {
fmt.Printf("failed to open file: %v", err)
return 1
}
defer rc.Close()
fileName := fmt.Sprintf("%s_%s", build.Name, build.Version)
outPath := filepath.Join(dataDir, fileName)
outFile, err := os.Create(outPath)
if err != nil {
fmt.Printf("failed to create file: %v", err)
return 1
}
if err := outFile.Chmod(0755); err != nil {
fmt.Printf("failed to chmod file: %v", err)
return 1
}
defer outFile.Close()
_, err = io.Copy(outFile, rc)
if err != nil {
fmt.Printf("failed to copy file: %v", err)
return 1
}
break
}
}
return 0
}
// Synopsis prints a one-liner about the install command.
func (i *installCommand) Synopsis() string {
return "Install a specific version of a product."
}