-
Notifications
You must be signed in to change notification settings - Fork 97
/
main.go
177 lines (163 loc) · 4.23 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
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
"syscall"
"github.com/rs/curlie/args"
"github.com/rs/curlie/formatter"
"golang.org/x/crypto/ssh/terminal"
)
var (
commit = "0000000"
version = "v0.0.0-LOCAL"
date = "0000-00-00T00:00:00Z"
)
func main() {
// handle `curlie version` separately from `curl --version`
if len(os.Args) == 2 && os.Args[1] == "version" {
fmt.Printf("curlie %s (%s)\n", version, date)
os.Exit(0)
return
}
// *nixes use 0, 1, 2
// Windows uses random numbers
stdinFd := int(os.Stdin.Fd())
stdoutFd := int(os.Stdout.Fd())
stderrFd := int(os.Stderr.Fd())
// Setting Console mode on windows to allow color output, By default scheme is DefaultColorScheme
// But in case of any error, it is set to ColorScheme{}.
scheme := formatter.DefaultColorScheme
if err := setupWindowsConsole(stdoutFd); err != nil {
scheme = formatter.ColorScheme{}
}
var stdout io.Writer = os.Stdout
var stderr io.Writer = os.Stderr
var stdin io.Reader = os.Stdin
input := &bytes.Buffer{}
var inputWriter io.Writer = input
opts := args.Parse(os.Args)
verbose := opts.Has("verbose") || opts.Has("v")
quiet := opts.Has("silent") || opts.Has("s")
pretty := opts.Remove("pretty")
opts.Remove("i")
if len(opts) == 0 {
// Show help if no args
opts = append(opts, "-h", "all")
} else {
// Remove progress bar.
opts = append(opts, "-s", "-S")
}
// Change default method based on binary name.
switch os.Args[0] {
case "post", "put", "delete":
if !opts.Has("X") && !opts.Has("request") {
opts = append(opts, "-X", os.Args[0])
}
case "head":
if !opts.Has("I") && !opts.Has("head") {
opts = append(opts, "-I")
}
}
if opts.Has("h") || opts.Has("help") {
stdout = &formatter.HelpAdapter{Out: stdout, CmdName: os.Args[0]}
} else {
isForm := opts.Has("F")
if pretty || terminal.IsTerminal(stdoutFd) {
inputWriter = &formatter.JSON{
Out: inputWriter,
Scheme: scheme,
}
// Format/colorize JSON output if stdout is to the terminal.
stdout = &formatter.JSON{
Out: stdout,
Scheme: scheme,
}
// Filter out binary output.
stdout = &formatter.BinaryFilter{Out: stdout}
}
if pretty || terminal.IsTerminal(stderrFd) {
// If stderr is not redirected, output headers.
if !quiet {
opts = append(opts, "-v")
}
stderr = &formatter.HeaderColorizer{
Out: stderr,
Scheme: scheme,
}
}
hasInput := true
if data := opts.Val("d"); data != "" {
// If data is provided via -d, read it from there for the verbose mode.
// XXX handle the @filename case.
inputWriter.Write([]byte(data))
} else if !terminal.IsTerminal(stdinFd) {
// If something is piped in to the command, tell curl to use it as input.
opts = append(opts, "-d@-")
// Tee the stdin to the buffer used show the posted data in verbose mode.
stdin = io.TeeReader(stdin, inputWriter)
} else {
hasInput = false
}
if hasInput {
if !headerSupplied(opts, "Content-Type") && !isForm {
opts = append(opts, "-H", "Content-Type: application/json")
}
}
}
if !headerSupplied(opts, "Accept") {
opts = append(opts, "-H", "Accept: application/json, */*")
}
if opts.Has("curl") {
opts.Remove("curl")
fmt.Print("curl")
for _, opt := range opts {
if strings.IndexByte(opt, ' ') != -1 {
fmt.Printf(" %q", opt)
} else {
fmt.Printf(" %s", opt)
}
}
fmt.Println()
return
}
cmd := exec.Command("curl", opts...)
cmd.Stdin = stdin
cmd.Stdout = stdout
cmd.Stderr = &formatter.HeaderCleaner{
Out: stderr,
Verbose: verbose,
Post: input,
}
if (opts.Has("I") || opts.Has("head")) && terminal.IsTerminal(stdoutFd) {
cmd.Stdout = ioutil.Discard
}
status := 0
if err := cmd.Run(); err != nil {
switch err := err.(type) {
case *exec.ExitError:
if err.Stderr != nil {
fmt.Fprint(stderr, string(err.Stderr))
}
if ws, ok := err.ProcessState.Sys().(syscall.WaitStatus); ok {
status = ws.ExitStatus()
}
default:
fmt.Fprint(stderr, err)
}
}
os.Exit(status)
}
func headerSupplied(opts args.Opts, header string) bool {
header = strings.ToLower(header)
for _, h := range append(opts.Vals("H"), opts.Vals("header")...) {
if strings.HasPrefix(strings.ToLower(h), header+":") {
return true
}
}
return false
}