Skip to content

Commit

Permalink
bt: add output buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed Sep 19, 2024
1 parent 74c7203 commit f286948
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions bt/stack/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
func generateDAG(opt *getoptions.GetOpt, id string, cfg *sconfig.Config, normal bool) (*dag.Graph, error) {
tm := dag.NewTaskMap()
g := dag.NewGraph("stack " + id)
g.SetOutputBuffer(os.Stdout)

wd, err := os.Getwd()
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion bt/terraform/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/DavidGamba/dgtools/fsmodtime"
"github.com/DavidGamba/dgtools/run"
"github.com/DavidGamba/go-getoptions"
"github.com/DavidGamba/go-getoptions/dag"
"github.com/mattn/go-isatty"
)

Expand All @@ -31,6 +32,8 @@ func applyRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error

cfg := config.ConfigFromContext(ctx)
dir := DirFromContext(ctx)
stdout := dag.Stdout(ctx)
stderr := dag.Stderr(ctx)
LogConfig(cfg, profile)

ws, err := updateWSIfSelected(cfg.Config.DefaultTerraformProfile, cfg.Profile(profile), ws)
Expand Down Expand Up @@ -80,7 +83,7 @@ func applyRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error
Logger.Printf("export %s\n", wsEnv)
ri.Env(wsEnv)
}
err = ri.Run()
err = ri.Run(stdout, stderr)
if err != nil {
os.Remove(filepath.Join(dir, planFile))
return fmt.Errorf("failed to run: %w", err)
Expand Down
5 changes: 4 additions & 1 deletion bt/terraform/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/DavidGamba/dgtools/fsmodtime"
"github.com/DavidGamba/dgtools/run"
"github.com/DavidGamba/go-getoptions"
"github.com/DavidGamba/go-getoptions/dag"
"github.com/mattn/go-isatty"
)

Expand All @@ -28,6 +29,8 @@ func initRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {

cfg := config.ConfigFromContext(ctx)
dir := DirFromContext(ctx)
stdout := dag.Stdout(ctx)
stderr := dag.Stderr(ctx)
LogConfig(cfg, profile)
os.Setenv("CONFIG_ROOT", cfg.ConfigRoot)

Expand All @@ -50,7 +53,7 @@ func initRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
cmd = append(cmd, args...)
dataDir := fmt.Sprintf("TF_DATA_DIR=%s", getDataDir(cfg.Config.DefaultTerraformProfile, cfg.Profile(profile)))
Logger.Printf("export %s\n", dataDir)
err := run.CMDCtx(ctx, cmd...).Stdin().Log().Env(dataDir).Dir(dir).DryRun(dryRun).Run()
err := run.CMDCtx(ctx, cmd...).Stdin().Log().Env(dataDir).Dir(dir).DryRun(dryRun).Run(stdout, stderr)
if err != nil {
os.Remove(filepath.Join(dir, ".tf.lock"))
return fmt.Errorf("failed to run: %w", err)
Expand Down
5 changes: 4 additions & 1 deletion bt/terraform/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/DavidGamba/dgtools/fsmodtime"
"github.com/DavidGamba/dgtools/run"
"github.com/DavidGamba/go-getoptions"
"github.com/DavidGamba/go-getoptions/dag"
"github.com/hashicorp/terraform-config-inspect/tfconfig"
"github.com/mattn/go-isatty"
)
Expand Down Expand Up @@ -50,6 +51,8 @@ func planRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {

cfg := config.ConfigFromContext(ctx)
dir := DirFromContext(ctx)
stdout := dag.Stdout(ctx)
stderr := dag.Stderr(ctx)
LogConfig(cfg, profile)
os.Setenv("CONFIG_ROOT", cfg.ConfigRoot)

Expand Down Expand Up @@ -195,7 +198,7 @@ func planRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
Logger.Printf("export %s\n", wsEnv)
ri.Env(wsEnv)
}
err = ri.Run()
err = ri.Run(stdout, stderr)
if err != nil {
// exit code 2 with detailed-exitcode means changes found
var eerr *exec.ExitError
Expand Down
5 changes: 4 additions & 1 deletion bt/terraform/show_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/DavidGamba/dgtools/bt/config"
"github.com/DavidGamba/dgtools/run"
"github.com/DavidGamba/go-getoptions"
"github.com/DavidGamba/go-getoptions/dag"
"github.com/mattn/go-isatty"
)

Expand All @@ -26,6 +27,8 @@ func showPlanRun(ctx context.Context, opt *getoptions.GetOpt, args []string) err

cfg := config.ConfigFromContext(ctx)
dir := DirFromContext(ctx)
stdout := dag.Stdout(ctx)
stderr := dag.Stderr(ctx)
LogConfig(cfg, profile)

ws, err := updateWSIfSelected(cfg.Config.DefaultTerraformProfile, cfg.Profile(profile), ws)
Expand Down Expand Up @@ -60,7 +63,7 @@ func showPlanRun(ctx context.Context, opt *getoptions.GetOpt, args []string) err
Logger.Printf("export %s\n", wsEnv)
ri.Env(wsEnv)
}
err = ri.Run()
err = ri.Run(stdout, stderr)
if err != nil {
return fmt.Errorf("failed to run: %w", err)
}
Expand Down

0 comments on commit f286948

Please sign in to comment.