-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
212 lines (188 loc) · 4.85 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
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
package main
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"os/signal"
"syscall"
"time"
"github.com/ryanfowler/fetch/internal/cli"
"github.com/ryanfowler/fetch/internal/config"
"github.com/ryanfowler/fetch/internal/core"
"github.com/ryanfowler/fetch/internal/fetch"
"github.com/ryanfowler/fetch/internal/format"
"github.com/ryanfowler/fetch/internal/multipart"
"github.com/ryanfowler/fetch/internal/update"
)
func main() {
// Cancel the context when one of the below signals are caught.
ctx, cancel := context.WithCancelCause(context.Background())
chSig := make(chan os.Signal, 1)
signal.Notify(chSig, syscall.SIGINT, syscall.SIGHUP, syscall.SIGTERM)
go func() {
sig := <-chSig
cancel(core.SignalError(sig.String()))
}()
// Parse the CLI args.
app, err := cli.Parse(os.Args[1:])
if err != nil {
p := core.NewHandle(app.Cfg.Color).Stderr()
writeCLIErr(p, err)
os.Exit(1)
}
// Parse any config file, and merge with it.
err = parseConfigFile(app)
if err != nil {
p := core.NewHandle(app.Cfg.Color).Stderr()
core.WriteErrorMsg(p, err)
os.Exit(1)
}
handle := core.NewHandle(app.Cfg.Color)
verbosity := getVerbosity(app)
// Start async update, if necessary.
if !app.Update && app.Cfg.AutoUpdate != nil && *app.Cfg.AutoUpdate >= 0 {
checkForUpdate(ctx, handle.Stderr(), *app.Cfg.AutoUpdate)
}
// Print help to stdout.
if app.Help {
p := handle.Stdout()
app.PrintHelp(p)
p.Flush()
os.Exit(0)
}
// Print version to stdout.
if app.Version {
fmt.Fprintln(os.Stdout, "fetch", core.Version)
os.Exit(0)
}
// Print build info to stdout.
if app.BuildInfo {
p := handle.Stdout()
info := core.GetBuildInfo()
if app.Cfg.Format != core.FormatOff {
format.FormatJSON(info, p)
} else {
p.Write(info)
}
p.Flush()
os.Exit(0)
}
// Attempt to update the current executable.
if app.Update {
p := handle.Stderr()
timeout := getValue(app.Cfg.Timeout)
status := update.Update(ctx, p, timeout, verbosity == core.VSilent)
os.Exit(status)
}
// Otherwise, a URL must be provided.
if app.URL == nil {
p := handle.Stderr()
writeCLIErr(p, errors.New("<URL> must be provided"))
os.Exit(1)
}
// Make the HTTP request using the parsed configuration.
req := fetch.Request{
AWSSigv4: app.AWSSigv4,
Basic: app.Basic,
Bearer: app.Bearer,
Data: app.Data,
DNSServer: app.Cfg.DNSServer,
DryRun: app.DryRun,
Edit: app.Edit,
Form: app.Form,
Format: app.Cfg.Format,
Headers: app.Cfg.Headers,
HTTP: app.Cfg.HTTP,
IgnoreStatus: getValue(app.Cfg.IgnoreStatus),
Image: app.Cfg.Image,
Insecure: getValue(app.Cfg.Insecure),
JSON: app.JSON,
Method: app.Method,
Multipart: multipart.NewMultipart(app.Multipart),
NoEncode: getValue(app.Cfg.NoEncode),
NoPager: getValue(app.Cfg.NoPager),
Output: app.Output,
PrinterHandle: handle,
Proxy: app.Cfg.Proxy,
QueryParams: app.Cfg.QueryParams,
Range: app.Range,
Redirects: app.Cfg.Redirects,
Timeout: getValue(app.Cfg.Timeout),
TLS: getValue(app.Cfg.TLS),
URL: app.URL,
Verbosity: verbosity,
XML: app.XML,
}
status := fetch.Fetch(ctx, &req)
os.Exit(status)
}
// parse and merge any config file with the CLI app configuration.
func parseConfigFile(app *cli.App) error {
file, err := config.GetFile(app.ConfigPath)
if err != nil {
return err
}
if file == nil {
return nil
}
if app.URL != nil {
hostCfg, ok := file.Hosts[app.URL.Hostname()]
if ok {
app.Cfg.Merge(hostCfg)
}
}
app.Cfg.Merge(file.Global)
return nil
}
func getValue[T any](v *T) T {
if v == nil {
var t T
return t
}
return *v
}
// getVerbosity returns the Verbosity level based on the app configuration.
func getVerbosity(app *cli.App) core.Verbosity {
if getValue(app.Cfg.Silent) {
return core.VSilent
}
switch getValue(app.Cfg.Verbosity) {
case 0:
return core.VNormal
case 1:
return core.VVerbose
default:
return core.VExtraVerbose
}
}
func checkForUpdate(ctx context.Context, p *core.Printer, dur time.Duration) {
// Check the metadata file to see if we should start an async update.
ok, err := update.ShouldAttemptUpdate(ctx, p, dur)
if err != nil {
msg := fmt.Sprintf("unable to check if update is needed: %s", err.Error())
core.WriteWarningMsg(p, msg)
return
}
if !ok {
return
}
// Asynchronously start an update process.
// Should we output a log here?
path, err := os.Executable()
if err != nil {
return
}
_ = exec.Command(path, "--update", "--timeout=300").Start()
}
// writeCLIErr writes the provided CLI error to the Printer.
func writeCLIErr(p *core.Printer, err error) {
core.WriteErrorMsgNoFlush(p, err)
p.WriteString("\nFor more information, try '")
p.Set(core.Bold)
p.WriteString("--help")
p.Reset()
p.WriteString("'.\n")
p.Flush()
}