Skip to content

Commit

Permalink
Factor out common print loop code (#25?)
Browse files Browse the repository at this point in the history
  • Loading branch information
aidansteele committed May 29, 2019
1 parent 323f258 commit b51e07c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 48 deletions.
13 changes: 1 addition & 12 deletions cmd/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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 {
Expand Down
13 changes: 1 addition & 12 deletions cmd/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 1 addition & 12 deletions cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 15 additions & 12 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/glassechidna/stackit/pkg/stackit"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io"
"io/ioutil"
"os"
"strings"
Expand Down Expand Up @@ -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 {
Expand All @@ -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{}

Expand Down

0 comments on commit b51e07c

Please sign in to comment.