-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparameters.go
104 lines (89 loc) · 3.13 KB
/
parameters.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
)
var (
help bool
stderr, stdout *log.Logger
)
type multiParams map[string][]string
type getOptParameters struct {
optFormURLEncode bool
optSilence bool
optQueryString bool
optPostForm bool
optOutFile bool
optReadStdin bool
optSummary bool
optPost bool
optHTTPAction string
optURI string
optBasic string
optToken string
optBearer string
optType string
optBinData string
optHeaders multiParams
optData multiParams
}
const doc = `
%s is a tool for looking at the request being made
version: %s
compiled: %s
built: %s
Usage: %s -u <URL> [option [option]...]
Options:
`
// GetParams parses CLI args into values used by the application
func getParams(buildVersion, buildTimestamp, compiledBy string) *getOptParameters {
flag.Usage = func() {
fmt.Fprintf(
os.Stderr,
doc,
os.Args[0],
buildVersion,
compiledBy,
buildTimestamp,
os.Args[0],
)
flag.PrintDefaults()
}
params := &getOptParameters{
optHeaders: multiParams{},
optData: multiParams{},
}
// redis
flag.BoolVar(&help, "help", false, "display these program options")
flag.BoolVar(¶ms.optPost, "post", false, "sugar for setting the HTTP action to POST")
flag.BoolVar(¶ms.optFormURLEncode, "form", false, "sugar for adding 'Content-Type: application/x-www-form-urlencoded'")
flag.BoolVar(¶ms.optPostForm, "pf", false, "sugar for -post -form")
flag.BoolVar(¶ms.optSilence, "s", false, "shutup")
flag.BoolVar(¶ms.optQueryString, "query", false, "append -data-urlencoded's to the target URL as a query string")
flag.BoolVar(¶ms.optOutFile, "save", false, "write the output to a similarly named local file; to specify a different filename, simply redirect stdout")
flag.BoolVar(¶ms.optReadStdin, "stdin", false, "read the request body from stdin; request will ingore 'param' and 'body'")
flag.BoolVar(¶ms.optSummary, "summary", false, "after the request is finished, print a brief summary")
flag.Var(¶ms.optHeaders, "header", "`param=value` headers for the request")
flag.Var(¶ms.optData, "param", "`param=value` data for the request")
flag.StringVar(¶ms.optBinData, "body", "", "data as a string for the body of the request")
flag.StringVar(¶ms.optHTTPAction, "X", "GET", "specify the HTTP `action` (e.g. GET, POST, etc)")
flag.StringVar(¶ms.optURI, "url", "", "the destination URI")
flag.StringVar(¶ms.optBasic, "basic", "", "sugar for adding the 'Authorization: Basic $val' header")
flag.StringVar(¶ms.optToken, "token", "", "sugar for adding the 'Authorization: Token $val' header")
flag.StringVar(¶ms.optBearer, "bearer", "", "sugar for adding the 'Authorization: Bearer $val' header")
flag.StringVar(¶ms.optType, "type", "", "sugar for adding the 'Content-Type: $val' header")
flag.Parse()
if help || len(params.optURI) == 0 {
flag.Usage()
os.Exit(0)
}
stdout = log.New(os.Stdout, "", 0)
stderr = log.New(os.Stderr, "", 0)
if params.optSilence {
stderr = log.New(ioutil.Discard, "", 0)
}
return params
}