7
7
"runtime"
8
8
9
9
"github.com/spf13/cobra"
10
- "github.com/spiegel-im-spiegel/gocli"
10
+ "github.com/spiegel-im-spiegel/gocli/exitcode"
11
+ "github.com/spiegel-im-spiegel/gocli/rwi"
11
12
"github.com/spiegel-im-spiegel/gpgpdump"
12
13
"github.com/spiegel-im-spiegel/gpgpdump/options"
13
14
)
@@ -20,62 +21,81 @@ var (
20
21
)
21
22
22
23
var (
23
- versionFlag bool //version flag
24
- jsonFlag bool //output with JSON format
25
- tomlFlag bool //output with TOML format
26
- cui = gocli . NewUI () //CUI instance
24
+ versionFlag bool //version flag
25
+ jsonFlag bool //output with JSON format
26
+ tomlFlag bool //output with TOML format
27
+ cui = rwi . New () //CUI instance
27
28
)
28
29
29
- // rootCmd represents the base command when called without any subcommands
30
- var rootCmd = & cobra.Command {
31
- Use : Name + " [flags] [OpenPGP file]" ,
32
- RunE : func (cmd * cobra.Command , args []string ) error {
33
- //parse options
34
- if versionFlag {
35
- cui .OutputErr (Name )
36
- if len (Version ) > 0 {
37
- cui .OutputErr (fmt .Sprintf (" v%s" , Version ))
30
+ //newRootCmd returns cobra.Command instance for root command
31
+ func newRootCmd (ui * rwi.RWI , args []string ) * cobra.Command {
32
+ cui = ui
33
+ rootCmd := & cobra.Command {
34
+ Use : Name + " [flags] [OpenPGP file]" ,
35
+ RunE : func (cmd * cobra.Command , args []string ) error {
36
+ //parse options
37
+ if versionFlag {
38
+ cui .OutputErr (Name )
39
+ if len (Version ) > 0 {
40
+ cui .OutputErr (fmt .Sprintf (" v%s" , Version ))
41
+ }
42
+ cui .OutputErrln ()
43
+ cui .OutputErrln ("Copyright 2016-2018 Spiegel (based on pgpdump by kazu-yamamoto)" )
44
+ cui .OutputErrln ("Licensed under Apache License, Version 2.0" )
45
+ return nil
46
+ }
47
+ opts := parseOpt (cmd )
48
+
49
+ //open PGP file
50
+ reader := cui .Reader ()
51
+ if len (args ) > 0 {
52
+ file , err := os .Open (args [0 ]) //args[0] is maybe file path
53
+ if err != nil {
54
+ return err
55
+ }
56
+ defer file .Close ()
57
+ reader = file
38
58
}
39
- cui .OutputErrln ()
40
- cui .OutputErrln ("Copyright 2016,2017 Spiegel (based on pgpdump by kazu-yamamoto)" )
41
- cui .OutputErrln ("Licensed under Apache License, Version 2.0" )
42
- return nil
43
- }
44
- opts := parseOpt (cmd )
45
59
46
- //open PGP file
47
- reader := cui .Reader ()
48
- if len (args ) > 0 {
49
- file , err := os .Open (args [0 ]) //args[0] is maybe file path
60
+ //parse OpenPGP packets
61
+ info , err := gpgpdump .Parse (reader , opts )
50
62
if err != nil {
51
63
return err
52
64
}
53
- defer file .Close ()
54
- reader = file
55
- }
56
65
57
- //parse OpenPGP packets
58
- info , err := gpgpdump .Parse (reader , opts )
59
- if err != nil {
60
- return err
61
- }
66
+ //marshal packet info
67
+ var result io.Reader
68
+ if jsonFlag {
69
+ result , err = info .JSON ()
70
+ } else if tomlFlag {
71
+ result , err = info .TOML ()
72
+ } else {
73
+ result = info .ToString ("\t " )
74
+ err = nil
75
+ }
76
+ if err != nil {
77
+ return err
78
+ }
79
+ cui .WriteFrom (result )
80
+ return nil
81
+ },
82
+ }
83
+ rootCmd .Flags ().BoolVarP (& versionFlag , "version" , "v" , false , "output version of " + Name )
84
+ rootCmd .Flags ().BoolVarP (& jsonFlag , "json" , "j" , false , "output with JSON format" )
85
+ rootCmd .Flags ().BoolVarP (& tomlFlag , "toml" , "t" , false , "output with TOML format" )
86
+ rootCmd .Flags ().BoolP (options .ArmorOpt , "a" , false , "accepts ASCII input only" )
87
+ rootCmd .Flags ().BoolP (options .DebugOpt , "" , false , "for debug" ) //not use
88
+ //rootCmd.Flags().BoolP(options.GDumpOpt, "g", false, "selects alternate (GnuPG type) dump format") //not use
89
+ rootCmd .Flags ().BoolP (options .IntegerOpt , "i" , false , "dumps multi-precision integers" )
90
+ rootCmd .Flags ().BoolP (options .LiteralOpt , "l" , false , "dumps literal packets (tag 11)" )
91
+ rootCmd .Flags ().BoolP (options .MarkerOpt , "m" , false , "dumps marker packets (tag 10)" )
92
+ rootCmd .Flags ().BoolP (options .PrivateOpt , "p" , false , "dumps private packets (tag 60-63)" )
93
+ rootCmd .Flags ().BoolP (options .UTCOpt , "u" , false , "output with UTC time" )
62
94
63
- //marshal packet info
64
- var result io.Reader
65
- if jsonFlag {
66
- result , err = info .JSON ()
67
- } else if tomlFlag {
68
- result , err = info .TOML ()
69
- } else {
70
- result = info .ToString ("\t " )
71
- err = nil
72
- }
73
- if err != nil {
74
- return err
75
- }
76
- cui .WriteFrom (result )
77
- return nil
78
- },
95
+ rootCmd .SetArgs (args )
96
+ rootCmd .SetOutput (ui .ErrorWriter ())
97
+
98
+ return rootCmd
79
99
}
80
100
81
101
func getBool (cmd * cobra.Command , name string ) bool {
@@ -99,9 +119,8 @@ func parseOpt(cmd *cobra.Command) *options.Options {
99
119
)
100
120
}
101
121
102
- // Execute adds all child commands to the root command and sets flags appropriately.
103
- // This is called by main.main(). It only needs to happen once to the rootCmd.
104
- func Execute (ui * gocli.UI , args []string ) (exit ExitCode ) {
122
+ //Execute is called from main function
123
+ func Execute (ui * rwi.RWI , args []string ) (exit exitcode.ExitCode ) {
105
124
defer func () {
106
125
//panic hundling
107
126
if r := recover (); r != nil {
@@ -113,36 +132,19 @@ func Execute(ui *gocli.UI, args []string) (exit ExitCode) {
113
132
}
114
133
cui .OutputErrln (" ->" , depth , ":" , runtime .FuncForPC (pc ).Name (), ":" , src , ":" , line )
115
134
}
116
- exit = ExitAbnormal
135
+ exit = exitcode . Abnormal
117
136
}
118
137
}()
119
138
120
139
//execution
121
- cui = ui
122
- rootCmd .SetArgs (args )
123
- rootCmd .SetOutput (ui .ErrorWriter ())
124
- exit = ExitNormal
125
- if err := rootCmd .Execute (); err != nil {
126
- exit = ExitAbnormal
140
+ exit = exitcode .Normal
141
+ if err := newRootCmd (ui , args ).Execute (); err != nil {
142
+ exit = exitcode .Abnormal
127
143
}
128
144
return
129
145
}
130
146
131
- func init () {
132
- rootCmd .Flags ().BoolVarP (& versionFlag , "version" , "v" , false , "output version of " + Name )
133
- rootCmd .Flags ().BoolVarP (& jsonFlag , "json" , "j" , false , "output with JSON format" )
134
- rootCmd .Flags ().BoolVarP (& tomlFlag , "toml" , "t" , false , "output with TOML format" )
135
- rootCmd .Flags ().BoolP (options .ArmorOpt , "a" , false , "accepts ASCII input only" )
136
- rootCmd .Flags ().BoolP (options .DebugOpt , "" , false , "for debug" ) //not use
137
- //rootCmd.Flags().BoolP(options.GDumpOpt, "g", false, "selects alternate (GnuPG type) dump format") //not use
138
- rootCmd .Flags ().BoolP (options .IntegerOpt , "i" , false , "dumps multi-precision integers" )
139
- rootCmd .Flags ().BoolP (options .LiteralOpt , "l" , false , "dumps literal packets (tag 11)" )
140
- rootCmd .Flags ().BoolP (options .MarkerOpt , "m" , false , "dumps marker packets (tag 10)" )
141
- rootCmd .Flags ().BoolP (options .PrivateOpt , "p" , false , "dumps private packets (tag 60-63)" )
142
- rootCmd .Flags ().BoolP (options .UTCOpt , "u" , false , "output with UTC time" )
143
- }
144
-
145
- /* Copyright 2017 Spiegel
147
+ /* Copyright 2017,2018 Spiegel
146
148
*
147
149
* Licensed under the Apache License, Version 2.0 (the "License");
148
150
* you may not use this file except in compliance with the License.
0 commit comments