Skip to content

Commit

Permalink
overboard logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jlarfors committed Oct 13, 2024
1 parent 6e942ab commit 78314e7
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions pkg/x/sylt/terra.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,21 @@ func Terra[T terra.Exporter](
cmd: opt.cmd,
}

return &TerraAction[T]{
act := TerraAction[T]{
Name: name,
Stack: stack,
opts: opt,
cmd: &cmd,
log: slog.With("action_name", name, "action_type", ActionTypeTerra),
}
act.log = slog.With(
"action_name",
name,
"action_type",
ActionTypeTerra,
"dir",
act.dir(),
)
return &act
}

var _ Actioner = (*TerraAction[*terra.Stack])(nil)
Expand Down Expand Up @@ -101,6 +109,9 @@ func (a *TerraAction[T]) Run(ctx context.Context, opts RunOpts) error {
return ErrMissingActionName
}

runLog := a.log.With("run_opts", opts)

runLog.Info("exporting stack")
if err := a.Export(); err != nil {
return err
}
Expand All @@ -116,6 +127,7 @@ func (a *TerraAction[T]) Run(ctx context.Context, opts RunOpts) error {

// Skip terraform init if the stack is cached.
if !isCached {
runLog.Info("initialising stack")
if err := a.Init(ctx); err != nil {
return fmt.Errorf(
"initializing stack %s: %w",
Expand All @@ -127,6 +139,7 @@ func (a *TerraAction[T]) Run(ctx context.Context, opts RunOpts) error {
// If the action is marked for destruction, skip the plan and apply.
// We only need to show the state.
if opts.Destroy {
runLog.Info("importing state into stack")
if err := a.ImportState(ctx); err != nil {
return fmt.Errorf(
"getting state for stack %s: %w",
Expand All @@ -139,27 +152,32 @@ func (a *TerraAction[T]) Run(ctx context.Context, opts RunOpts) error {
// Skip plan if the stack is cached.
var diff bool
if !isCached {
runLog.Info("planning stack", "diff", diff)
var err error
diff, err = a.Plan(ctx)
if err != nil {
return fmt.Errorf(
"planning stack %s: %w", a.Name, err,
)
}
runLog.Info("planned stack", "diff", diff)
}
// Apply if there is a diff AND not dry run.
if diff && !opts.DryRun {
runLog.Info("applying stack")
if err := a.Apply(ctx); err != nil {
return fmt.Errorf(
"applying stack %s: %w", a.Name, err,
)
}
}
if err := a.ImportState(ctx); err != nil {
return fmt.Errorf(
"getting state for stack %s: %w",
a.Name, err,
)
} else {
runLog.Info("importing state into stack")
if err := a.ImportState(ctx); err != nil {
return fmt.Errorf(
"getting state for stack %s: %w",
a.Name, err,
)
}
}

// Only write the md5 checksum if the stack has no changes and caching is
Expand Down

0 comments on commit 78314e7

Please sign in to comment.