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 pathuse.go
186 lines (160 loc) · 4.09 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"archive/zip"
"fmt"
"io"
"io/ioutil"
"log"
"net/url"
"os"
"os/user"
"path/filepath"
"runtime"
"github.com/mitchellh/cli"
"github.com/sudomateo/hashicorp-releases/pkg/hcrelease"
)
// fileExists checks whether or not a file exists.
func fileExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// useCommandFactory is the factory that produces the use command.
func useCommandFactory() (cli.Command, error) {
var i useCommand
return &i, nil
}
// useCommand is a blank struct that satisfies the cli.Command interface.
type useCommand struct{}
// Help prints help text for the use command.
func (u *useCommand) Help() string {
help := `Usage: hashicorp-releases use <product> <version>`
return help
}
// Run runs the use command.
func (u *useCommand) Run(args []string) int {
if len(args) < 2 {
fmt.Println("The use command expects exactly two arguments.")
fmt.Printf("%s\n", u.Help())
return 1
}
product := args[0]
version := args[1]
user, err := user.Current()
if err != nil {
log.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 {
log.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 {
log.Printf("failed to retrieve product details: %v", err)
return 1
}
release, err := products.GetRelease(product)
if err != nil {
log.Printf("failed to retrieve release details: %v", err)
return 1
}
ver, err := release.GetVersion(version)
if err != nil {
log.Printf("failed to retrieve version details: %v", err)
return 1
}
build, err := ver.GetBuild(runtime.GOOS, runtime.GOARCH)
if err != nil {
log.Printf("failed to retrieve build details: %v", err)
return 1
}
tmpfile, err := ioutil.TempFile(dataDir, "terraform")
if err != nil {
log.Printf("failed to create temporary directory: %v", err)
return 1
}
defer os.Remove(tmpfile.Name())
err = build.Download(tmpfile)
if err != nil {
log.Printf("failed to download build: %v", err)
return 1
}
ext := filepath.Ext(build.Filename)
if ext != ".zip" {
log.Printf("invalid file extenstion %s: %v", ext, err)
return 1
}
zipReader, err := zip.OpenReader(tmpfile.Name())
if err != nil {
log.Printf("failed to open zip reader: %v", err)
return 1
}
defer zipReader.Close()
fileName := fmt.Sprintf("%s_%s", build.Name, build.Version)
outPath := filepath.Join(dataDir, fileName)
for _, f := range zipReader.File {
if f.Name == build.Name {
rc, err := f.Open()
if err != nil {
log.Printf("failed to open file: %v", err)
return 1
}
defer rc.Close()
outFile, err := os.Create(outPath)
if err != nil {
log.Printf("failed to create file: %v", err)
return 1
}
if err := outFile.Chmod(0755); err != nil {
log.Printf("failed to chmod file: %v", err)
return 1
}
defer outFile.Close()
_, err = io.Copy(outFile, rc)
if err != nil {
log.Printf("failed to copy file: %v", err)
return 1
}
break
}
}
symlinkDir := filepath.Join(homeDir, ".local/bin")
if err := os.MkdirAll(symlinkDir, 0755); err != nil {
log.Printf("failed to create symlink directory: %v", err)
return 1
}
symlinkPath := filepath.Join(symlinkDir, build.Name)
exists, err := fileExists(symlinkPath)
if err != nil {
log.Printf("failed to check for file existence: %v", err)
return 1
}
if exists {
if err := os.Remove(symlinkPath); err != nil {
log.Printf("failed to remove file: %v", err)
return 1
}
}
if err := os.Symlink(outPath, symlinkPath); err != nil {
log.Printf("failed to create symlink: %v", err)
return 1
}
return 0
}
// Synopsis prints a one-liner about the use command.
func (u *useCommand) Synopsis() string {
return "Use a specific version of a product."
}