Commit d2f0439

Karn Wong <[email protected]>
2024-11-24 09:01:36
feat(listVolumes): init
1 parent 9056293
cmd/list/list.go
@@ -0,0 +1,23 @@
+package list
+
+import (
+	"os"
+
+	"github.com/rs/zerolog/log"
+
+	"github.com/spf13/cobra"
+)
+
+var Cmd = &cobra.Command{
+	Use:   "list",
+	Short: "List devices and volumes",
+	Run: func(cmd *cobra.Command, args []string) {
+		if len(args) == 0 {
+			err := cmd.Help()
+			if err != nil {
+				log.Fatal().Err(err).Msg("Failed to display help")
+			}
+			os.Exit(0)
+		}
+	},
+}
cmd/list/list_volumes.go
@@ -0,0 +1,49 @@
+package list
+
+import (
+	"fmt"
+
+	human "github.com/dustin/go-humanize"
+	"github.com/shirou/gopsutil/v4/disk"
+	"github.com/spf13/cobra"
+)
+
+func listVolumes() string {
+	// ref: <https://stackoverflow.com/a/64141403>
+	formatter := "%-14s %7s %7s %7s %4s %s\n"
+	fmt.Printf(formatter, "Filesystem", "Size", "Used", "Avail", "Use%", "Mounted on")
+
+	partitions, _ := disk.Partitions(false)
+	for _, partition := range partitions {
+		device := partition.Mountpoint
+		stats, _ := disk.Usage(device)
+
+		if stats.Total == 0 {
+			continue
+		}
+
+		percent := fmt.Sprintf("%2.f%%", stats.UsedPercent)
+
+		fmt.Printf(formatter,
+			stats.Fstype,
+			human.Bytes(stats.Total),
+			human.Bytes(stats.Used),
+			human.Bytes(stats.Free),
+			percent,
+			partition.Mountpoint,
+		)
+	}
+	return "foo"
+}
+
+var listVolumesCmd = &cobra.Command{
+	Use:   "volumes",
+	Short: "List volumes",
+	Run: func(cmd *cobra.Command, args []string) {
+		listVolumes()
+	},
+}
+
+func init() {
+	Cmd.AddCommand(listVolumesCmd)
+}
cmd/root.go
@@ -5,6 +5,7 @@ import (
 
 	"github.com/kahnwong/swissknife/cmd/generate"
 	"github.com/kahnwong/swissknife/cmd/get"
+	"github.com/kahnwong/swissknife/cmd/list"
 	"github.com/spf13/cobra"
 )
 
@@ -29,4 +30,5 @@ func Execute() {
 func init() {
 	rootCmd.AddCommand(get.Cmd)
 	rootCmd.AddCommand(generate.Cmd)
+	rootCmd.AddCommand(list.Cmd)
 }
go.mod
@@ -8,6 +8,7 @@ require (
 	github.com/charmbracelet/bubbles v0.20.0
 	github.com/charmbracelet/bubbletea v1.2.3
 	github.com/charmbracelet/lipgloss v1.0.0
+	github.com/dustin/go-humanize v1.0.1
 	github.com/fatih/color v1.18.0
 	github.com/libp2p/go-netroute v0.2.2
 	github.com/rs/zerolog v1.33.0
go.sum
@@ -20,6 +20,8 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
 github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
+github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
 github.com/ebitengine/purego v0.8.1 h1:sdRKd6plj7KYW33EH5As6YKfe8m9zbN9JMrOjNVF/BE=
 github.com/ebitengine/purego v0.8.1/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=