Skip to content

Commit 462df46

Browse files
Merge pull request #17 from spiegel-im-spiegel/refactoring-options
Refactoring options
2 parents fce9d05 + a36417c commit 462df46

File tree

18 files changed

+260
-445
lines changed

18 files changed

+260
-445
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
language: go
22

33
go:
4-
- 1.9.x
4+
- "1.10"
55

66
env:
7-
- DEP_VERSION="0.3.2"
7+
- DEP_VERSION="0.4.1"
88

99
before_install:
1010
# Download the binary to bin folder in $GOPATH

Gopkg.lock

Lines changed: 16 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,23 @@
1-
2-
# Gopkg.toml example
3-
#
4-
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
5-
# for detailed Gopkg.toml documentation.
6-
#
7-
# required = ["github.com/user/thing/cmd/thing"]
8-
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
9-
#
10-
# [[constraint]]
11-
# name = "github.com/user/project"
12-
# version = "1.0.0"
13-
#
14-
# [[constraint]]
15-
# name = "github.com/user/project2"
16-
# branch = "dev"
17-
# source = "github.com/myfork/project2"
18-
#
19-
# [[override]]
20-
# name = "github.com/x/y"
21-
# version = "2.4.0"
22-
23-
ignored = [
24-
"github.com/cpuguy83/go-md2man/md2man",
25-
"github.com/mitchellh/go-homedir",
26-
"github.com/russross/blackfriday",
27-
"github.com/spf13/cobra/cobra",
28-
"github.com/spf13/cobra/cobra/cmd",
29-
"github.com/spf13/cobra/doc",
30-
"github.com/spf13/viper",
31-
"gopkg.in/yaml.v2",
32-
]
33-
341
[[constraint]]
352
name = "github.com/spf13/cobra"
36-
version = "~0.0.1"
3+
version = "0.0.*"
374

385
[[constraint]]
396
name = "github.com/spiegel-im-spiegel/gocli"
40-
version = "~0.4.0"
7+
version = "0.5.*"
418

429
[[constraint]]
4310
name = "github.com/pkg/errors"
44-
version = "~0.8.0"
11+
version = "0.8.*"
4512

4613
[[constraint]]
4714
name = "github.com/BurntSushi/toml"
48-
version = "~0.3.0"
15+
version = "0.3.*"
4916

5017
[[constraint]]
5118
name = "golang.org/x/crypto"
5219
branch = "master"
20+
21+
[prune]
22+
go-tests = true
23+
unused-packages = true

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ $ cat sig | gpgpdump -j -u
210210
## Dependencies
211211

212212
```
213-
dep status -dot | dot -Tpng -o dependency.png
213+
$ dep status -dot | dot -Tpng -o dependency.png
214214
```
215215

216216
[![Dependencies](dependency.png)](dependency.png)

cli/gpgpdump/facade/armor_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ import (
44
"bytes"
55
"testing"
66

7-
"github.com/spiegel-im-spiegel/gocli"
7+
"github.com/spiegel-im-spiegel/gocli/exitcode"
8+
"github.com/spiegel-im-spiegel/gocli/rwi"
89
)
910

1011
func TestArmorNormal(t *testing.T) {
1112
inData := bytes.NewBufferString(ascdata1)
1213
outBuf := new(bytes.Buffer)
1314
outErrBuf := new(bytes.Buffer)
14-
ui := gocli.NewUI(gocli.Reader(inData), gocli.Writer(outBuf), gocli.ErrorWriter(outErrBuf))
15+
ui := rwi.New(rwi.Reader(inData), rwi.Writer(outBuf), rwi.ErrorWriter(outErrBuf))
1516
args := []string{"-a", "-u"}
1617

17-
clearFlags()
1818
exit := Execute(ui, args)
19-
if exit != ExitNormal {
20-
t.Errorf("Execute(armor) = \"%v\", want \"%v\".", exit, ExitNormal)
19+
if exit != exitcode.Normal {
20+
t.Errorf("Execute(armor) = \"%v\", want \"%v\".", exit, exitcode.Normal)
2121
}
2222
str := outErrBuf.String()
2323
if str != "" {
@@ -33,17 +33,16 @@ func TestArmorErr(t *testing.T) {
3333
inData := bytes.NewReader(bindata1)
3434
outBuf := new(bytes.Buffer)
3535
outErrBuf := new(bytes.Buffer)
36-
ui := gocli.NewUI(gocli.Reader(inData), gocli.Writer(outBuf), gocli.ErrorWriter(outErrBuf))
36+
ui := rwi.New(rwi.Reader(inData), rwi.Writer(outBuf), rwi.ErrorWriter(outErrBuf))
3737
args := []string{"-a"}
3838

39-
clearFlags()
4039
exit := Execute(ui, args)
41-
if exit != ExitAbnormal {
42-
t.Errorf("Execute(armor) = \"%v\", want \"%v\".", exit, ExitAbnormal)
40+
if exit != exitcode.Abnormal {
41+
t.Errorf("Execute(armor) = \"%v\", want \"%v\".", exit, exitcode.Abnormal)
4342
}
4443
}
4544

46-
/* Copyright 2017 Spiegel
45+
/* Copyright 2017,2018 Spiegel
4746
*
4847
* Licensed under the Apache License, Version 2.0 (the "License");
4948
* you may not use this file except in compliance with the License.

cli/gpgpdump/facade/facade.go

Lines changed: 75 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77
"runtime"
88

99
"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"
1112
"github.com/spiegel-im-spiegel/gpgpdump"
1213
"github.com/spiegel-im-spiegel/gpgpdump/options"
1314
)
@@ -20,62 +21,81 @@ var (
2021
)
2122

2223
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
2728
)
2829

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
3858
}
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)
4559

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)
5062
if err != nil {
5163
return err
5264
}
53-
defer file.Close()
54-
reader = file
55-
}
5665

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")
6294

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
7999
}
80100

81101
func getBool(cmd *cobra.Command, name string) bool {
@@ -99,9 +119,8 @@ func parseOpt(cmd *cobra.Command) *options.Options {
99119
)
100120
}
101121

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) {
105124
defer func() {
106125
//panic hundling
107126
if r := recover(); r != nil {
@@ -113,36 +132,19 @@ func Execute(ui *gocli.UI, args []string) (exit ExitCode) {
113132
}
114133
cui.OutputErrln(" ->", depth, ":", runtime.FuncForPC(pc).Name(), ":", src, ":", line)
115134
}
116-
exit = ExitAbnormal
135+
exit = exitcode.Abnormal
117136
}
118137
}()
119138

120139
//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
127143
}
128144
return
129145
}
130146

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
146148
*
147149
* Licensed under the Apache License, Version 2.0 (the "License");
148150
* you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)