forked from warrensbox/tgswitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
187 lines (153 loc) · 6.11 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
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
187
package main
/*
* Version 0.3.0
* Compatible with Mac OS X ONLY
*/
/*** OPERATION WORKFLOW ***/
/*
* 1- Create /usr/local/terragrunt directory if does not exist
* 2- Download binary file from url to /usr/local/terragrunt
* 3- Rename the file from `terragrunt` to `terragrunt_version`
* 4- Read the existing symlink for terragrunt (Check if it's a homebrew symlink)
* 6- Remove that symlink (Check if it's a homebrew symlink)
* 7- Create new symlink to binary `terragrunt_version`
*/
import (
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
"strings"
"github.com/manifoldco/promptui"
"github.com/pborman/getopt"
lib "github.com/warrensbox/tgswitch/lib"
"github.com/warrensbox/tgswitch/modal"
)
const (
terragruntURL = "https://api.github.com/repos/gruntwork-io/terragrunt/releases?"
defaultBin = "/usr/local/bin/terragrunt" //default bin installation dir
rcFilename = ".tgswitchrc"
tgvFilename = ".terragrunt-version"
installVersion = "terragrunt_"
)
var version = "0.2.0\n"
var CLIENT_ID = "xxx"
var CLIENT_SECRET = "xxx"
func main() {
var client modal.Client
client.ClientID = CLIENT_ID
client.ClientSecret = CLIENT_SECRET
custBinPath := getopt.StringLong("bin", 'b', defaultBin, "Custom binary path. For example: /Users/username/bin/terragrunt")
versionFlag := getopt.BoolLong("version", 'v', "displays the version of tgswitch")
helpFlag := getopt.BoolLong("help", 'h', "displays help message")
_ = versionFlag
getopt.Parse()
args := getopt.Args()
dir, err := os.Getwd()
if err != nil {
log.Printf("Failed to get current directory %v\n", err)
os.Exit(1)
}
tgvfile := dir + fmt.Sprintf("/%s", tgvFilename) //settings for .terragrunt-version file in current directory (tgenv compatible)
rcfile := dir + fmt.Sprintf("/%s", rcFilename) //settings for .tgswitchrc file in current directory
if *versionFlag {
fmt.Printf("\nVersion: %v\n", version)
} else if *helpFlag {
usageMessage()
} else {
installLocation := lib.GetInstallLocation()
if _, err := os.Stat(rcfile); err == nil && len(args) == 0 { //if there is a .tgswitchrc file, and no commmand line arguments
fmt.Printf("Reading required terragrunt version %s \n", rcFilename)
fileContents, err := ioutil.ReadFile(rcfile)
if err != nil {
fmt.Printf("Failed to read %s file. Follow the README.md instructions for setup. https://github.com/warrensbox/tgswitch/blob/master/README.md\n", rcFilename)
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
tgversion := strings.TrimSuffix(string(fileContents), "\n")
fileExist := lib.CheckFileExist(installLocation + installVersion + tgversion)
if fileExist {
lib.ChangeSymlink(*custBinPath, string(tgversion))
os.Exit(0)
}
_, assets := lib.GetAppList(terragruntURL, &client)
if lib.ValidVersionFormat(tgversion) { //check if version is correct
lib.Install(terragruntURL, string(tgversion), assets, *custBinPath)
} else {
fmt.Println("Invalid terragrunt version format. Format should be #.#.# or #.#.#-@# where # is numbers and @ is word characters. For example, 0.11.7 and 0.11.9-beta1 are valid versions")
os.Exit(1)
}
} else if _, err := os.Stat(tgvfile); err == nil && len(args) == 0 {
fmt.Printf("Reading required terragrunt version %s \n", tgvFilename)
fileContents, err := ioutil.ReadFile(tgvfile)
if err != nil {
fmt.Printf("Failed to read %s file. Follow the README.md instructions for setup. https://github.com/warrensbox/tgswitch/blob/master/README.md\n", tgvFilename)
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
tgversion := strings.TrimSuffix(string(fileContents), "\n")
fileExist := lib.CheckFileExist(installLocation + installVersion + string(tgversion))
if fileExist {
lib.ChangeSymlink(*custBinPath, string(tgversion))
os.Exit(0)
}
_, assets := lib.GetAppList(terragruntURL, &client)
if lib.ValidVersionFormat(tgversion) { //check if version is correct
lib.Install(terragruntURL, string(tgversion), assets, *custBinPath)
} else {
fmt.Println("Invalid terragrunt version format. Format should be #.#.# or #.#.#-@# where # is numbers and @ is word characters. For example, 0.11.7 and 0.11.9-beta1 are valid versions")
os.Exit(1)
}
} else if len(args) == 1 {
semverRegex := regexp.MustCompile(`\A\d+(\.\d+){2}\z`)
if semverRegex.MatchString(args[0]) {
requestedVersion := args[0]
fileExist := lib.CheckFileExist(installLocation + installVersion + string(requestedVersion))
if fileExist {
lib.ChangeSymlink(*custBinPath, string(requestedVersion))
os.Exit(0)
}
//check if version exist before downloading it
tflist, assets := lib.GetAppList(terragruntURL, &client)
exist := lib.VersionExist(requestedVersion, tflist)
if exist {
installLocation := lib.Install(terragruntURL, requestedVersion, assets, *custBinPath)
lib.AddRecent(requestedVersion, installLocation) //add to recent file for faster lookup
} else {
fmt.Println("Not a valid terragrunt version")
}
} else {
fmt.Println("Not a valid terragrunt version")
fmt.Println("Args must be a valid terragrunt version")
usageMessage()
}
} else if len(args) == 0 {
tglist, assets := lib.GetAppList(terragruntURL, &client)
recentVersions, _ := lib.GetRecentVersions() //get recent versions from RECENT file
tglist = append(recentVersions, tglist...) //append recent versions to the top of the list
tglist = lib.RemoveDuplicateVersions(tglist) //remove duplicate version
/* prompt user to select version of terragrunt */
prompt := promptui.Select{
Label: "Select terragrunt version",
Items: tglist,
}
_, tgversion, errPrompt := prompt.Run()
tgversion = strings.Trim(tgversion, " *recent")
if errPrompt != nil {
log.Printf("Prompt failed %v\n", errPrompt)
os.Exit(1)
}
installLocation := lib.Install(terragruntURL, tgversion, assets, *custBinPath)
lib.AddRecent(tgversion, installLocation) //add to recent file for faster lookup
os.Exit(0)
} else {
usageMessage()
}
}
}
func usageMessage() {
fmt.Print("\n\n")
getopt.PrintUsage(os.Stderr)
fmt.Println("Supply the terragrunt version as an argument, or choose from a menu")
}