Skip to content

Commit

Permalink
pkg/*: add spinner
Browse files Browse the repository at this point in the history
Signed-off-by: Gyuho Lee <leegyuho@amazon.com>
  • Loading branch information
gyuho committed Jul 10, 2020
1 parent 718c7dd commit 2d0aa8a
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 6 deletions.
12 changes: 8 additions & 4 deletions pkg/aws/cfn/cfn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"context"
"errors"
"fmt"
"io"
"strings"
"time"

"github.com/aws/aws-k8s-tester/pkg/ctxutil"
"github.com/aws/aws-k8s-tester/pkg/spinner"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/cloudformation/cloudformationiface"
Expand All @@ -28,13 +30,15 @@ func Poll(
ctx context.Context,
stopc chan struct{},
lg *zap.Logger,
logWriter io.Writer,
cfnAPI cloudformationiface.CloudFormationAPI,
stackID string,
desiredStackStatus string,
initialWait time.Duration,
pollInterval time.Duration,
) <-chan StackStatus {
now := time.Now()
sp := spinner.New("Waiting for CFN stack "+desiredStackStatus, logWriter)

lg.Info("polling stack",
zap.String("stack-id", stackID),
Expand Down Expand Up @@ -158,28 +162,28 @@ func Poll(

if first {
lg.Info("sleeping", zap.Duration("initial-wait", initialWait))

sp.Restart()
select {
case <-ctx.Done():
sp.Stop()
lg.Warn("wait aborted, ctx done", zap.Error(ctx.Err()))
ch <- StackStatus{Stack: nil, Error: ctx.Err()}
close(ch)
return

case <-stopc:
sp.Stop()
lg.Warn("wait stopped, stopc closed", zap.Error(ctx.Err()))
ch <- StackStatus{Stack: nil, Error: errors.New("wait stopped")}
close(ch)
return

case <-time.After(initialWait):
sp.Stop()
}
first = false
}

// continue for-loop
}

lg.Warn("wait aborted, ctx done", zap.Error(ctx.Err()))
ch <- StackStatus{Stack: nil, Error: ctx.Err()}
close(ch)
Expand Down
2 changes: 1 addition & 1 deletion pkg/logutil/multi-writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"go.uber.org/zap"
)

// NewWithStderrWriter creates a new logger with stderr multi writer.
// NewWithStderrWriter creates a new logger and multi-writer with os.Stderr.
// The returned file object is the log file.
// The log file must be specified with extension ".log".
func NewWithStderrWriter(logLevel string, logOutputs []string) (lg *zap.Logger, wr io.Writer, logFile *os.File, err error) {
Expand Down
15 changes: 14 additions & 1 deletion pkg/logutil/multi-writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"io/ioutil"
"os"
"testing"
"time"

"github.com/aws/aws-k8s-tester/pkg/fileutil"
"github.com/aws/aws-k8s-tester/pkg/spinner"
)

func TestMultiWriter(t *testing.T) {
Expand All @@ -23,9 +25,20 @@ func TestMultiWriter(t *testing.T) {
fmt.Fprintf(wr, "hello %q\n", "test")
fmt.Fprintf(wr, "hello %q\n", "test")

go func() {
time.Sleep(2 * time.Second)
lg.Info("here")
}()
s := spinner.New("Wating...\n", wr)
s.Start()
println()
defer s.Stop()
time.Sleep(5 * time.Second)
s.Restart()

b, err := ioutil.ReadFile(tmpPath)
if err != nil {
t.Fatal(err)
}
fmt.Println(string(b))
fmt.Println("output:", string(b))
}
44 changes: 44 additions & 0 deletions pkg/spinner/spinner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Package spinner implements spinner.
package spinner

import (
"fmt"
"io"
"os"
"strings"
"time"

"github.com/briandowns/spinner"
)

// New returns a new spinner based on the time.
// If the local time is day, returns "🌍".
// If the local time is night, returns "🌓".
func New(suffix string, wr io.Writer) (s Spinner) {
sets := spinner.CharSets[39]
if time.Now().Hour() > 17 { // after business hours
sets = spinner.CharSets[70]
}
if wr == nil {
wr = os.Stderr
}
s = Spinner{wr: wr, Spinner: spinner.New(sets, 100*time.Millisecond, spinner.WithWriter(wr))}
s.Suffix = " " + strings.TrimSpace(suffix)
s.FinalMSG = "\n"
return s
}

type Spinner struct {
wr io.Writer
*spinner.Spinner
}

func (s Spinner) Start() {
fmt.Fprintf(s.wr, "\n")
s.Spinner.Start()
}

func (s Spinner) Stop() {
fmt.Fprintf(s.wr, "\n")
s.Spinner.Stop()
}
16 changes: 16 additions & 0 deletions pkg/spinner/spinner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package spinner

import (
"fmt"
"os"
"testing"
"time"
)

func TestSpinner(t *testing.T) {
s := New("hello", os.Stderr)
s.Start()
time.Sleep(3 * time.Second)
s.Stop()
fmt.Println("hello")
}

0 comments on commit 2d0aa8a

Please sign in to comment.