Skip to content

Commit 86d340a

Browse files
committed
Added moving spinner to context check, since it just kinda sat there
1 parent bd329c3 commit 86d340a

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

main.go

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@ import (
66
"fmt"
77
"os"
88
"path/filepath"
9+
"sync"
910
"time"
1011

1112
"github.com/alexellis/kubetrim/pkg"
1213
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1314
"k8s.io/client-go/kubernetes"
1415
_ "k8s.io/client-go/plugin/pkg/client/auth/azure"
1516
_ "k8s.io/client-go/plugin/pkg/client/auth/exec"
16-
1717
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // Import for GCP auth
1818
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc" // Import for OIDC (often used with EKS)
1919
"k8s.io/client-go/tools/clientcmd"
2020
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
21-
2221
"k8s.io/client-go/util/homedir"
2322
)
2423

@@ -28,11 +27,8 @@ var (
2827
)
2928

3029
func main() {
31-
32-
// set usage:
33-
30+
// Set usage
3431
flag.Usage = func() {
35-
3632
fmt.Printf("kubetrim (%s %s) Copyright Alex Ellis (c) 2024\n\n", pkg.Version, pkg.GitCommit)
3733
fmt.Print("Sponsor Alex on GitHub: https://github.com/sponsors/alexellis\n\n")
3834
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
@@ -63,6 +59,16 @@ func main() {
6359

6460
fmt.Printf("kubetrim (%s %s) by Alex Ellis \n\nLoaded: %s. Checking..\n", pkg.Version, pkg.GitCommit, kubeconfig)
6561

62+
// Start the spinner in a goroutine
63+
var wg sync.WaitGroup
64+
stopSpinner := make(chan struct{})
65+
wg.Add(1)
66+
67+
go func() {
68+
defer wg.Done()
69+
spinner(stopSpinner)
70+
}()
71+
6672
st := time.Now()
6773
// List of contexts to be deleted
6874
var contextsToDelete []string
@@ -94,17 +100,21 @@ func main() {
94100
fmt.Printf("❌ - (%v)\n", err)
95101
contextsToDelete = append(contextsToDelete, contextName)
96102
} else {
103+
fmt.Println("/n")
97104
fmt.Println("✅")
98105
}
99106
}
100107

108+
// Stop the spinner
109+
close(stopSpinner)
110+
wg.Wait()
111+
101112
// Delete the contexts that are not working
102113
for _, contextName := range contextsToDelete {
103114
deleteContextAndCluster(config, contextName)
104115
}
105116

106117
if writeFile {
107-
108118
if len(contextsToDelete) == len(config.Contexts) && !force {
109119
fmt.Println("No contexts are working, the Internet may be down, use --force to delete all contexts anyway.")
110120
os.Exit(1)
@@ -116,7 +126,25 @@ func main() {
116126
os.Exit(1)
117127
}
118128
}
119-
fmt.Printf("Updated: %s (in %s).\n", kubeconfig, time.Since(st).Round(time.Millisecond))
129+
fmt.Printf("\n Updated: %s (in %s).\n", kubeconfig, time.Since(st).Round(time.Millisecond))
130+
}
131+
}
132+
133+
// spinner runs a visual timer in the terminal
134+
func spinner(stop <-chan struct{}) {
135+
chars := []rune{'|', '/', '-', '\\'}
136+
for {
137+
for _, char := range chars {
138+
select {
139+
case <-stop:
140+
fmt.Print("\r") // Clear spinner line
141+
return
142+
default:
143+
fmt.Printf("")
144+
fmt.Printf("\r%c Checking ... ", char)
145+
time.Sleep(100 * time.Millisecond)
146+
}
147+
}
120148
}
121149
}
122150

@@ -131,7 +159,6 @@ func checkCluster(clientset *kubernetes.Clientset) error {
131159

132160
// deleteContextAndCluster removes the context and its associated cluster from the config
133161
func deleteContextAndCluster(config *clientcmdapi.Config, contextName string) {
134-
135162
// Get the cluster name associated with the context
136163
clusterName := config.Contexts[contextName].Cluster
137164

0 commit comments

Comments
 (0)