Skip to content

Commit

Permalink
fix: magerun command now handles config override correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
furan917 committed Nov 21, 2023
1 parent 1db92f3 commit 66eb5ea
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions cmd/magerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ var MagerunCmd = &cobra.Command{
//empty pre run to stop execution of parent RootCmd pre run
},
RunE: func(cmd *cobra.Command, args []string) error {

// handle global arguments (e.g. --config, --debug) as root command cannot due to DisableFlagParsing
var globalArguments = handleGlobalArguments(args)
magerunArgs := args[len(globalArguments):]

if len(magerunArgs) < 1 {
return fmt.Errorf("no command provided")
}
Expand Down Expand Up @@ -99,22 +97,42 @@ func initializeModuleWhichRequireConfig() {

func handleGlobalArguments(args []string) []string {
// Replicates RootCmd.PersistentPreRunE as it is not usable when DisableFlagParsing is set to true
// global Arg handling must be done manually
var globalArguments []string
var overrideFilePath string
for _, arg := range args {

//Note arguments between start and magerun command.
for i, arg := range args {
if strings.HasPrefix(arg, "--") {
globalArguments = append(globalArguments, arg)

if strings.HasPrefix(arg, "--config") {
// Catch both --config /file/path and --config=/file/path
overrideFilePath = strings.TrimPrefix(arg, "--config=")
overrideFilePath = strings.TrimPrefix(arg, "--config ")
}
if strings.HasPrefix(arg, "--debug") {
//if arg is --debug then enable debug mode
if arg == "--debug" {
logger.Warnf("Magerun: Debug mode enabled")
logger.EnableDebugMode()
}
}
if !strings.HasPrefix(arg, "--") {

//if arg is --config then configure
if strings.HasPrefix(arg, "--config") {
var configPath string
if strings.Contains(arg, "=") {
configPath = strings.Split(arg, "=")[1]
} else {
//ensure next argument is config path
if len(args) > i+1 && !strings.HasPrefix(args[i+1], "--") {
configPath = args[i+1]
//Ensure we remove the config path from args to avoid breaking early
args = append(args[:i+1], args[i+2:]...)
}
}
if configPath == "" {
logger.Warnf("No config file path provided with argument, using default config file path")
}

overrideFilePath = strings.Trim(configPath, `"'`)
}
} else {
//We have reached the magerun command, so exit loop
break
}
}
Expand Down

0 comments on commit 66eb5ea

Please sign in to comment.