Commit 4bf8f26

Karn Wong <karn@karnwong.me>
2024-07-24 09:29:11
extract colors to single file
1 parent 2184434
cmd/color/constants.go
@@ -0,0 +1,9 @@
+package color
+
+import "github.com/fatih/color"
+
+var (
+	Blue  = color.New(color.FgBlue).SprintFunc()
+	Green = color.New(color.FgHiGreen).SprintFunc()
+	Red   = color.New(color.FgRed).SprintFunc()
+)
cmd/get/get_ip.go
@@ -8,8 +8,7 @@ import (
 	"net/http"
 	"strings"
 
-	"github.com/fatih/color"
-
+	"github.com/kahnwong/swissknife/cmd/color"
 	"github.com/spf13/cobra"
 )
 
@@ -67,20 +66,18 @@ var getIPCmd = &cobra.Command{
 	Short: "Get IP information",
 	Long:  `Get IP information`,
 	Run: func(cmd *cobra.Command, args []string) {
-		green := color.New(color.FgGreen).SprintFunc()
-
 		localIP, err := getLocalIP()
 		if err != nil {
 			fmt.Println(err)
 		} else {
-			fmt.Printf("Local IP:   %s\n", green(localIP))
+			fmt.Printf("Local IP:   %s\n", color.Green(localIP))
 		}
 
 		publicIP, err := getPublicIP()
 		if err != nil {
 			fmt.Println(err)
 		} else {
-			fmt.Printf("Public IP:  %s (%s)\n", green(publicIP.Ip), publicIP.Country)
+			fmt.Printf("Public IP:  %s (%s)\n", color.Green(publicIP.Ip), publicIP.Country)
 		}
 	},
 }
cmd/get/get_system_info.go
@@ -7,11 +7,11 @@ import (
 	"os/user"
 	"strings"
 
+	"github.com/kahnwong/swissknife/cmd/color"
 	"github.com/shirou/gopsutil/v4/disk"
 
 	"github.com/shirou/gopsutil/v4/mem"
 
-	"github.com/fatih/color"
 	"github.com/shirou/gopsutil/v4/cpu"
 	"github.com/shirou/gopsutil/v4/host"
 	"github.com/spf13/cobra"
@@ -116,20 +116,17 @@ var getSystemInfoCmd = &cobra.Command{
 		}
 
 		// format message
-		green := color.New(color.FgHiGreen).SprintFunc()
-		blue := color.New(color.FgBlue).SprintFunc()
-
 		cpuInfo := fmt.Sprintf("%s (%v)", systemInfo.CPUModelName, systemInfo.CPUThreads)
-		memoryInfo := fmt.Sprintf("%v/%v GB (%v%%)", systemInfo.MemoryUsed, systemInfo.MemoryTotal, blue(systemInfo.MemoryUsedPercent))
-		diskInfo := fmt.Sprintf("%v/%v GB (%v%%)", systemInfo.DiskUsed, systemInfo.DiskTotal, blue(systemInfo.DiskUsedPercent))
+		memoryInfo := fmt.Sprintf("%v/%v GB (%v%%)", systemInfo.MemoryUsed, systemInfo.MemoryTotal, color.Blue(systemInfo.MemoryUsedPercent))
+		diskInfo := fmt.Sprintf("%v/%v GB (%v%%)", systemInfo.DiskUsed, systemInfo.DiskTotal, color.Blue(systemInfo.DiskUsedPercent))
 
 		systemInfoStr := "" +
-			fmt.Sprintf("%s@%s\n", green(systemInfo.Username), green(systemInfo.Hostname)) +
+			fmt.Sprintf("%s@%s\n", color.Green(systemInfo.Username), color.Green(systemInfo.Hostname)) +
 			strings.Repeat("-", len(systemInfo.Username)+len(systemInfo.Hostname)+1) + "\n" +
-			fmt.Sprintf("%s:      %s\n", green("OS"), systemInfo.Platform) +
-			fmt.Sprintf("%s:     %s\n", green("CPU"), cpuInfo) +
-			fmt.Sprintf("%s:  %s\n", green("Memory"), memoryInfo) +
-			fmt.Sprintf("%s:    %s", green("Disk"), diskInfo)
+			fmt.Sprintf("%s:      %s\n", color.Green("OS"), systemInfo.Platform) +
+			fmt.Sprintf("%s:     %s\n", color.Green("CPU"), cpuInfo) +
+			fmt.Sprintf("%s:  %s\n", color.Green("Memory"), memoryInfo) +
+			fmt.Sprintf("%s:    %s", color.Green("Disk"), diskInfo)
 
 		fmt.Println(systemInfoStr)
 	},
cmd/misc/shouldideploytoday.go
@@ -8,7 +8,7 @@ import (
 	"net/http"
 	"time"
 
-	"github.com/fatih/color"
+	"github.com/kahnwong/swissknife/cmd/color"
 	"github.com/spf13/cobra"
 )
 
@@ -62,15 +62,10 @@ var ShouldIDeployTodayCmd = &cobra.Command{
 			fmt.Println(err)
 		}
 
-		// init output colors
-		green := color.New(color.FgGreen).SprintFunc()
-		red := color.New(color.FgRed).SprintFunc()
-
-		// set output colors
 		if response.ShouldIDeploy {
-			fmt.Printf("%s\n", green(response.Message))
+			fmt.Printf("%s\n", color.Green(response.Message))
 		} else if !response.ShouldIDeploy {
-			fmt.Printf("%s\n", red(response.Message))
+			fmt.Printf("%s\n", color.Red(response.Message))
 		}
 	},
 }
cmd/misc/speedtest.go
@@ -3,8 +3,7 @@ package misc
 import (
 	"fmt"
 
-	"github.com/fatih/color"
-
+	"github.com/kahnwong/swissknife/cmd/color"
 	"github.com/showwin/speedtest-go/speedtest"
 	"github.com/spf13/cobra"
 )
@@ -14,16 +13,13 @@ var SpeedTestCmd = &cobra.Command{
 	Short: "Speedtest",
 	Run: func(cmd *cobra.Command, args []string) {
 		// https://github.com/showwin/speedtest-go#api-usage
-
-		green := color.New(color.FgHiGreen).SprintFunc()
-
 		var speedtestClient = speedtest.New()
 
 		serverList, _ := speedtestClient.FetchServers()
 		targets, _ := serverList.FindServer([]int{})
 
 		for _, s := range targets {
-			fmt.Printf("%s:   %s\n", green("Server"), s.Name)
+			fmt.Printf("%s:   %s\n", color.Green("Server"), s.Name)
 
 			err := s.PingTest(nil)
 			if err != nil {
@@ -41,9 +37,9 @@ var SpeedTestCmd = &cobra.Command{
 			}
 
 			fmt.Printf("" +
-				fmt.Sprintf("%s:  %s\n", green("Latency"), s.Latency) +
-				fmt.Sprintf("%s: %s\n", green("Download"), s.DLSpeed) +
-				fmt.Sprintf("%s:   %s\n", green("Upload"), s.ULSpeed),
+				fmt.Sprintf("%s:  %s\n", color.Green("Latency"), s.Latency) +
+				fmt.Sprintf("%s: %s\n", color.Green("Download"), s.DLSpeed) +
+				fmt.Sprintf("%s:   %s\n", color.Green("Upload"), s.ULSpeed),
 			)
 
 			s.Context.Reset()