-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflag_bytes_format.go
76 lines (62 loc) · 1.52 KB
/
flag_bytes_format.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
package main
import (
"errors"
"fmt"
"strings"
"github.com/azdagron/spire-pipe/codec"
)
type BytesFormatFlag []codec.Bytes
func AllBytesFormats() []codec.Bytes {
return append(Base64Formats(), codec.RawBytes())
}
func Base64Formats() []codec.Bytes {
return []codec.Bytes{
codec.StdBase64Bytes(),
codec.URLBase64Bytes(),
codec.RawStdBase64Bytes(),
codec.RawURLBase64Bytes(),
}
}
func (fl BytesFormatFlag) Set(value string) error {
if len(fl) == 0 {
return errors.New("flag misconfigured internally")
}
name := strings.ToLower(value)
for i, choice := range fl {
if choice.Name() == name {
fl[i] = fl[0]
fl[0] = choice
return nil
}
}
return fmt.Errorf("unknown format %q", value)
}
func (fl BytesFormatFlag) Type() string {
return "format"
}
func (fl BytesFormatFlag) String() string {
return fl.selection().Name()
}
func (fl BytesFormatFlag) Name() string {
return fl.selection().Name()
}
func (fl BytesFormatFlag) BytesIn(b []byte) ([]byte, error) {
return fl.selection().BytesIn(b)
}
func (fl BytesFormatFlag) BytesOut(b []byte) ([]byte, error) {
return fl.selection().BytesOut(b)
}
func (fl BytesFormatFlag) selection() codec.Bytes {
if len(fl) == 0 {
return unsetBytes{}
}
return fl[0]
}
type unsetBytes struct{}
func (unsetBytes) Name() string { return "" }
func (unsetBytes) BytesIn([]byte) ([]byte, error) {
return nil, errors.New("internal: flag is not initialized")
}
func (unsetBytes) BytesOut([]byte) ([]byte, error) {
return nil, errors.New("internal: flag is not initialized")
}