MCPcopy
hub / github.com/keploy/keploy / PreProcessFlags

Method PreProcessFlags

cli/provider/cmd.go:573–671  ·  view source on GitHub ↗
(cmd *cobra.Command)

Source from the content-addressed store, hash-verified

571}
572
573func (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, &notFound) {
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)

Callers 1

ValidateMethod · 0.95

Implementers 1

CmdConfiguratorcli/provider/cmd.go

Calls 8

LogErrorFunction · 0.92
BindFlagsToViperFunction · 0.92
GetAbsPathFunction · 0.92
GetLastDirectoryFunction · 0.92
UnmarshalMethod · 0.80
NewMethod · 0.65
DebugMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected