(cmd *cobra.Command)
| 571 | } |
| 572 | |
| 573 | func (c *CmdConfigurator) PreProcessFlags(cmd *cobra.Command) error { |
| 574 | // 1) Bind flags (highest precedence in Viper) |
| 575 | // used to bind common flags for commands like record, test. For eg: PATH, PORT, COMMAND etc. |
| 576 | if err := viper.BindPFlags(cmd.Flags()); err != nil { |
| 577 | errMsg := "failed to bind flags to config" |
| 578 | utils.LogError(c.logger, err, errMsg) |
| 579 | return errors.New(errMsg) |
| 580 | } |
| 581 | |
| 582 | // 2) Env: KEPLOY_* |
| 583 | viper.AutomaticEnv() |
| 584 | viper.SetEnvPrefix("KEPLOY") |
| 585 | |
| 586 | // 3) Nested flag binding (your existing util) |
| 587 | if err := utils.BindFlagsToViper(c.logger, cmd, ""); err != nil { |
| 588 | errMsg := "failed to bind cmd specific flags to viper" |
| 589 | utils.LogError(c.logger, err, errMsg) |
| 590 | return errors.New(errMsg) |
| 591 | } |
| 592 | |
| 593 | // 4) Use provided configPath and convert to absolute path |
| 594 | configPath, err := cmd.Flags().GetString("config-path") |
| 595 | if err != nil { |
| 596 | utils.LogError(c.logger, nil, "failed to read the config path") |
| 597 | return err |
| 598 | } |
| 599 | |
| 600 | // Convert to absolute path to ensure viper can find the config file correctly |
| 601 | absConfigPath, err := utils.GetAbsPath(configPath) |
| 602 | if err != nil { |
| 603 | errMsg := fmt.Sprintf("failed to convert config path to absolute path: %v", err) |
| 604 | utils.LogError(c.logger, err, errMsg) |
| 605 | return errors.New(errMsg) |
| 606 | } |
| 607 | configPath = absConfigPath |
| 608 | |
| 609 | c.logger.Debug("config path is ", zap.String("configPath", configPath)) |
| 610 | |
| 611 | // 5) Read base keploy.yml exactly like before |
| 612 | viper.SetConfigName("keploy") |
| 613 | viper.SetConfigType("yml") |
| 614 | viper.AddConfigPath(configPath) |
| 615 | |
| 616 | if err := viper.ReadInConfig(); err != nil { |
| 617 | var notFound viper.ConfigFileNotFoundError |
| 618 | if !errors.As(err, ¬Found) { |
| 619 | errMsg := "failed to read config file" |
| 620 | utils.LogError(c.logger, err, errMsg) |
| 621 | return errors.New(errMsg) |
| 622 | } |
| 623 | IsConfigFileFound = false |
| 624 | c.logger.Debug("config file not found; proceeding with flags only") |
| 625 | } else { |
| 626 | // 6) Base exists → try merging <last-dir>.keploy.yml (override) from the application folder (current working directory) |
| 627 | lastDir, err := utils.GetLastDirectory() |
| 628 | if err != nil { |
| 629 | errMsg := "failed to get last directory name for override config file" |
| 630 | utils.LogError(c.logger, err, errMsg) |
no test coverage detected