From 43221c6674090651e48da525d87429ada9998d40 Mon Sep 17 00:00:00 2001 From: shivam Date: Sun, 11 Feb 2024 06:59:59 +0530 Subject: [PATCH 1/5] Created a spinner for let users wait that the test is running --- pkg/common/args.go | 112 +++++++++++++++++++++++++++++++++++++++++++-- pkg/log/logs.go | 8 +++- 2 files changed, 116 insertions(+), 4 deletions(-) diff --git a/pkg/common/args.go b/pkg/common/args.go index 0a390d0..97417ec 100644 --- a/pkg/common/args.go +++ b/pkg/common/args.go @@ -20,16 +20,23 @@ import ( "fmt" "os" "strings" - + "io" + "runtime" + "sync" + "time" "github.com/spf13/viper" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" - + "sigs.k8s.io/hydrophone/pkg/log" ) // PrintInfo prints the information about the cluster func PrintInfo(clientSet *kubernetes.Clientset, config *rest.Config) { + spinner := NewSpinner(os.Stdout) + spinner.Start() + + time.Sleep(2 * time.Second) serverVersion, err := clientSet.ServerVersion() if err != nil { log.Fatal("Error fetching server version: ", err) @@ -41,7 +48,7 @@ func PrintInfo(clientSet *kubernetes.Clientset, config *rest.Config) { viper.Set("busybox-image", busyboxImage) } - log.Printf("API endpoint : %s", config.Host) + log.PrintfAPI("API endpoint : %s", config.Host) log.Printf("Server version : %#v", *serverVersion) } @@ -87,3 +94,102 @@ func ValidateArgs() error { } return nil } + +var spinnerFrames = []string{ + "⠈⠁", "⠈⠑", "⠈⠱", "⠈⡱", "⢀⡱", "⢄⡱", "⢄⡱", "⢆⡱", "⢎⡱", "⢎⡰", + "⢎⡠", "⢎⡀", "⢎⠁", "⠎⠁", "⠊⠁", +} + +type Spinner struct { + stop chan struct{} + stopped chan struct{} + mu *sync.Mutex + running bool + writer io.Writer + ticker *time.Ticker + prefix string + suffix string + frameFormat string +} + +func NewSpinner(w io.Writer) *Spinner { + frameFormat := "\x1b[?7l\r%s%s%s\x1b[?7h" + if runtime.GOOS == "windows" { + frameFormat = "\r%s%s%s" + } + return &Spinner{ + stop: make(chan struct{}, 1), + stopped: make(chan struct{}), + mu: &sync.Mutex{}, + writer: w, + frameFormat: frameFormat, + } +} + +func (s *Spinner) SetPrefix(prefix string) { + s.mu.Lock() + defer s.mu.Unlock() + s.prefix = prefix +} + +func (s *Spinner) SetSuffix(suffix string) { + s.mu.Lock() + defer s.mu.Unlock() + s.suffix = suffix +} + +func (s *Spinner) Start() { + s.mu.Lock() + defer s.mu.Unlock() + if s.running { + return + } + s.running = true + s.ticker = time.NewTicker(time.Millisecond * 100) + go func() { + for { + for _, frame := range spinnerFrames { + select { + case <-s.stop: + func() { + s.mu.Lock() + defer s.mu.Unlock() + s.ticker.Stop() + s.running = false + s.stopped <- struct{}{} + }() + return + case <-s.ticker.C: + func() { + s.mu.Lock() + defer s.mu.Unlock() + fmt.Fprintf(s.writer, s.frameFormat, s.prefix, frame, s.suffix) + }() + } + } + } + }() +} + +func (s *Spinner) Stop() { + s.mu.Lock() + if !s.running { + s.mu.Unlock() + return + } + s.stop <- struct{}{} + s.mu.Unlock() + <-s.stopped +} + +func (s *Spinner) Write(p []byte) (n int, err error) { + s.mu.Lock() + defer s.mu.Unlock() + if !s.running { + return s.writer.Write(p) + } + if _, err := s.writer.Write([]byte("\r")); err != nil { + return 0, err + } + return s.writer.Write(p) +} \ No newline at end of file diff --git a/pkg/log/logs.go b/pkg/log/logs.go index d412ee9..5992f25 100644 --- a/pkg/log/logs.go +++ b/pkg/log/logs.go @@ -55,7 +55,13 @@ func Printf(format string, v ...any) { slog.Info(fmt.Sprintf(format, v...)) } +// Print logs for API +func PrintfAPI(format string, v ...interface{}) { + fmt.Print("\n") + slog.Info(fmt.Sprintf(format, v...)) +} + // Println logs an info message from the given arguments. func Println(v ...any) { slog.Info(fmt.Sprint(v...)) -} +} \ No newline at end of file From 7ef5f87a1819293dab4bcca0f83161565a1caacf Mon Sep 17 00:00:00 2001 From: shivam Date: Mon, 12 Feb 2024 00:02:19 +0530 Subject: [PATCH 2/5] Removed unused methods --- pkg/common/args.go | 105 +----------------------------------------- pkg/common/spinner.go | 85 ++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 104 deletions(-) create mode 100644 pkg/common/spinner.go diff --git a/pkg/common/args.go b/pkg/common/args.go index 97417ec..274bdaf 100644 --- a/pkg/common/args.go +++ b/pkg/common/args.go @@ -18,11 +18,7 @@ package common import ( "fmt" - "os" "strings" - "io" - "runtime" - "sync" "time" "github.com/spf13/viper" "k8s.io/client-go/kubernetes" @@ -48,7 +44,7 @@ func PrintInfo(clientSet *kubernetes.Clientset, config *rest.Config) { viper.Set("busybox-image", busyboxImage) } - log.PrintfAPI("API endpoint : %s", config.Host) + log.printf("API endpoint : %s", config.Host) log.Printf("Server version : %#v", *serverVersion) } @@ -93,103 +89,4 @@ func ValidateArgs() error { } } return nil -} - -var spinnerFrames = []string{ - "⠈⠁", "⠈⠑", "⠈⠱", "⠈⡱", "⢀⡱", "⢄⡱", "⢄⡱", "⢆⡱", "⢎⡱", "⢎⡰", - "⢎⡠", "⢎⡀", "⢎⠁", "⠎⠁", "⠊⠁", -} - -type Spinner struct { - stop chan struct{} - stopped chan struct{} - mu *sync.Mutex - running bool - writer io.Writer - ticker *time.Ticker - prefix string - suffix string - frameFormat string -} - -func NewSpinner(w io.Writer) *Spinner { - frameFormat := "\x1b[?7l\r%s%s%s\x1b[?7h" - if runtime.GOOS == "windows" { - frameFormat = "\r%s%s%s" - } - return &Spinner{ - stop: make(chan struct{}, 1), - stopped: make(chan struct{}), - mu: &sync.Mutex{}, - writer: w, - frameFormat: frameFormat, - } -} - -func (s *Spinner) SetPrefix(prefix string) { - s.mu.Lock() - defer s.mu.Unlock() - s.prefix = prefix -} - -func (s *Spinner) SetSuffix(suffix string) { - s.mu.Lock() - defer s.mu.Unlock() - s.suffix = suffix -} - -func (s *Spinner) Start() { - s.mu.Lock() - defer s.mu.Unlock() - if s.running { - return - } - s.running = true - s.ticker = time.NewTicker(time.Millisecond * 100) - go func() { - for { - for _, frame := range spinnerFrames { - select { - case <-s.stop: - func() { - s.mu.Lock() - defer s.mu.Unlock() - s.ticker.Stop() - s.running = false - s.stopped <- struct{}{} - }() - return - case <-s.ticker.C: - func() { - s.mu.Lock() - defer s.mu.Unlock() - fmt.Fprintf(s.writer, s.frameFormat, s.prefix, frame, s.suffix) - }() - } - } - } - }() -} - -func (s *Spinner) Stop() { - s.mu.Lock() - if !s.running { - s.mu.Unlock() - return - } - s.stop <- struct{}{} - s.mu.Unlock() - <-s.stopped -} - -func (s *Spinner) Write(p []byte) (n int, err error) { - s.mu.Lock() - defer s.mu.Unlock() - if !s.running { - return s.writer.Write(p) - } - if _, err := s.writer.Write([]byte("\r")); err != nil { - return 0, err - } - return s.writer.Write(p) } \ No newline at end of file diff --git a/pkg/common/spinner.go b/pkg/common/spinner.go new file mode 100644 index 0000000..76c8ce8 --- /dev/null +++ b/pkg/common/spinner.go @@ -0,0 +1,85 @@ +package common + +import ( + "fmt" + "io" + "os" + "runtime" + "sync" + "time" +) + +var spinnerFrames = []string{ + "⠈⠁", "⠈⠑", "⠈⠱", "⠈⡱", "⢀⡱", "⢄⡱", "⢄⡱", "⢆⡱", "⢎⡱", "⢎⡰", + "⢎⡠", "⢎⡀", "⢎⠁", "⠎⠁", "⠊⠁", +} + +type Spinner struct { + stop chan struct{} + stopped chan struct{} + mu *sync.Mutex + running bool + writer io.Writer + ticker *time.Ticker + prefix string + suffix string + frameFormat string +} + +func NewSpinner(w io.Writer) *Spinner { + frameFormat := "\x1b[?7l\r%s%s%s\x1b[?7h" + if runtime.GOOS == "windows" { + frameFormat = "\r%s%s%s" + } + return &Spinner{ + stop: make(chan struct{}, 1), + stopped: make(chan struct{}), + mu: &sync.Mutex{}, + writer: w, + frameFormat: frameFormat, + } +} + +func (s *Spinner) Start() { + s.mu.Lock() + defer s.mu.Unlock() + if s.running { + return + } + s.running = true + s.ticker = time.NewTicker(time.Millisecond * 100) + go func() { + for { + for _, frame := range spinnerFrames { + select { + case <-s.stop: + func() { + s.mu.Lock() + defer s.mu.Unlock() + s.ticker.Stop() + s.running = false + s.stopped <- struct{}{} + }() + return + case <-s.ticker.C: + func() { + s.mu.Lock() + defer s.mu.Unlock() + fmt.Fprintf(s.writer, s.frameFormat, s.prefix, frame, s.suffix) + }() + } + } + } + }() +} + +func (s *Spinner) Stop() { + s.mu.Lock() + if !s.running { + s.mu.Unlock() + return + } + s.stop <- struct{}{} + s.mu.Unlock() + <-s.stopped +} \ No newline at end of file From 4f299d5f384527dc5604fd40cd56dd638a9d68b9 Mon Sep 17 00:00:00 2001 From: shivam Date: Mon, 12 Feb 2024 00:06:02 +0530 Subject: [PATCH 3/5] used PrintfAPI --- pkg/common/args.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/common/args.go b/pkg/common/args.go index 274bdaf..ef9d601 100644 --- a/pkg/common/args.go +++ b/pkg/common/args.go @@ -44,7 +44,7 @@ func PrintInfo(clientSet *kubernetes.Clientset, config *rest.Config) { viper.Set("busybox-image", busyboxImage) } - log.printf("API endpoint : %s", config.Host) + log.PrintfAPI("API endpoint : %s", config.Host) log.Printf("Server version : %#v", *serverVersion) } From 514d711b31c66be083440966994387753e5751a5 Mon Sep 17 00:00:00 2001 From: shivam Date: Mon, 12 Feb 2024 00:14:51 +0530 Subject: [PATCH 4/5] created seperate file for spinner --- pkg/common/args.go | 2 +- pkg/common/spinner.go | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/common/args.go b/pkg/common/args.go index ef9d601..b4d6f17 100644 --- a/pkg/common/args.go +++ b/pkg/common/args.go @@ -23,7 +23,7 @@ import ( "github.com/spf13/viper" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" - + "os" "sigs.k8s.io/hydrophone/pkg/log" ) diff --git a/pkg/common/spinner.go b/pkg/common/spinner.go index 76c8ce8..88a4ab4 100644 --- a/pkg/common/spinner.go +++ b/pkg/common/spinner.go @@ -3,7 +3,6 @@ package common import ( "fmt" "io" - "os" "runtime" "sync" "time" From dd76f561922c417db45a9b46141a841a341cc983 Mon Sep 17 00:00:00 2001 From: shivam Date: Thu, 15 Feb 2024 04:59:46 +0530 Subject: [PATCH 5/5] formatted newly made files Signed-off-by: shivam --- pkg/common/args.go | 10 +++++----- pkg/common/spinner.go | 2 +- pkg/log/logs.go | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/common/args.go b/pkg/common/args.go index b4d6f17..21f21c8 100644 --- a/pkg/common/args.go +++ b/pkg/common/args.go @@ -18,20 +18,20 @@ package common import ( "fmt" - "strings" - "time" "github.com/spf13/viper" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "os" "sigs.k8s.io/hydrophone/pkg/log" + "strings" + "time" ) // PrintInfo prints the information about the cluster func PrintInfo(clientSet *kubernetes.Clientset, config *rest.Config) { spinner := NewSpinner(os.Stdout) - spinner.Start() - + spinner.Start() + time.Sleep(2 * time.Second) serverVersion, err := clientSet.ServerVersion() if err != nil { @@ -89,4 +89,4 @@ func ValidateArgs() error { } } return nil -} \ No newline at end of file +} diff --git a/pkg/common/spinner.go b/pkg/common/spinner.go index 88a4ab4..ff2b696 100644 --- a/pkg/common/spinner.go +++ b/pkg/common/spinner.go @@ -81,4 +81,4 @@ func (s *Spinner) Stop() { s.stop <- struct{}{} s.mu.Unlock() <-s.stopped -} \ No newline at end of file +} diff --git a/pkg/log/logs.go b/pkg/log/logs.go index 5992f25..829dc0b 100644 --- a/pkg/log/logs.go +++ b/pkg/log/logs.go @@ -64,4 +64,4 @@ func PrintfAPI(format string, v ...interface{}) { // Println logs an info message from the given arguments. func Println(v ...any) { slog.Info(fmt.Sprint(v...)) -} \ No newline at end of file +}