Skip to content

Commit e075ed1

Browse files
committed
SetupCommonFlags
1 parent 0e6ecff commit e075ed1

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

internal/cli/adv2new/adv2new.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package adv2new
33
import (
44
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/cli"
55
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/convert"
6-
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/flag"
76
"github.com/spf13/afero"
87
"github.com/spf13/cobra"
98
)
@@ -26,13 +25,6 @@ func Builder() *cobra.Command {
2625
return o.Run()
2726
},
2827
}
29-
cmd.Flags().StringVarP(&o.File, flag.File, flag.FileShort, "", "input file")
30-
_ = cmd.MarkFlagRequired(flag.File)
31-
cmd.Flags().StringVarP(&o.Output, flag.Output, flag.OutputShort, "", "output file")
32-
_ = cmd.MarkFlagRequired(flag.Output)
33-
cmd.Flags().BoolVarP(&o.ReplaceOutput, flag.ReplaceOutput, flag.ReplaceOutputShort, false,
34-
"replace output file if exists")
35-
cmd.Flags().BoolVarP(&o.Watch, flag.Watch, flag.WatchShort, false,
36-
"keeps the plugin running and watches the input file for changes")
28+
cli.SetupCommonFlags(cmd, o)
3729
return cmd
3830
}

internal/cli/clu2adv/clu2adv.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,7 @@ func Builder() *cobra.Command {
3333
return o.Run()
3434
},
3535
}
36-
cmd.Flags().StringVarP(&o.File, flag.File, flag.FileShort, "", "input file")
37-
_ = cmd.MarkFlagRequired(flag.File)
38-
cmd.Flags().StringVarP(&o.Output, flag.Output, flag.OutputShort, "", "output file")
39-
_ = cmd.MarkFlagRequired(flag.Output)
40-
cmd.Flags().BoolVarP(&o.ReplaceOutput, flag.ReplaceOutput, flag.ReplaceOutputShort, false,
41-
"replace output file if exists")
42-
cmd.Flags().BoolVarP(&o.Watch, flag.Watch, flag.WatchShort, false,
43-
"keeps the plugin running and watches the input file for changes")
36+
cli.SetupCommonFlags(cmd, o.BaseOpts)
4437
cmd.Flags().BoolVarP(&o.includeMoved, flag.IncludeMoved, flag.IncludeMovedShort, false,
4538
"include moved blocks in the output file")
4639
return cmd

internal/cli/common.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66

77
"github.com/fsnotify/fsnotify"
88
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/file"
9+
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/flag"
910
"github.com/spf13/afero"
11+
"github.com/spf13/cobra"
1012
)
1113

1214
type ConvertFn func(config []byte) ([]byte, error)
@@ -106,3 +108,15 @@ func (o *BaseOpts) waitForFileEvent(watcher *fsnotify.Watcher) error {
106108
}
107109
return nil
108110
}
111+
112+
// SetupCommonFlags sets up the common flags used by all commands.
113+
func SetupCommonFlags(cmd *cobra.Command, opts *BaseOpts) {
114+
cmd.Flags().StringVarP(&opts.File, flag.File, flag.FileShort, "", "input file")
115+
_ = cmd.MarkFlagRequired(flag.File)
116+
cmd.Flags().StringVarP(&opts.Output, flag.Output, flag.OutputShort, "", "output file")
117+
_ = cmd.MarkFlagRequired(flag.Output)
118+
cmd.Flags().BoolVarP(&opts.ReplaceOutput, flag.ReplaceOutput, flag.ReplaceOutputShort, false,
119+
"replace output file if exists")
120+
cmd.Flags().BoolVarP(&opts.Watch, flag.Watch, flag.WatchShort, false,
121+
"keeps the plugin running and watches the input file for changes")
122+
}

0 commit comments

Comments
 (0)