Skip to content

Commit

Permalink
feat: Adding log-disable-error-summary flag (#3687)
Browse files Browse the repository at this point in the history
* feat: Adding `skip-error-summary` flag

* fix: Renaming the flag to something better
  • Loading branch information
yhakbar authored Dec 20, 2024
1 parent ebfba03 commit e7abbd0
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 28 deletions.
9 changes: 9 additions & 0 deletions cli/commands/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ const (
TerragruntLogCustomFormatFlagName = "terragrunt-log-custom-format"
TerragruntLogCustomFormatEnvName = "TERRAGRUNT_LOG_CUSTOM_FORMAT"

TerragruntLogDisableErrorSummaryFlagName = "terragrunt-log-disable-error-summary"
TerragruntLogDisableErrorSummaryEnvName = "TERRAGRUNT_LOG_DISABLE_ERROR_SUMMARY"

// Strict Mode related flags/envs
TerragruntStrictModeFlagName = "strict-mode"
TerragruntStrictModeEnvName = "TERRAGRUNT_STRICT_MODE"
Expand Down Expand Up @@ -642,6 +645,12 @@ func NewGlobalFlags(opts *options.TerragruntOptions) cli.Flags {
Usage: "Terragrunt engine log level.",
Hidden: true,
},
&cli.BoolFlag{
Name: TerragruntLogDisableErrorSummaryFlagName,
EnvVar: TerragruntLogDisableErrorSummaryEnvName,
Destination: &opts.LogDisableErrorSummary,
Usage: "Skip error summary at the end of the command.",
},
}

flags.Sort()
Expand Down
15 changes: 8 additions & 7 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ func createEngine(terragruntOptions *options.TerragruntOptions) (*proto.EngineCl

// invoke engine for working directory
func invoke(ctx context.Context, runOptions *ExecutionOptions, client *proto.EngineClient) (*util.CmdOutput, error) {
terragruntOptions := runOptions.TerragruntOptions
opts := runOptions.TerragruntOptions

meta, err := ConvertMetaToProtobuf(runOptions.TerragruntOptions.Engine.Meta)
if err != nil {
Expand Down Expand Up @@ -607,15 +607,16 @@ func invoke(ctx context.Context, runOptions *ExecutionOptions, client *proto.Eng
return nil, errors.New(err)
}

terragruntOptions.Logger.Debugf("Engine execution done in %v", terragruntOptions.WorkingDir)
opts.Logger.Debugf("Engine execution done in %v", opts.WorkingDir)

if resultCode != 0 {
err = util.ProcessExecutionError{
Err: errors.Errorf("command failed with exit code %d", resultCode),
Output: output,
WorkingDir: terragruntOptions.WorkingDir,
Command: runOptions.Command,
Args: runOptions.Args,
Err: errors.Errorf("command failed with exit code %d", resultCode),
Output: output,
WorkingDir: opts.WorkingDir,
Command: runOptions.Command,
Args: runOptions.Args,
DisableSummary: opts.LogDisableErrorSummary,
}

return nil, errors.New(err)
Expand Down
19 changes: 13 additions & 6 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,12 @@ type TerragruntOptions struct {
// as it's not something intended for a user
// to use in a programmatic way.
Headless bool

// LogDisableErrorSummary is a flag to skip the error summary
// provided at the end of Terragrunt execution to
// recap all that was emitted in stderr throughout
// the run of an orchestrated process.
LogDisableErrorSummary bool
}

// TerragruntOptionsFunc is a functional option type used to pass options in certain integration tests
Expand Down Expand Up @@ -676,12 +682,13 @@ func (opts *TerragruntOptions) Clone(terragruntConfigPath string) (*TerragruntOp
// the future, we can deep clone this as well.
Experiments: opts.Experiments,
// copy array
StrictControls: util.CloneStringList(opts.StrictControls),
FeatureFlags: opts.FeatureFlags,
Errors: cloneErrorsConfig(opts.Errors),
ScaffoldNoIncludeRoot: opts.ScaffoldNoIncludeRoot,
ScaffoldRootFileName: opts.ScaffoldRootFileName,
Headless: opts.Headless,
StrictControls: util.CloneStringList(opts.StrictControls),
FeatureFlags: opts.FeatureFlags,
Errors: cloneErrorsConfig(opts.Errors),
ScaffoldNoIncludeRoot: opts.ScaffoldNoIncludeRoot,
ScaffoldRootFileName: opts.ScaffoldRootFileName,
Headless: opts.Headless,
LogDisableErrorSummary: opts.LogDisableErrorSummary,
}, nil
}

Expand Down
20 changes: 11 additions & 9 deletions shell/run_shell_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,11 @@ func RunShellCommandWithOutput(

if err := cmd.Start(); err != nil { //nolint:contextcheck
err = util.ProcessExecutionError{
Err: err,
Args: args,
Command: command,
WorkingDir: cmd.Dir,
Err: err,
Args: args,
Command: command,
WorkingDir: cmd.Dir,
DisableSummary: opts.LogDisableErrorSummary,
}

return errors.New(err)
Expand All @@ -235,11 +236,12 @@ func RunShellCommandWithOutput(

if err := cmd.Wait(); err != nil {
err = util.ProcessExecutionError{
Err: err,
Args: args,
Command: command,
Output: output,
WorkingDir: cmd.Dir,
Err: err,
Args: args,
Command: command,
Output: output,
WorkingDir: cmd.Dir,
DisableSummary: opts.LogDisableErrorSummary,
}

return errors.New(err)
Expand Down
22 changes: 16 additions & 6 deletions util/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,30 @@ func GetExitCode(err error) (int, error) {

// ProcessExecutionError - error returned when a command fails, contains StdOut and StdErr
type ProcessExecutionError struct {
Err error
Output CmdOutput
WorkingDir string
Command string
Args []string
Err error
Output CmdOutput
WorkingDir string
Command string
Args []string
DisableSummary bool
}

func (err ProcessExecutionError) Error() string {
if err.DisableSummary {
return fmt.Sprintf("Failed to execute \"%s %s\" in %s",
err.Command,
strings.Join(err.Args, " "),
err.WorkingDir,
)
}

return fmt.Sprintf("Failed to execute \"%s %s\" in %s\n%s\n%v",
err.Command,
strings.Join(err.Args, " "),
err.WorkingDir,
err.Output.Stderr.String(),
err.Err)
err.Err,
)
}

func (err ProcessExecutionError) ExitStatus() (int, error) {
Expand Down

0 comments on commit e7abbd0

Please sign in to comment.