Skip to content

Commit

Permalink
perf: improve spinner when updating
Browse files Browse the repository at this point in the history
style: code-gardening
  • Loading branch information
rogosprojects committed Aug 11, 2024
1 parent d0a7767 commit 165e5f3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 34 deletions.
62 changes: 34 additions & 28 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ var (
defaultLogPath = "logs/" + time.Now().Format("2006-01-02T15:04")
)

// NotifyReadSize notify some bytes have been read
type NotifyReadSize func(total int, delta int)

// MeteredReader decorate Reader to measure reads
type MeteredReader struct {
reader io.Reader
total int
notify NotifyReadSize
}

// notify progress through specified function
func (w *MeteredReader) Read(p []byte) (int, error) {
size, err := w.reader.Read(p)
w.total += size
w.notify(w.total, size)
return size, err
}

// splashScreen prints the splash screen!
func splashScreen() {

Expand All @@ -57,6 +75,7 @@ func splashScreen() {
pterm.DefaultParagraph.Printfln("Version: %s", BuildVersion)
}

// configClient creates a new Kubernetes client
func configClient() {

// use the current context in kubeconfig
Expand All @@ -72,6 +91,7 @@ func configClient() {
}
}

// configNamespace configures the namespace to use
func configNamespace() {
if *namespace == "" {
*namespace = getCurrentNamespace(*kubeconfig)
Expand All @@ -87,6 +107,7 @@ func configNamespace() {
pterm.Info.Printfln("Using Namespace: %s", pterm.Green(*namespace))
}

// listNamespaces lists all namespaces in the cluster
func listNamespaces() {
// get namespaces and prompt user to select one
namespaces, err := client.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
Expand All @@ -106,6 +127,7 @@ func listNamespaces() {
Show()
}

// listAllPods lists all the ready pods in the namespace
func listAllPods() v1.PodList {
var _podList v1.PodList
pods, err := client.CoreV1().Pods(*namespace).List(context.TODO(), metav1.ListOptions{})
Expand Down Expand Up @@ -146,6 +168,7 @@ func listAllPods() v1.PodList {
return _podList
}

// showInteractivePodSelect shows an interactive multiselect printer with the pod names
func showInteractivePodSelect(podNames []string) []string {
// Create a new interactive multiselect printer with the options
// Disable the filter and set the keys for confirming and selecting options
Expand Down Expand Up @@ -179,6 +202,7 @@ func getCurrentNamespace(kubeconfig string) string {
return ns
}

// getLopOpts returns the Kubernetes option for logs
func getLopOpts() v1.PodLogOptions {
var logOpts v1.PodLogOptions
// Since
Expand All @@ -201,6 +225,7 @@ func getLopOpts() v1.PodLogOptions {
return logOpts
}

// getPodLogs gets logs for the pods
func getPodLogs(pods v1.PodList, logOpts v1.PodLogOptions) {
var wg sync.WaitGroup
// Create a multi printer for managing multiple printers
Expand Down Expand Up @@ -236,6 +261,7 @@ func getPodLogs(pods v1.PodList, logOpts v1.PodLogOptions) {
wg.Wait()
}

// streamLog streams logs for the container
func streamLog(pod v1.Pod, container v1.Container, logOpts v1.PodLogOptions, wg *sync.WaitGroup, multiPrinter *pterm.MultiPrinter) {
defer wg.Done()

Expand All @@ -255,6 +281,7 @@ func streamLog(pod v1.Pod, container v1.Container, logOpts v1.PodLogOptions, wg

}

// findPodByLabel finds pods by label
func findPodByLabel(label string) v1.PodList {
pterm.Info.Printf("Getting Pods in namespace %s with label %s\n\n", pterm.Green(*namespace), pterm.Green(label))

Expand All @@ -277,6 +304,7 @@ func findPodByLabel(label string) v1.PodList {
return *pods
}

// writeLogToDisk writes logs to disk
func writeLogToDisk(logs io.ReadCloser, podName string, containerName string, multiPrinter *pterm.MultiPrinter) {
anyLogFound = true

Expand Down Expand Up @@ -306,25 +334,21 @@ func writeLogToDisk(logs io.ReadCloser, podName string, containerName string, mu
}
}(logFile)

var spinnerMsg string
if *follow {
spinnerMsg = "Streaming logs..."
} else {
spinnerMsg = "Acquiring logs..."
}
spinner1, _ := pterm.DefaultSpinner.WithWriter(multiPrinter.NewWriter()).Start(spinnerMsg)
defer spinner1.Stop()
spinnerLog, _ := pterm.DefaultSpinner.WithWriter(multiPrinter.NewWriter()).
WithRemoveWhenDone(false).Start("Acquiring logs...")
defer spinnerLog.Stop()

reader := &MeteredReader{reader: bufio.NewReader(logs), notify: func(total, delta int) {
s := pterm.Style{pterm.FgWhite, pterm.BgDefault, pterm.Bold, pterm.Italic}

spinner1.Text = pterm.Info.WithPrefix(
spinnerLog.UpdateText(pterm.Info.WithPrefix(
pterm.Prefix{
Text: convertBytes(total),
Style: &s,
}).
WithMessageStyle(&s).
Sprintf("%s/%s", podName, containerName)
Sprintf("%s/%s", podName, containerName))

}}

// Create a buffered reader and writer
Expand Down Expand Up @@ -408,21 +432,3 @@ func init() {
pterm.Fatal.Printfln("Kubeconfig not found, please provide a kubeconfig file with --kubeconfig")
}
}

// notify some bytes have been read
type NotifyReadSize func(total int, delta int)

// decorate Reader to measure reads
type MeteredReader struct {
reader io.Reader
total int
notify NotifyReadSize
}

// notify progress through specified function
func (w *MeteredReader) Read(p []byte) (int, error) {
size, err := w.reader.Read(p)
w.total += size
w.notify(w.total, size)
return size, err
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
Expand Down
9 changes: 5 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ github.com/pterm/pterm v0.12.40/go.mod h1:ffwPLwlbXxP+rxT0GsgDTzS3y3rmpAO1NMjUkG
github.com/pterm/pterm v0.12.79 h1:lH3yrYMhdpeqX9y5Ep1u7DejyHy7NSQg9qrBjF9dFT4=
github.com/pterm/pterm v0.12.79/go.mod h1:1v/gzOF1N0FsjbgTHZ1wVycRkKiatFvJSJC4IGaQAAo=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down Expand Up @@ -177,8 +177,9 @@ golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down

0 comments on commit 165e5f3

Please sign in to comment.