Skip to content

Commit

Permalink
Merge pull request #336 from oasisprotocol/andrej/feature/format
Browse files Browse the repository at this point in the history
feat: Unify command output format flag
  • Loading branch information
abukosek authored Dec 18, 2024
2 parents 9d0a497 + 740ed50 commit d46f750
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 176 deletions.
68 changes: 58 additions & 10 deletions cmd/common/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,65 @@ package common
import (
"context"
"fmt"
"strings"

flag "github.com/spf13/pflag"

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

var (
selectedHeight int64
force bool
answerYes bool
// HeightFlag is the flag for specifying block height.
HeightFlag *flag.FlagSet

// ForceFlag is a force mode switch.
ForceFlag *flag.FlagSet

// AnswerYesFlag answers yes to all questions.
AnswerYesFlag *flag.FlagSet

// FormatFlag specifies the command's output format (text/json).
FormatFlag *flag.FlagSet
)

// HeightFlag is the flag for specifying block height.
var HeightFlag *flag.FlagSet
// FormatType specifies the type of format for output of commands.
type FormatType string

// ForceFlag is a force mode switch.
var ForceFlag *flag.FlagSet
// Supported output formats for the format flag.
const (
// Output plain text.
FormatText FormatType = "text"
// Output JSON.
FormatJSON FormatType = "json"
)

// String returns a string representation of the output format type.
func (f *FormatType) String() string {
return string(*f)
}

// AnswerYesFlag answers yes to all questions.
var AnswerYesFlag *flag.FlagSet
// Set sets the value of the type to the argument given.
func (f *FormatType) Set(v string) error {
switch strings.ToLower(v) {
case string(FormatText), string(FormatJSON):
*f = FormatType(v)
return nil
default:
return fmt.Errorf("unknown output format type, must be one of: %s", strings.Join([]string{string(FormatText), string(FormatJSON)}, ", "))
}
}

// Type returns the type of the flag.
func (f *FormatType) Type() string {
return "FormatType"
}

var (
selectedHeight int64
force bool
answerYes bool
outputFormat = FormatText
)

// GetHeight returns the user-selected block height.
func GetHeight() int64 {
Expand All @@ -34,11 +73,17 @@ func IsForce() bool {
return force
}

// IsForce returns force mode.
// GetAnswerYes returns whether all interactive questions should be answered
// with yes.
func GetAnswerYes() bool {
return answerYes
}

// OutputFormat returns the format of the command's output.
func OutputFormat() FormatType {
return outputFormat
}

// GetActualHeight returns the user-selected block height if explicitly
// specified, or the current latest height.
func GetActualHeight(
Expand All @@ -65,4 +110,7 @@ func init() {

AnswerYesFlag = flag.NewFlagSet("", flag.ContinueOnError)
AnswerYesFlag.BoolVarP(&answerYes, "yes", "y", false, "answer yes to all questions")

FormatFlag = flag.NewFlagSet("", flag.ContinueOnError)
FormatFlag.Var(&outputFormat, "format", "output format ["+strings.Join([]string{string(FormatText), string(FormatJSON)}, ",")+"]")
}
10 changes: 3 additions & 7 deletions cmd/network/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"

"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

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

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

if outputFormat == formatJSON {
if common.OutputFormat() == common.FormatJSON {
pp, err := json.MarshalIndent(doc, "", " ")
cobra.CheckErr(err)
fmt.Printf("%s\n", pp)
}
}

func init() {
formatFlag := flag.NewFlagSet("", flag.ContinueOnError)
formatFlag.StringVar(&outputFormat, "format", formatText, "output format ["+strings.Join([]string{formatText, formatJSON}, ",")+"]")
showCmd.Flags().AddFlagSet(formatFlag)

showCmd.Flags().AddFlagSet(common.SelectorNFlags)
showCmd.Flags().AddFlagSet(common.HeightFlag)
showCmd.Flags().AddFlagSet(common.FormatFlag)
}
Loading

0 comments on commit d46f750

Please sign in to comment.