-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (111 loc) · 3.31 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
/*
go-curling - an implementation of curl in golang
Copyright (C) 2022 Christopher Wiegand
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110
*/
package main
import (
"os"
curlcli "github.com/cdwiegand/go-curling/cli"
curl "github.com/cdwiegand/go-curling/context"
curlerrors "github.com/cdwiegand/go-curling/errors"
)
func main() {
ctx := new(curl.CurlContext)
var cerr *curlerrors.CurlError
nonFlagArgs, cerr := curlcli.ParseFlags(os.Args[1:], ctx)
if cerr != nil {
reportError(cerr, ctx)
os.Exit(cerr.ExitCode)
return
}
if ctx.Version {
os.Stdout.WriteString("go-curling build ##DEV##")
os.Exit(0)
return
}
cerr = ctx.SetupContextForRun(nonFlagArgs)
if cerr != nil {
reportError(cerr, ctx)
os.Exit(cerr.ExitCode)
return
}
// must be after version check
if len(ctx.Urls) == 0 {
cerr = curlerrors.NewCurlErrorFromString(curlerrors.ERROR_STATUS_CODE_FAILURE, "no valid URL was not found on the command line, try 'go-curling --help' for usage")
reportError(cerr, ctx)
os.Exit(cerr.ExitCode)
return
}
client, cerr := ctx.BuildClient()
if cerr != nil {
reportError(cerr, ctx)
os.Exit(cerr.ExitCode)
return
}
var lastErrorCode *curlerrors.CurlError
for index := range ctx.Urls {
request, cerr := ctx.BuildHttpRequest(ctx.Urls[index], index, true, true)
if cerr != nil {
lastErrorCode = cerr
if ctx.FailEarly {
reportError(cerr, ctx)
os.Exit(cerr.ExitCode)
}
} else {
resp, cerr := ctx.GetCompleteResponse(index, client, request)
if cerr != nil {
lastErrorCode = cerr
if resp != nil && len(resp.Responses) > 0 && ctx.FailWithBody {
ctx.ProcessResponseToOutputs(index, resp, request)
}
reportError(cerr, ctx)
if cerr.ExitCode != 0 && ctx.FailEarly {
os.Exit(cerr.ExitCode)
}
} else {
cerrs := ctx.ProcessResponseToOutputs(index, resp, request)
if cerrs.HasError() {
forceExitCode := 0
for _, h := range cerrs.Errors {
lastErrorCode = h
reportError(cerr, ctx)
if h.ExitCode != 0 {
forceExitCode = h.ExitCode
}
}
if forceExitCode != 0 && ctx.FailEarly {
os.Exit(forceExitCode)
}
}
}
}
}
if lastErrorCode != nil {
os.Exit(lastErrorCode.ExitCode)
}
}
func reportError(err *curlerrors.CurlError, ctx *curl.CurlContext) string {
if err == nil {
return ""
}
entry := "Error: " + err.ErrorString + "."
if err.ExitCode == curlerrors.ERROR_CANNOT_WRITE_TO_STDOUT {
// don't recurse (it called us to report the failure to write errors to a normal file)
panic(err)
}
if (!ctx.IsSilent && !ctx.SilentFail) || !ctx.ShowErrorEvenIfSilent {
ctx.WriteToFileBytes(ctx.ErrorOutput, []byte(entry))
}
return entry
}