@@ -11,6 +11,8 @@ import (
11
11
"github.com/spiegel-im-spiegel/gocli/exitcode"
12
12
"github.com/spiegel-im-spiegel/gocli/rwi"
13
13
"github.com/spiegel-im-spiegel/gpgpdump"
14
+ "github.com/spiegel-im-spiegel/gpgpdump/errs"
15
+ "github.com/spiegel-im-spiegel/gpgpdump/info"
14
16
"github.com/spiegel-im-spiegel/gpgpdump/options"
15
17
)
16
18
@@ -30,13 +32,15 @@ var (
30
32
versionFlag bool //version flag
31
33
jsonFlag bool //output with JSON format
32
34
tomlFlag bool //output with TOML format
35
+ debugFlag bool //debug flag
33
36
indentSize int
37
+ filePath string
34
38
)
35
39
36
40
//newRootCmd returns cobra.Command instance for root command
37
41
func newRootCmd (ui * rwi.RWI , args []string ) * cobra.Command {
38
42
rootCmd := & cobra.Command {
39
- Use : Name + " [flags] [OpenPGP file]" ,
43
+ Use : Name ,
40
44
RunE : func (cmd * cobra.Command , args []string ) error {
41
45
//parse options
42
46
if versionFlag {
@@ -46,65 +50,72 @@ func newRootCmd(ui *rwi.RWI, args []string) *cobra.Command {
46
50
47
51
//open PGP file
48
52
reader := ui .Reader ()
49
- if len (args ) > 0 {
50
- file , err := os .Open (args [ 0 ]) //args[0] is maybe file path
53
+ if len (filePath ) > 0 {
54
+ file , err := os .Open (filePath )
51
55
if err != nil {
52
- return err
56
+ return debugPrint ( ui , err )
53
57
}
54
58
defer file .Close ()
55
59
reader = file
56
60
}
57
61
58
62
//parse OpenPGP packets
59
- info , err := gpgpdump .Parse (reader , opts )
63
+ r , err := marshalPacketInfo ( gpgpdump .Parse (reader , opts ) )
60
64
if err != nil {
61
- if opts .Debug () {
62
- _ = ui .OutputErrln (fmt .Sprintf ("%+v" , err ))
63
- }
64
- return err
65
- }
66
-
67
- //marshal packet info
68
- var result io.Reader
69
- if jsonFlag {
70
- result , err = info .JSON (indentSize )
71
- } else if tomlFlag {
72
- result , err = info .TOML (indentSize )
73
- } else if indentSize > 0 {
74
- result = info .ToString (strings .Repeat (" " , indentSize ))
75
- err = nil
76
- } else {
77
- result = info .ToString ("\t " )
78
- err = nil
65
+ return debugPrint (ui , err )
79
66
}
80
- if err != nil {
81
- if opts .Debug () {
82
- _ = ui .OutputErrln (fmt .Sprintf ("%+v" , err ))
83
- }
84
- return err
85
- }
86
- return ui .WriteFrom (result )
67
+ return debugPrint (ui , ui .WriteFrom (r ))
87
68
},
88
69
}
89
70
rootCmd .Flags ().BoolVarP (& versionFlag , "version" , "v" , false , "output version of " + Name )
90
- rootCmd .Flags ().BoolVarP (& jsonFlag , "json" , "j" , false , "output with JSON format" )
91
- rootCmd .Flags ().BoolVarP (& tomlFlag , "toml" , "t" , false , "output with TOML format" )
92
- rootCmd .Flags ().IntVarP (& indentSize , "indent" , "" , 0 , "indent size for output string" )
93
- rootCmd .Flags ().BoolP (options .ARMOR .String (), "a" , false , "accepts ASCII input only" )
94
- rootCmd .Flags ().BoolP (options .DEBUG .String (), "" , false , "for debug" ) //not use
95
- //rootCmd.Flags().BoolP(options.GDUMP.String(), "g", false, "selects alternate (GnuPG type) dump format") //not use
96
- rootCmd .Flags ().BoolP (options .INTEGER .String (), "i" , false , "dumps multi-precision integers" )
97
- rootCmd .Flags ().BoolP (options .LITERAL .String (), "l" , false , "dumps literal packets (tag 11)" )
98
- rootCmd .Flags ().BoolP (options .MARKER .String (), "m" , false , "dumps marker packets (tag 10)" )
99
- rootCmd .Flags ().BoolP (options .PRIVATE .String (), "p" , false , "dumps private packets (tag 60-63)" )
100
- rootCmd .Flags ().BoolP (options .UTC .String (), "u" , false , "output with UTC time" )
71
+ rootCmd .Flags ().StringVarP (& filePath , "file" , "f" , "" , "path of OpenPGP file" )
72
+ rootCmd .PersistentFlags ().BoolVarP (& jsonFlag , "json" , "j" , false , "output with JSON format" )
73
+ rootCmd .PersistentFlags ().BoolVarP (& tomlFlag , "toml" , "t" , false , "output with TOML format" )
74
+ rootCmd .PersistentFlags ().IntVarP (& indentSize , "indent" , "" , 0 , "indent size for output string" )
75
+ rootCmd .PersistentFlags ().BoolP (options .ARMOR .String (), "a" , false , "accepts ASCII input only" )
76
+ rootCmd .PersistentFlags ().BoolP (options .DEBUG .String (), "" , false , "for debug" ) //not use
77
+ //rootCmd.PersistentFlags().BoolP(options.GDUMP.String(), "g", false, "selects alternate (GnuPG type) dump format") //not use
78
+ rootCmd .PersistentFlags ().BoolP (options .INTEGER .String (), "i" , false , "dumps multi-precision integers" )
79
+ rootCmd .PersistentFlags ().BoolP (options .LITERAL .String (), "l" , false , "dumps literal packets (tag 11)" )
80
+ rootCmd .PersistentFlags ().BoolP (options .MARKER .String (), "m" , false , "dumps marker packets (tag 10)" )
81
+ rootCmd .PersistentFlags ().BoolP (options .PRIVATE .String (), "p" , false , "dumps private packets (tag 60-63)" )
82
+ rootCmd .PersistentFlags ().BoolP (options .UTC .String (), "u" , false , "output with UTC time" )
101
83
102
84
rootCmd .SetArgs (args )
103
85
rootCmd .SetOutput (ui .ErrorWriter ())
86
+ rootCmd .AddCommand (newVersionCmd (ui ))
87
+ rootCmd .AddCommand (newHkpCmd (ui ))
104
88
105
89
return rootCmd
106
90
}
107
91
92
+ func debugPrint (ui * rwi.RWI , err error ) error {
93
+ if debugFlag && err != nil {
94
+ fmt .Fprintf (ui .ErrorWriter (), "error: %+v\n " , err )
95
+ return nil
96
+ }
97
+ return errs .Cause (err )
98
+ }
99
+
100
+ func marshalPacketInfo (i * info.Info , e error ) (r io.Reader , err error ) {
101
+ if e != nil {
102
+ err = e
103
+ return
104
+ }
105
+ if jsonFlag {
106
+ r , err = i .JSON (indentSize )
107
+ } else if tomlFlag {
108
+ r , err = i .TOML (indentSize )
109
+ } else if indentSize > 0 {
110
+ r = i .ToString (strings .Repeat (" " , indentSize ))
111
+ err = nil
112
+ } else {
113
+ r = i .ToString ("\t " )
114
+ err = nil
115
+ }
116
+ return
117
+ }
118
+
108
119
func getBool (cmd * cobra.Command , code options.OptCode ) (options.OptCode , bool ) {
109
120
name := code .String ()
110
121
f , err := cmd .Flags ().GetBool (name )
@@ -115,7 +126,7 @@ func getBool(cmd *cobra.Command, code options.OptCode) (options.OptCode, bool) {
115
126
}
116
127
117
128
func parseOpt (cmd * cobra.Command ) options.Options {
118
- return options .New (
129
+ opts := options .New (
119
130
options .Set (getBool (cmd , options .ARMOR )),
120
131
options .Set (getBool (cmd , options .DEBUG )), //for debug
121
132
//options.Set(getBool(cmd, options.GDUMP)), //not use
@@ -125,6 +136,8 @@ func parseOpt(cmd *cobra.Command) options.Options {
125
136
options .Set (getBool (cmd , options .PRIVATE )),
126
137
options .Set (getBool (cmd , options .UTC )),
127
138
)
139
+ debugFlag = opts .Debug ()
140
+ return opts
128
141
}
129
142
130
143
//Execute is called from main function
0 commit comments