-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
148 lines (118 loc) · 3.65 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
package main
/*
* Version 0.3.0
* Compatible with Mac OS X ONLY
*/
/*** OPERATION WORKFLOW ***/
/*
* 1- Make GET call to receive release json from github
* 2- Download released app from archive
* 3- Rename the file from `hubapp` to `hubapp_version`
* 4- Read the existing symlink for app (Check if it's a homebrew symlink)
* 6- Remove that symlink (Check if it's a homebrew symlink)
* 7- Create new symlink to binary `github app`
*/
//TODO
//rename application
import (
"fmt"
"os"
"regexp"
"strings"
"github.com/manifoldco/promptui"
log "github.com/sirupsen/logrus"
"github.com/warrensbox/hubapp/lib"
"github.com/warrensbox/hubapp/modal"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
const (
APIURL = "https://api.github.com/repos/%s/releases"
)
var version = "0.1.0\n"
var CLIENT_ID = "xxx"
var CLIENT_SECRET = "xxx"
var (
debugFlag *bool
versionFlag *bool
helpFlag *bool
action *string
giturl *string
//log *simplelogger.Logger
)
func init() {
const (
cmdDesc = "Install github app binaries on your local machine. Ex: hubapp install mmmorris1975/aws-runas"
versionFlagDesc = "Displays the version of hubapp"
actionArgDesc = "Provide action needed. Ex: install, upgrade, or uninstall"
giturlArgDesc = "Provide giturl in user/repo format. Ex: mmmorris1975/aws-runas"
debugFlagDesc = "Provide debug output"
)
debugFlag = kingpin.Flag("debug", debugFlagDesc).Short('d').Bool()
versionFlag = kingpin.Flag("version", versionFlagDesc).Short('v').Bool()
action = kingpin.Arg("action", actionArgDesc).String()
giturl = kingpin.Arg("user/repo", giturlArgDesc).String()
log.SetLevel(log.WarnLevel)
}
func main() {
var client modal.Client
client.ClientID = CLIENT_ID
client.ClientSecret = CLIENT_SECRET
kingpin.CommandLine.Interspersed(false)
kingpin.Parse()
if *versionFlag {
fmt.Printf("Version : %s\n", version)
}
if *debugFlag {
log.SetLevel(log.DebugLevel)
}
semverRegex := regexp.MustCompile(`^[a-zA-Z\d-_]*\/[a-zA-Z\d-_]*$`)
if semverRegex.MatchString(*giturl) == false && *versionFlag == false {
log.Info("Invalid repo format. Must be user/repo. Ex: hubapp install warrensbox/aws-find ")
os.Exit(1)
}
apiURL := fmt.Sprintf(APIURL, *giturl)
switch *action {
case "install":
log.Debug("Action -> install")
ghlist, assets := lib.GetAppList(apiURL, &client)
recentVersions, _ := lib.GetRecentVersions()
ghlist = append(recentVersions, ghlist...)
ghlist = lib.RemoveDuplicateVersions(ghlist) //remove duplicate version
/* prompt user to select version of github app */
prompt := promptui.Select{
Label: "Select app version",
Items: ghlist,
}
_, ghversion, errPrompt := prompt.Run()
if errPrompt != nil {
log.Infof("Prompt failed %v\n", errPrompt)
os.Exit(1)
}
installLocation := lib.Install(*giturl, ghversion, assets)
lib.AddRecent(ghversion, installLocation)
case "upgrade":
log.Debug("Action -> upgrade")
latestVersion, assets, err := lib.GetAppLatestVersion(apiURL, &client)
if err != nil {
log.Error("Could not get the latest version. Try `hubapp install user/repo`")
os.Exit(1)
}
installLocation := lib.Install(*giturl, latestVersion, assets)
lib.AddRecent(latestVersion, installLocation)
case "uninstall":
log.Debug("Action -> uninstall")
installLocation := lib.Uninstall(*giturl)
errContent := lib.RemoveContents(installLocation)
if errContent != nil {
log.Debug("Unable to remove files. Files might not have existed.")
os.Exit(0)
}
slice := strings.Split(*giturl, "/")
app := slice[1]
log.Infof("Uninstalled %s\n", app)
default:
if *versionFlag == false {
fmt.Println("Unknown action. See help. Ex: hubapp --help")
}
}
}