-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
174 lines (139 loc) · 4.2 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
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"github.com/alexflint/go-arg"
"github.com/eduhds/gspm/internal/gitservice"
"github.com/eduhds/gspm/internal/tui"
"github.com/eduhds/gspm/internal/util"
)
const appname = "gspm"
const version = "0.2.1"
const description = "Thanks for using gspm, the Git Services Package Manager.\n"
const asciiArt = "\n ,adPPYb,d8 ,adPPYba, 8b,dPPYba, 88,dPYba,,adPYba, \n" +
"a8\" `Y88 I8[ \"\" 88P' \"8a 88P' \"88\" \"8a \n" +
"8b 88 `\"Y8ba, 88 d8 88 88 88 \n" +
"\"8a, ,d88 aa ]8I 88b, ,a8\" 88 88 88 \n" +
" `\"YbbdP\"Y8 `\"YbbdP\"' 88`YbbdP\"' 88 88 88 \n" +
" aa, ,88 88 \n" +
" \"Y8bbdP\" 88 \n"
var customConfigDir string = ""
var customShellCommand string = ""
var downloadPrefix = util.GetDownloadsDir()
func (args) Version() string {
return fmt.Sprintf("%s v%s", appname, version)
}
func (args) Description() string {
return description
}
func (args) Epilogue() string {
return "For more information visit https://github.com/eduhds/gspm"
}
func main() {
var args args
arg.MustParse(&args)
if args.GitHubToken != "" {
gitservice.GHToken = args.GitHubToken
}
if args.ConfigDir != "" {
customConfigDir = args.ConfigDir
tui.ShowInfo("Using custom config directory: " + customConfigDir)
}
if args.ShellCommand != "" {
customShellCommand = strings.TrimSpace(args.ShellCommand)
if len(strings.Split(customShellCommand, " ")) != 2 {
tui.ShowError("Invalid shell command: " + customShellCommand)
os.Exit(1)
}
tui.ShowInfo("Using custom shell command: " + customShellCommand)
}
if args.Command == "" {
// Interactive mode
tui.TextInfo(asciiArt)
tui.ShowInfo(fmt.Sprintf("v%s", version))
tui.ShowLine()
quit := false
for !quit {
option := tui.ShowOptions("What command do you want to use?", []string{"add", "remove", "update", "install", "edit", "info", "list", "<cancel>"})
if option == "<cancel>" {
quit = true
} else {
repo := ""
if option != "list" && option != "install" {
repo = tui.ShowTextInput(fmt.Sprintf("What repository do you want \"%s\"? (Format: username/repository)", option), false, "")
}
program := strings.TrimSpace(strings.Join(os.Args, " "))
cmd := exec.Command(program, option, repo)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
tui.ShowError(err.Error())
}
tui.ShowLine()
quit = !tui.ShowConfirm("Continue to another command?")
}
if quit {
tui.TextSuccess(description)
tui.ShowLine()
}
}
return
}
// Non-interactive mode
tui.ShowInfo(fmt.Sprintf("%s v%s", appname, version))
tui.ShowLine()
config := ResolveConfig()
switch args.Command {
case "list":
CommandList(config)
case "install":
if len(args.Repos) > 0 || len(args.Scripts) > 0 {
tui.ShowWarning("Ignoring args for install command.")
}
CommandInstall(config)
case "add", "update", "remove", "edit", "info":
if len(args.Repos) == 0 {
tui.ShowError("No packages provided")
return
}
mustExist := args.Command != "add"
for index, value := range args.Repos {
if index > 0 {
tui.ShowLine()
}
gsp, err := ResolvePackage(value, config, mustExist)
if err != nil {
tui.ShowError(err.Error())
continue
}
withScript := len(args.Scripts) > 0 && args.Scripts[index] != ""
supportScript := args.Command == "add" || args.Command == "update" || args.Command == "edit" || args.Command == "remove"
if supportScript {
if withScript {
gsp.Script = args.Scripts[index]
}
}
if args.Command == "add" {
config = CommandAdd(config, gsp)
} else if args.Command == "update" {
config = CommandUpdate(config, gsp)
} else if args.Command == "remove" {
config = CommandRemove(config, gsp, withScript)
} else if args.Command == "edit" {
config = CommandEdit(config, gsp, withScript)
} else if args.Command == "info" {
CommandInfo(gsp)
}
}
if args.Command != "info" {
WriteConfig(config)
}
default:
tui.ShowError("Unknown command: " + args.Command)
os.Exit(1)
}
}