-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
cli.go
251 lines (202 loc) · 5.25 KB
/
cli.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package main
import (
"errors"
"fmt"
"io"
"net/http"
"os"
"runtime"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/srevinsaju/zap/appimage"
"github.com/srevinsaju/zap/config"
"github.com/srevinsaju/zap/daemon"
"github.com/srevinsaju/zap/internal/helpers"
"github.com/srevinsaju/zap/search"
"github.com/srevinsaju/zap/tui"
"github.com/urfave/cli/v2"
)
func installAppImageCliContextWrapper(context *cli.Context) error {
appName := context.Args().First()
// do not continue if we couldn't find the appname
if appName == "" && !context.Bool("github") {
fmt.Printf("%s is not provided\n", tui.Yellow("appname"))
return nil
}
installAppImageOptionsInstance, err := installAppImageOptionsFromCLIContext(context)
if err != nil && err.Error() == "github-from-flag-missing" {
return nil
} else if err != nil {
logger.Fatal(err)
}
zapConfigPath := config.GetPath()
zapConfig, err := config.NewZapConfig(zapConfigPath)
if err != nil {
return err
}
err = appimage.Install(installAppImageOptionsInstance, *zapConfig)
return err
}
func updateAppImageCliContextWrapper(context *cli.Context) error {
appName := context.Args().First()
if appName == "" {
fmt.Printf("%s missing", tui.Green("appname"))
return nil
}
updateAppImageOptionsInstance, err := updateAppImageOptionsFromCLIContext(context)
if err != nil {
logger.Fatal(err)
}
zapConfigPath := config.GetPath()
zapConfig, err := config.NewZapConfig(zapConfigPath)
if err != nil {
return err
}
err = appimage.Update(updateAppImageOptionsInstance, *zapConfig)
return err
}
func removeAppImageCliContextWrapper(context *cli.Context) error {
appName := context.Args().First()
if appName == "" {
fmt.Printf("%s missing", tui.Green(appName))
return nil
}
removeAppImageOptionsInstance, err := removeAppImageOptionsFromCLIContext(context)
if err != nil {
logger.Fatal(err)
}
zapConfigPath := config.GetPath()
zapConfig, err := config.NewZapConfig(zapConfigPath)
if err != nil {
return err
}
err = appimage.Remove(removeAppImageOptionsInstance, *zapConfig)
return err
}
func listAppImageCliContextWrapper(context *cli.Context) error {
formatter := "- %s\n"
if context.Bool("no-color") {
formatter = "%s\n"
}
zapConfigPath := config.GetPath()
zapConfig, err := config.NewZapConfig(zapConfigPath)
if err != nil {
return err
}
apps, err := appimage.List(*zapConfig, context.Bool("index"))
if err != nil {
return err
}
for appIdx := range apps {
if context.Bool("no-color") {
fmt.Printf(formatter, apps[appIdx])
continue
}
fmt.Printf(formatter, tui.Yellow(apps[appIdx]))
}
return err
}
func searchAppImagesCliContextWrapper(c *cli.Context) error {
zapConfigPath := config.GetPath()
zapConfig, err := config.NewZapConfig(zapConfigPath)
if err != nil {
return err
}
mirror := zapConfig.MirrorRoot
err = search.WithCli(mirror)
return err
}
func upgradeAppImageCliContextWrapper(_ *cli.Context) error {
zapConfigPath := config.GetPath()
zapConfig, err := config.NewZapConfig(zapConfigPath)
if err != nil {
return err
}
_, err = appimage.Upgrade(*zapConfig, false)
if err != nil {
return err
}
return nil
}
func configCliContextWrapper(_ *cli.Context) error {
zapConfigPath := config.GetPath()
_, err := config.NewZapConfigInteractive(zapConfigPath)
if err != nil {
return err
}
return nil
}
func daemonCliContextWrapper(context *cli.Context) error {
if context.Bool("install") {
err := daemon.SetupToRunThroughSystemd()
return err
}
zapConfigPath := config.GetPath()
zapConfig, err := config.NewZapConfig(zapConfigPath)
if err != nil {
return err
}
daemon.Sync(func() ([]string, error) {
return appimage.Upgrade(*zapConfig, true)
})
return err
}
func selfUpdateCliContextWrapper(c *cli.Context) error {
if BuildSource != "github" {
return nil
}
ex, err := os.Executable()
if err != nil {
return err
}
updateCheckUrl := fmt.Sprintf("%s/zap-release-metadata", DefaultUpdateUrlPrefix)
r, err := http.Get(updateCheckUrl)
if err != nil {
fmt.Println("Failed to check for updates.")
return err
}
newVersionBytes, err := io.ReadAll(r.Body)
if err != nil {
panic(err)
}
if (strings.TrimRight(string(newVersionBytes), "\n")) != BuildVersion {
fmt.Printf("Updates found %s -> %s\n", tui.Yellow(BuildVersion), tui.Green(string(newVersionBytes)))
} else {
fmt.Println(tui.Green("You are already up-to-date!"))
return nil
}
arch := runtime.GOARCH
updateUrl := fmt.Sprintf("%s/zap-%s", DefaultUpdateUrlPrefix, arch)
if !c.Bool("silent") {
// let the user know what is going to happen next
fmt.Println("Downloading latest version of zap")
confirmDownload := false
confirmDownloadPrompt := &survey.Confirm{
Message: "Proceed?",
}
err = survey.AskOne(confirmDownloadPrompt, &confirmDownload)
if err != nil {
return err
} else if !confirmDownload {
return errors.New("aborting on user request")
}
}
tempDestination := fmt.Sprintf("%s.tmp", ex)
err = os.Remove(tempDestination)
if helpers.CheckIfFileExists(tempDestination) {
return err
}
err = tui.DownloadFileWithProgressBar(updateUrl, tempDestination, "zap")
if err != nil {
return err
}
err = os.Remove(ex)
if err != nil {
return err
}
err = os.Rename(tempDestination, ex)
if err != nil {
return err
}
return nil
}