-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
111 lines (91 loc) · 2.77 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
package main
import (
"log"
"os"
"path/filepath"
"github.com/spf13/cobra"
)
const (
EncodeCmdName = "enc"
EncodeActName = "Encode"
DecodeCmdName = "dec"
DecodeActName = "Decode"
DefaultStreamName = "-"
)
type Options struct {
CmdName string
ActName string
Decode bool
IgnoreWhitespace bool
AppendNewline bool
CheckVersion *uint8
CheckVersionFlag string
Key string
Offset uint8
InputFilename string
OutputFilename string
}
func newEncCmd(options *Options) *cobra.Command {
return &cobra.Command{
Use: options.CmdName, Short: "Transcode various formats between streams or files.",
CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
}
}
var encCmd = newEncCmd(getDefaultOptions())
func main() {
log.SetFlags(0)
options := getDefaultOptions()
encCmd.PersistentFlags().BoolVarP(&options.Decode,
"decode", "D", options.Decode,
"decode input on stdin")
encCmd.PersistentFlags().BoolVarP(&options.IgnoreWhitespace,
"ignore-whitespace", "w", options.IgnoreWhitespace,
"ignore whitespace characters when decoding")
encCmd.PersistentFlags().BoolVarP(&options.AppendNewline,
"append-newline", "n", options.AppendNewline,
"append a trailing newline to the output")
encCmd.PersistentFlags().StringVarP(&options.InputFilename,
"input-file", "i", options.InputFilename,
"the input filename, omit or use \"-\" for stdin")
encCmd.PersistentFlags().StringVarP(&options.OutputFilename,
"output-file", "o", options.OutputFilename,
"the output filename, omit or use \"-\" for stdout")
encCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
setFilenameOptions(cmd, options)
}
addStreamingCodecs(encCmd, options)
addBufferedCodecs(encCmd, options)
if err := encCmd.Execute(); err != nil {
os.Exit(1)
}
}
func getDefaultOptions() *Options {
// Determine if we're being called as "dec" instead of "enc".
options := &Options{CmdName: EncodeCmdName, ActName: EncodeActName}
cmdName := filepath.Base(os.Args[0])
if cmdName == DecodeCmdName {
options.Decode = true
options.CmdName = DecodeCmdName
options.ActName = DecodeActName
}
return options
}
func setFilenameOptions(cmd *cobra.Command, options *Options) {
infilename := options.InputFilename
if infilename != "" && infilename != DefaultStreamName {
if in, err := os.Open(infilename); err != nil {
log.Fatalf("FATAL: failed to open input file %q for reading: %v", infilename, err)
} else {
cmd.SetIn(in)
}
}
outfilename := options.OutputFilename
if outfilename != "" && outfilename != DefaultStreamName {
flag := os.O_WRONLY | os.O_CREATE | os.O_EXCL
if out, err := os.OpenFile(outfilename, flag, 0600); err != nil {
log.Fatalf("FATAL: failed to open output file %q for writing: %v", outfilename, err)
} else {
cmd.SetOut(out)
}
}
}