Skip to content

Commit

Permalink
{cli} Combine option parameters and pass as struct
Browse files Browse the repository at this point in the history
  • Loading branch information
asmaloney committed Mar 2, 2024
1 parent 46a3d40 commit bc605dd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
12 changes: 9 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
34 changes: 20 additions & 14 deletions modes/defaultmode/defaultmode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit bc605dd

Please sign in to comment.