Skip to content

Commit d46f750

Browse files
authored
Merge pull request #336 from oasisprotocol/andrej/feature/format
feat: Unify command output format flag
2 parents 9d0a497 + 740ed50 commit d46f750

File tree

4 files changed

+199
-176
lines changed

4 files changed

+199
-176
lines changed

cmd/common/flags.go

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,65 @@ package common
33
import (
44
"context"
55
"fmt"
6+
"strings"
67

78
flag "github.com/spf13/pflag"
89

910
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
1011
)
1112

1213
var (
13-
selectedHeight int64
14-
force bool
15-
answerYes bool
14+
// HeightFlag is the flag for specifying block height.
15+
HeightFlag *flag.FlagSet
16+
17+
// ForceFlag is a force mode switch.
18+
ForceFlag *flag.FlagSet
19+
20+
// AnswerYesFlag answers yes to all questions.
21+
AnswerYesFlag *flag.FlagSet
22+
23+
// FormatFlag specifies the command's output format (text/json).
24+
FormatFlag *flag.FlagSet
1625
)
1726

18-
// HeightFlag is the flag for specifying block height.
19-
var HeightFlag *flag.FlagSet
27+
// FormatType specifies the type of format for output of commands.
28+
type FormatType string
2029

21-
// ForceFlag is a force mode switch.
22-
var ForceFlag *flag.FlagSet
30+
// Supported output formats for the format flag.
31+
const (
32+
// Output plain text.
33+
FormatText FormatType = "text"
34+
// Output JSON.
35+
FormatJSON FormatType = "json"
36+
)
37+
38+
// String returns a string representation of the output format type.
39+
func (f *FormatType) String() string {
40+
return string(*f)
41+
}
2342

24-
// AnswerYesFlag answers yes to all questions.
25-
var AnswerYesFlag *flag.FlagSet
43+
// Set sets the value of the type to the argument given.
44+
func (f *FormatType) Set(v string) error {
45+
switch strings.ToLower(v) {
46+
case string(FormatText), string(FormatJSON):
47+
*f = FormatType(v)
48+
return nil
49+
default:
50+
return fmt.Errorf("unknown output format type, must be one of: %s", strings.Join([]string{string(FormatText), string(FormatJSON)}, ", "))
51+
}
52+
}
53+
54+
// Type returns the type of the flag.
55+
func (f *FormatType) Type() string {
56+
return "FormatType"
57+
}
58+
59+
var (
60+
selectedHeight int64
61+
force bool
62+
answerYes bool
63+
outputFormat = FormatText
64+
)
2665

2766
// GetHeight returns the user-selected block height.
2867
func GetHeight() int64 {
@@ -34,11 +73,17 @@ func IsForce() bool {
3473
return force
3574
}
3675

37-
// IsForce returns force mode.
76+
// GetAnswerYes returns whether all interactive questions should be answered
77+
// with yes.
3878
func GetAnswerYes() bool {
3979
return answerYes
4080
}
4181

82+
// OutputFormat returns the format of the command's output.
83+
func OutputFormat() FormatType {
84+
return outputFormat
85+
}
86+
4287
// GetActualHeight returns the user-selected block height if explicitly
4388
// specified, or the current latest height.
4489
func GetActualHeight(
@@ -65,4 +110,7 @@ func init() {
65110

66111
AnswerYesFlag = flag.NewFlagSet("", flag.ContinueOnError)
67112
AnswerYesFlag.BoolVarP(&answerYes, "yes", "y", false, "answer yes to all questions")
113+
114+
FormatFlag = flag.NewFlagSet("", flag.ContinueOnError)
115+
FormatFlag.Var(&outputFormat, "format", "output format ["+strings.Join([]string{string(FormatText), string(FormatJSON)}, ",")+"]")
68116
}

cmd/network/show.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"strings"
1010

1111
"github.com/spf13/cobra"
12-
flag "github.com/spf13/pflag"
1312

1413
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
1514
consensusPretty "github.com/oasisprotocol/oasis-core/go/common/prettyprint"
@@ -428,7 +427,7 @@ func showParameters(ctx context.Context, npa *common.NPASelection, height int64,
428427
doc := make(map[string]interface{})
429428

430429
doSection := func(name string, params interface{}) {
431-
if outputFormat == formatJSON {
430+
if common.OutputFormat() == common.FormatJSON {
432431
doc[name] = params
433432
} else {
434433
fmt.Printf("=== %s PARAMETERS ===\n", strings.ToUpper(name))
@@ -446,18 +445,15 @@ func showParameters(ctx context.Context, npa *common.NPASelection, height int64,
446445
doSection("beacon", beaconParams)
447446
doSection("governance", governanceParams)
448447

449-
if outputFormat == formatJSON {
448+
if common.OutputFormat() == common.FormatJSON {
450449
pp, err := json.MarshalIndent(doc, "", " ")
451450
cobra.CheckErr(err)
452451
fmt.Printf("%s\n", pp)
453452
}
454453
}
455454

456455
func init() {
457-
formatFlag := flag.NewFlagSet("", flag.ContinueOnError)
458-
formatFlag.StringVar(&outputFormat, "format", formatText, "output format ["+strings.Join([]string{formatText, formatJSON}, ",")+"]")
459-
showCmd.Flags().AddFlagSet(formatFlag)
460-
461456
showCmd.Flags().AddFlagSet(common.SelectorNFlags)
462457
showCmd.Flags().AddFlagSet(common.HeightFlag)
458+
showCmd.Flags().AddFlagSet(common.FormatFlag)
463459
}

0 commit comments

Comments
 (0)