diff --git a/cmd/down.go b/cmd/down.go index c80060c..8066ec0 100644 --- a/cmd/down.go +++ b/cmd/down.go @@ -30,7 +30,6 @@ var downCmd = &cobra.Command{ region := viper.GetString("region") profile := viper.GetString("profile") stackName := viper.GetString("stack-name") - printer := stackit.NewTailPrinter(cmd.OutOrStderr()) events := make(chan stackit.TailStackEvent) @@ -40,17 +39,7 @@ var downCmd = &cobra.Command{ ctx := context.Background() printerCtx, printerCancel := context.WithCancel(ctx) defer printerCancel() - - go func() { - for { - select { - case <-printerCtx.Done(): - return - case tailEvent := <-events: - printer.PrintTailEvent(tailEvent) - } - } - }() + go printUntilDone(printerCtx, events, cmd.OutOrStderr()) err := sit.Down(ctx, stackName, events) if err != nil { diff --git a/cmd/execute.go b/cmd/execute.go index 0d9ab7b..a39d984 100644 --- a/cmd/execute.go +++ b/cmd/execute.go @@ -29,21 +29,10 @@ func executeChangeSet(ctx context.Context, region, profile, stackName, changeSet sess := awsSession(profile, region) sit := stackit.NewStackit(cloudformation.New(sess), sts.New(sess)) events := make(chan stackit.TailStackEvent) - printer := stackit.NewTailPrinter(writer) printerCtx, printerCancel := context.WithCancel(ctx) defer printerCancel() - - go func() { - for { - select { - case <-printerCtx.Done(): - return - case tailEvent := <-events: - printer.PrintTailEvent(tailEvent) - } - } - }() + go printUntilDone(printerCtx, events, writer) var err error if len(changeSet) == 0 { diff --git a/cmd/package.go b/cmd/package.go index 8c3af32..a343010 100644 --- a/cmd/package.go +++ b/cmd/package.go @@ -52,21 +52,10 @@ func packageTemplate(ctx context.Context, region, profile, stackName, templatePa packager := stackit.NewPackager(s3api, sts.New(sess), region) events := make(chan stackit.TailStackEvent) - printer := stackit.NewTailPrinter(writer) printerCtx, printerCancel := context.WithCancel(ctx) defer printerCancel() - - go func() { - for { - select { - case <-printerCtx.Done(): - return - case tailEvent := <-events: - printer.PrintTailEvent(tailEvent) - } - } - }() + go printUntilDone(printerCtx, events, writer) upInput, err := packager.Package(stackName, absPath, tags, parameters) if err != nil { diff --git a/cmd/up.go b/cmd/up.go index eb9f514..aea5b2e 100644 --- a/cmd/up.go +++ b/cmd/up.go @@ -23,6 +23,7 @@ import ( "github.com/glassechidna/stackit/pkg/stackit" "github.com/spf13/cobra" "github.com/spf13/viper" + "io" "io/ioutil" "os" "strings" @@ -68,20 +69,9 @@ var upCmd = &cobra.Command{ ctx := context.Background() - printer := stackit.NewTailPrinter(cmd.OutOrStderr()) printerCtx, printerCancel := context.WithCancel(ctx) defer printerCancel() - - go func() { - for { - select { - case <-printerCtx.Done(): - return - case tailEvent := <-events: - printer.PrintTailEvent(tailEvent) - } - } - }() + go printUntilDone(printerCtx, events, cmd.OutOrStderr()) prepared, err := sit.Prepare(ctx, parsed, events) if err != nil { @@ -106,6 +96,19 @@ var upCmd = &cobra.Command{ }, } +func printUntilDone(ctx context.Context, events <-chan stackit.TailStackEvent, w io.Writer) { + printer := stackit.NewTailPrinter(w) + + for { + select { + case <-ctx.Done(): + return + case tailEvent := <-events: + printer.PrintTailEvent(tailEvent) + } + } +} + func keyvalSliceToMap(slice []string) map[string]string { theMap := map[string]string{}