(ctx context.Context, cmd *cobra.Command)
| 508 | } |
| 509 | |
| 510 | func (c *CmdConfigurator) Validate(ctx context.Context, cmd *cobra.Command) error { |
| 511 | err := isCompatible(c.logger) |
| 512 | if err != nil { |
| 513 | return err |
| 514 | } |
| 515 | defaultCfg := *c.cfg |
| 516 | err = c.PreProcessFlags(cmd) |
| 517 | if err != nil { |
| 518 | c.logger.Error("failed to preprocess flags", zap.Error(err)) |
| 519 | return err |
| 520 | } |
| 521 | err = c.ValidateFlags(ctx, cmd) |
| 522 | if err != nil { |
| 523 | if err == c.noCommandError() { |
| 524 | utils.LogError(c.logger, nil, "missing required -c flag or appCmd in config file") |
| 525 | if c.cfg.InDocker { |
| 526 | c.logger.Info(`Example usage: keploy test -c "docker run -p 8080:8080 --network myNetworkName myApplicationImageName" --delay 6`) |
| 527 | } else { |
| 528 | c.logger.Info(LogExample(RootExamples)) |
| 529 | } |
| 530 | } |
| 531 | c.logger.Error("failed to validate flags", zap.Error(err)) |
| 532 | return err |
| 533 | } |
| 534 | |
| 535 | appName, err := utils.GetLastDirectory() |
| 536 | if err != nil { |
| 537 | return fmt.Errorf("failed to get the last directory for appName: %v", err) |
| 538 | } |
| 539 | |
| 540 | if c.cfg.AppName == "" { |
| 541 | c.logger.Debug("Using the last directory name as appName : " + appName) |
| 542 | c.cfg.AppName = appName |
| 543 | } else if c.cfg.AppName != appName { |
| 544 | c.logger.Info("AppName in config (" + c.cfg.AppName + ") does not match current directory name (" + appName + ")") |
| 545 | } |
| 546 | |
| 547 | // The "create config file if missing" behavior is meaningful |
| 548 | // only for user-facing commands (record/test/etc.) that are |
| 549 | // expected to persist keploy.yml for reuse across invocations. |
| 550 | // The `agent` subcommand is a worker process spawned by the |
| 551 | // parent keploy: it still reads an existing keploy.yml via |
| 552 | // viper.ReadInConfig() in PreProcessFlags to pick up the same |
| 553 | // settings the parent resolved, but it has no use for writing |
| 554 | // a fresh one if the file is missing — the parent has already |
| 555 | // handed it the effective config via CLI flags + env. Running |
| 556 | // CreateConfigFile from the agent therefore produces a file |
| 557 | // no one reads; worse, on containerised runs the host's |
| 558 | // absolute --config-path doesn't resolve inside the agent's |
| 559 | // filesystem and the call fails with a misleading ENOENT |
| 560 | // "failed to write config file" ERROR (kafka-ecommerce CI |
| 561 | // run 2541/10). Skip the create-on-missing branch for the |
| 562 | // agent subcommand specifically. |
| 563 | if !IsConfigFileFound && cmd.Name() != "agent" { |
| 564 | err := c.CreateConfigFile(ctx, defaultCfg) |
| 565 | if err != nil { |
| 566 | c.logger.Error("failed to create config file", zap.Error(err)) |
| 567 | return err |
nothing calls this directly
no test coverage detected