Skip to content

Commit

Permalink
feat: use commonFlagsBuilder as a method
Browse files Browse the repository at this point in the history
Signed-off-by: ismael FALL <[email protected]>
  • Loading branch information
Doozers committed Sep 15, 2022
1 parent 42eb923 commit 3f69966
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
22 changes: 11 additions & 11 deletions go/cmd/yolo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ type GlobalOptions struct {
server server
}

func (g *GlobalOptions) commonFlagsBuilder(fs *flag.FlagSet) {
fs.BoolVar(&g.verbose, "v", false, "increase log verbosity")
fs.StringVar(&g.logFormat, "log-format", "console", strings.Join(zapconfig.AvailablePresets, ", "))
fs.StringVar(&g.dbStorePath, "db-path", ":memory:", "DB Store path")
}

var optsGlobal = &GlobalOptions{}

func main() {
Expand All @@ -56,20 +62,14 @@ func yolo(args []string) error {
rand.Seed(time.Now().UnixNano())
rootFlagSet.SetOutput(os.Stderr)

commonFlagsBuilder := func(fs *flag.FlagSet) {
fs.BoolVar(&optsGlobal.verbose, "v", false, "increase log verbosity")
fs.StringVar(&optsGlobal.logFormat, "log-format", "console", strings.Join(zapconfig.AvailablePresets, ", "))
fs.StringVar(&optsGlobal.dbStorePath, "db-path", ":memory:", "DB Store path")
}

root := &climan.Command{
ShortUsage: `server [flags] <subcommand>`,
FlagSetBuilder: commonFlagsBuilder,
FlagSetBuilder: optsGlobal.commonFlagsBuilder,
Subcommands: []*climan.Command{
serverCommand(commonFlagsBuilder),
dumpObjectsCommand(commonFlagsBuilder),
infoCommand(commonFlagsBuilder),
treeCommand(commonFlagsBuilder),
serverCommand(),
dumpObjectsCommand(),
infoCommand(),
treeCommand(),
},
FFOptions: []ff.Option{ff.WithEnvVarNoPrefix()},
Exec: func(_ context.Context, _ []string) error {
Expand Down
4 changes: 2 additions & 2 deletions go/cmd/yolo/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ func (s server) parse(fs *flag.FlagSet) {
fs.StringVar(&optsGlobal.server.iosPrivkeyPass, "ios-pass", "", "iOS signing: password for private key or p12 file")
}

func serverCommand(commonFlagsBuilder flagsBuilder) *climan.Command {
func serverCommand() *climan.Command {
return &climan.Command{
Name: `server`,
ShortHelp: `Start a Yolo Server`,
FFOptions: []ff.Option{ff.WithEnvVarNoPrefix()},
FlagSetBuilder: func(fs *flag.FlagSet) {
commonFlagsBuilder(fs)
optsGlobal.commonFlagsBuilder(fs)
optsGlobal.server.parse(fs)
},
Exec: func(ctx context.Context, _ []string) error {
Expand Down
12 changes: 6 additions & 6 deletions go/cmd/yolo/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"github.com/peterbourgon/ff/v3"
)

func dumpObjectsCommand(commonFlagsBuilder flagsBuilder) *climan.Command {
func dumpObjectsCommand() *climan.Command {
return &climan.Command{
Name: `dump-objects`,
FlagSetBuilder: commonFlagsBuilder,
FlagSetBuilder: optsGlobal.commonFlagsBuilder,
FFOptions: []ff.Option{ff.WithEnvVarNoPrefix()},
Exec: func(_ context.Context, _ []string) error {
logger, err := loggerFromArgs(optsGlobal.verbose, optsGlobal.logFormat)
Expand Down Expand Up @@ -51,10 +51,10 @@ func dumpObjectsCommand(commonFlagsBuilder flagsBuilder) *climan.Command {
}
}

func treeCommand(commonFlagsBuilder flagsBuilder) *climan.Command {
func treeCommand() *climan.Command {
return &climan.Command{
Name: `tree`,
FlagSetBuilder: commonFlagsBuilder,
FlagSetBuilder: optsGlobal.commonFlagsBuilder,
FFOptions: []ff.Option{ff.WithEnvVarNoPrefix()},
Exec: func(ctx context.Context, _ []string) error {
logger, err := loggerFromArgs(optsGlobal.verbose, optsGlobal.logFormat)
Expand Down Expand Up @@ -89,10 +89,10 @@ func treeCommand(commonFlagsBuilder flagsBuilder) *climan.Command {
}
}

func infoCommand(commonFlagsBuilder flagsBuilder) *climan.Command {
func infoCommand() *climan.Command {
return &climan.Command{
Name: `info`,
FlagSetBuilder: commonFlagsBuilder,
FlagSetBuilder: optsGlobal.commonFlagsBuilder,
FFOptions: []ff.Option{ff.WithEnvVarNoPrefix()},
Exec: func(_ context.Context, _ []string) error {
logger, err := loggerFromArgs(optsGlobal.verbose, optsGlobal.logFormat)
Expand Down

0 comments on commit 3f69966

Please sign in to comment.