From bc605ddded4da97f696c03b420069bd3e866006f Mon Sep 17 00:00:00 2001 From: Andy Maloney Date: Sat, 2 Mar 2024 18:08:51 -0500 Subject: [PATCH] {cli} Combine option parameters and pass as struct --- cmd/root.go | 12 ++++++++--- modes/defaultmode/defaultmode.go | 34 +++++++++++++++++++------------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 0742ba1..c479c73 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -39,8 +39,11 @@ var ( flagDebug = []string{} flagNoColour = false - flagRun = false + // special option just for outputting version flagVersion = false + + // options for the default command line mode + defaultModeOptions defaultmode.Options ) type errRequiresSubcommand struct { @@ -94,7 +97,9 @@ var rootCmd = &cobra.Command{ return err } - s, err := defaultmode.Initialize(settings, args, flagRun) + defaultModeOptions.FileList = args + + s, err := defaultmode.Initialize(settings, defaultModeOptions) if err != nil { return err } @@ -145,8 +150,9 @@ func init() { rootCmd.PersistentFlags().BoolVar(&flagNoColour, "no-colour", false, "do not use colour output on command line") // Local flags - only run when this action is called directly. - rootCmd.Flags().BoolVarP(&flagRun, "run", "r", false, "run the models after generating the code") rootCmd.Flags().BoolVarP(&flagVersion, "version", "v", false, "output the version and quit") + // Run options for default command line mode. + rootCmd.Flags().BoolVarP(&defaultModeOptions.RunAfterGeneration, "run", "r", false, "run the models after generating the code") rootCmd.MarkFlagsMutuallyExclusive("run", "version") rootCmd.SetGlobalNormalizationFunc(normalizeAliasFlagsFunc) diff --git a/modes/defaultmode/defaultmode.go b/modes/defaultmode/defaultmode.go index 9664c56..4e78702 100644 --- a/modes/defaultmode/defaultmode.go +++ b/modes/defaultmode/defaultmode.go @@ -22,26 +22,26 @@ var ( ErrNoValidModels = errors.New("no valid models to run") ) +type Options struct { + FileList []string + RunAfterGeneration bool +} + type DefaultMode struct { settings *cli.Settings - runAfterGenerate bool - fileList []string + runOptions Options } -func Initialize(settings *cli.Settings, files []string, runAfterGenerate bool) (d *DefaultMode, err error) { - d = &DefaultMode{ - settings: settings, - runAfterGenerate: runAfterGenerate, - } - +func Initialize(settings *cli.Settings, options Options) (d *DefaultMode, err error) { // Check if files exist first - if len(files) == 0 { + if len(options.FileList) == 0 { return nil, ErrNoInputFiles } - existingFiles := files[:0] - for _, file := range files { + existingFiles := make([]string, len(options.FileList)) + + for _, file := range options.FileList { if _, fileErr := os.Stat(file); errors.Is(fileErr, os.ErrNotExist) { fileErr = &filesystem.ErrFileDoesNotExist{FileName: file} chalk.PrintErr(fileErr) @@ -56,19 +56,25 @@ func Initialize(settings *cli.Settings, files []string, runAfterGenerate bool) ( return nil, ErrNoFilesToProcess } - d.fileList = existingFiles + d = &DefaultMode{ + settings: settings, + runOptions: options, + } + + d.runOptions.FileList = existingFiles + return } func (d *DefaultMode) Start() (err error) { fmt.Printf("Intermediate file path: %q\n", d.settings.TempPath) - err = generateCode(d.settings.ActiveFrameworks, d.fileList, d.settings.TempPath) + err = generateCode(d.settings.ActiveFrameworks, d.runOptions.FileList, d.settings.TempPath) if err != nil { return err } - if d.runAfterGenerate { + if d.runOptions.RunAfterGeneration { runCode(d.settings.ActiveFrameworks) } return