forked from powerman/dockerize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag.go
132 lines (113 loc) · 2.75 KB
/
flag.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
package main
import (
"errors"
"flag"
"fmt"
"net/url"
"os"
"strconv"
"strings"
)
var (
errNameValueMultiline = errors.New("name:value must be a single line")
errNameValueRequired = errors.New("must be a name:value")
errStatusCodeOutOfRange = errors.New("status code must be between 100 and 599")
errLeftRightRequired = errors.New("must be a left:right")
)
type stringsFlag []string
func (f *stringsFlag) Set(value string) error {
*f = append(*f, value)
return nil
}
func (f stringsFlag) String() string {
return strings.Join(f, ",")
}
type urlsFlag []*url.URL
func (f *urlsFlag) Set(value string) error {
u, err := url.Parse(value)
if err != nil {
return err
}
*f = append(*f, u)
return nil
}
func (f urlsFlag) String() string {
urls := make([]string, len(f))
for i := range f {
urls[i] = f[i].String()
}
return strings.Join(urls, " ")
}
type httpHeader struct {
name string
value string
}
type httpHeadersFlag []httpHeader
func (f *httpHeadersFlag) Set(value string) error {
buf, err := os.ReadFile(value) //nolint:gosec // File inclusion via variable.
if err == nil {
value = string(buf)
} else if !os.IsNotExist(err) {
return err
}
value = strings.TrimSpace(value)
if strings.ContainsAny(value, "\r\n") {
return errNameValueMultiline
} else if strings.Count(value, ":") == 0 {
return errNameValueRequired
}
nv := strings.SplitN(value, ":", 1+1)
for i := range nv {
nv[i] = strings.TrimSpace(nv[i])
if nv[i] == "" {
return errNameValueRequired
}
}
*f = append(*f, httpHeader{name: nv[0], value: nv[1]})
return nil
}
func (f httpHeadersFlag) String() string {
hs := make([]string, len(f))
for i := range f {
hs[i] = f[i].name + ":" + f[i].value
}
return strings.Join(hs, ", ")
}
type statusCodesFlag []int
func (f *statusCodesFlag) Set(value string) error {
i, err := strconv.Atoi(value)
if err != nil {
return err
}
if i < 100 || 599 < i {
return errStatusCodeOutOfRange
}
*f = append(*f, i)
return nil
}
func (f statusCodesFlag) String() string {
ns := make([]string, len(f))
for i := range f {
ns[i] = strconv.Itoa(f[i])
}
return strings.Join(ns, ", ")
}
type delimsFlag [2]string
func (f *delimsFlag) Set(value string) error {
delims := strings.Split(value, ":")
if len(delims) != 2 || len(strings.Fields(delims[0])) != 1 || len(strings.Fields(delims[1])) != 1 {
return errLeftRightRequired
}
(*f)[0] = delims[0]
(*f)[1] = delims[1]
return nil
}
func (f delimsFlag) String() string {
return f[0] + ":" + f[1]
}
// fatalFlagValue report invalid flag values in same way as flag.Parse().
func fatalFlagValue(msg, name string, val interface{}) {
fmt.Fprintf(os.Stderr, "invalid value %q for flag -%s: %s\n", val, name, msg)
flag.Usage()
os.Exit(exitCodeUsage) //nolint:revive // By design.
}