diff --git a/ghwc/cmd/block.go b/ghwc/cmd/block.go new file mode 100644 index 00000000..f967f344 --- /dev/null +++ b/ghwc/cmd/block.go @@ -0,0 +1,38 @@ +// +// Use and distribution licensed under the Apache license version 2. +// +// See the COPYING file in the root project directory for full text. +// + +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// blockCmd represents the install command +var blockCmd = &cobra.Command{ + Use: "block", + Short: "Show block storage information for the host system", + RunE: showBlock, +} + +// showBlock show block storage information for the host system. +func showBlock(cmd *cobra.Command, args []string) error { + block := info.Block + fmt.Printf("%v\n", block) + + for _, disk := range block.Disks { + fmt.Printf(" %v\n", disk) + for _, part := range disk.Partitions { + fmt.Printf(" %v\n", part) + } + } + return nil +} + +func init() { + rootCmd.AddCommand(blockCmd) +} diff --git a/ghwc/cmd/cpu.go b/ghwc/cmd/cpu.go new file mode 100644 index 00000000..6b0afd92 --- /dev/null +++ b/ghwc/cmd/cpu.go @@ -0,0 +1,58 @@ +// +// Use and distribution licensed under the Apache license version 2. +// +// See the COPYING file in the root project directory for full text. +// + +package cmd + +import ( + "fmt" + "math" + "strings" + + "github.com/spf13/cobra" +) + +// cpuCmd represents the install command +var cpuCmd = &cobra.Command{ + Use: "cpu", + Short: "Show CPU information for the host system", + RunE: showCPU, +} + +// showCPU show CPU information for the host system. +func showCPU(cmd *cobra.Command, args []string) error { + cpu := info.CPU + fmt.Printf("%v\n", cpu) + + for _, proc := range cpu.Processors { + fmt.Printf(" %v\n", proc) + for _, core := range proc.Cores { + fmt.Printf(" %v\n", core) + } + if len(proc.Capabilities) > 0 { + // pretty-print the (large) block of capability strings into rows + // of 6 capability strings + rows := int(math.Ceil(float64(len(proc.Capabilities)) / float64(6))) + for row := 1; row < rows; row = row + 1 { + rowStart := (row * 6) - 1 + rowEnd := int(math.Min(float64(rowStart+6), float64(len(proc.Capabilities)))) + rowElems := proc.Capabilities[rowStart:rowEnd] + capStr := strings.Join(rowElems, " ") + if row == 1 { + fmt.Printf(" capabilities: [%s\n", capStr) + } else if rowEnd < len(proc.Capabilities) { + fmt.Printf(" %s\n", capStr) + } else { + fmt.Printf(" %s]\n", capStr) + } + } + } + } + return nil +} + +func init() { + rootCmd.AddCommand(cpuCmd) +} diff --git a/ghwc/cmd/gpu.go b/ghwc/cmd/gpu.go new file mode 100644 index 00000000..6ad7598b --- /dev/null +++ b/ghwc/cmd/gpu.go @@ -0,0 +1,35 @@ +// +// Use and distribution licensed under the Apache license version 2. +// +// See the COPYING file in the root project directory for full text. +// + +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// gpuCmd represents the install command +var gpuCmd = &cobra.Command{ + Use: "gpu", + Short: "Show graphics/GPU information for the host system", + RunE: showGPU, +} + +// showGPU show graphics/GPU information for the host system. +func showGPU(cmd *cobra.Command, args []string) error { + gpu := info.GPU + fmt.Printf("%v\n", gpu) + + for _, card := range gpu.GraphicsCards { + fmt.Printf(" %v\n", card) + } + return nil +} + +func init() { + rootCmd.AddCommand(gpuCmd) +} diff --git a/ghwc/cmd/memory.go b/ghwc/cmd/memory.go new file mode 100644 index 00000000..032029dd --- /dev/null +++ b/ghwc/cmd/memory.go @@ -0,0 +1,31 @@ +// +// Use and distribution licensed under the Apache license version 2. +// +// See the COPYING file in the root project directory for full text. +// + +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// memoryCmd represents the install command +var memoryCmd = &cobra.Command{ + Use: "memory", + Short: "Show memory information for the host system", + RunE: showMemory, +} + +// showMemory show memory information for the host system. +func showMemory(cmd *cobra.Command, args []string) error { + mem := info.Memory + fmt.Printf("%v\n", mem) + return nil +} + +func init() { + rootCmd.AddCommand(memoryCmd) +} diff --git a/ghwc/cmd/net.go b/ghwc/cmd/net.go new file mode 100644 index 00000000..ab389dce --- /dev/null +++ b/ghwc/cmd/net.go @@ -0,0 +1,35 @@ +// +// Use and distribution licensed under the Apache license version 2. +// +// See the COPYING file in the root project directory for full text. +// + +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// netCmd represents the install command +var netCmd = &cobra.Command{ + Use: "net", + Short: "Show network information for the host system", + RunE: showNetwork, +} + +// showNetwork show network information for the host system. +func showNetwork(cmd *cobra.Command, args []string) error { + net := info.Network + fmt.Printf("%v\n", net) + + for _, nic := range net.NICs { + fmt.Printf(" %v\n", nic) + } + return nil +} + +func init() { + rootCmd.AddCommand(netCmd) +} diff --git a/ghwc/cmd/root.go b/ghwc/cmd/root.go new file mode 100644 index 00000000..d085f0b8 --- /dev/null +++ b/ghwc/cmd/root.go @@ -0,0 +1,93 @@ +// +// Use and distribution licensed under the Apache license version 2. +// +// See the COPYING file in the root project directory for full text. +// + +package cmd + +import ( + "fmt" + "os" + + "github.com/jaypipes/ghw" + "github.com/spf13/cobra" +) + +var ( + version string + buildHash string + buildDate string + debug bool + info *ghw.HostInfo +) + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "ghwc", + Short: "ghwc - Discover hardware information.", + Long: ` + __ + .-----. | |--. .--.--.--. + | _ | | | | | | | + |___ | |__|__| |________| + |_____| + +Discover hardware information. + +https://github.com/jaypipes/ghw +`, + RunE: showAll, +} + +func showAll(cmd *cobra.Command, args []string) error { + err := showMemory(cmd, args) + if err != nil { + return err + } + err = showCPU(cmd, args) + if err != nil { + return err + } + err = showBlock(cmd, args) + if err != nil { + return err + } + err = showTopology(cmd, args) + if err != nil { + return err + } + err = showNetwork(cmd, args) + if err != nil { + return err + } + err = showGPU(cmd, args) + if err != nil { + return err + } + return nil +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute(v string, bh string, bd string) { + version = v + buildHash = bh + buildDate = bd + + i, err := ghw.Host() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + info = i + + if err := rootCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) + } +} + +func init() { + rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "Enable or disable debug mode") +} diff --git a/ghwc/cmd/topology.go b/ghwc/cmd/topology.go new file mode 100644 index 00000000..7064a601 --- /dev/null +++ b/ghwc/cmd/topology.go @@ -0,0 +1,38 @@ +// +// Use and distribution licensed under the Apache license version 2. +// +// See the COPYING file in the root project directory for full text. +// + +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// topologyCmd represents the install command +var topologyCmd = &cobra.Command{ + Use: "topology", + Short: "Show topology information for the host system", + RunE: showTopology, +} + +// showTopology show topology information for the host system. +func showTopology(cmd *cobra.Command, args []string) error { + topology := info.Topology + fmt.Printf("%v\n", topology) + + for _, node := range topology.Nodes { + fmt.Printf(" %v\n", node) + for _, cache := range node.Caches { + fmt.Printf(" %v\n", cache) + } + } + return nil +} + +func init() { + rootCmd.AddCommand(topologyCmd) +} diff --git a/ghwc/cmd/version.go b/ghwc/cmd/version.go new file mode 100644 index 00000000..3c4de2bf --- /dev/null +++ b/ghwc/cmd/version.go @@ -0,0 +1,35 @@ +// +// Use and distribution licensed under the Apache license version 2. +// +// See the COPYING file in the root project directory for full text. +// + +package cmd + +import ( + "fmt" + "runtime" + + "github.com/spf13/cobra" +) + +const debugHeader = ` +Date: %s +Build: %s +Version: %s +Git Hash: %s +` + +// versionCmd represents the version command +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Display the version of gofile", + Run: func(cmd *cobra.Command, args []string) { + goVersion := fmt.Sprintf("%s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH) + fmt.Printf(debugHeader, buildDate, goVersion, version, buildHash) + }, +} + +func init() { + rootCmd.AddCommand(versionCmd) +} diff --git a/ghwc/main.go b/ghwc/main.go index f34651d4..8b849700 100644 --- a/ghwc/main.go +++ b/ghwc/main.go @@ -1,194 +1,24 @@ +// +// Use and distribution licensed under the Apache license version 2. +// +// See the COPYING file in the root project directory for full text. +// + package main import ( - "fmt" - "math" - "os" - "strings" - - "github.com/spf13/cobra" - - "github.com/jaypipes/ghw" + "github.com/jaypipes/ghw/ghwc/cmd" ) var ( - info *ghw.HostInfo + // version of application at compile time (-X 'main.version=$(VERSION)'). + version = "(Unknown Version)" + // buildHash GIT hash of application at compile time (-X 'main.buildHash=$(GITCOMMIT)'). + buildHash = "No Git-hash Provided." + // buildDate of application at compile time (-X 'main.buildDate=$(BUILDDATE)'). + buildDate = "No Build Date Provided." ) func main() { - i, err := ghw.Host() - if err != nil { - fmt.Println(err) - os.Exit(1) - } - info = i - err = rootCommand.Execute() - if err != nil { - fmt.Println(err) - os.Exit(1) - } -} - -func init() { - rootCommand.AddCommand(memoryCommand) - rootCommand.AddCommand(cpuCommand) - rootCommand.AddCommand(blockCommand) - rootCommand.AddCommand(topologyCommand) - rootCommand.AddCommand(netCommand) - rootCommand.AddCommand(gpuCommand) - rootCommand.SilenceUsage = true -} - -var rootCommand = &cobra.Command{ - Use: "ghwc", - Short: "ghwc - Discover hardware information.", - Long: "ghwc - Discover hardware information.", - RunE: showAll, -} - -func showAll(cmd *cobra.Command, args []string) error { - err := showMemory(cmd, args) - if err != nil { - return err - } - err = showCPU(cmd, args) - if err != nil { - return err - } - err = showBlock(cmd, args) - if err != nil { - return err - } - err = showTopology(cmd, args) - if err != nil { - return err - } - err = showNetwork(cmd, args) - if err != nil { - return err - } - err = showGPU(cmd, args) - if err != nil { - return err - } - return nil -} - -var memoryCommand = &cobra.Command{ - Use: "memory", - Short: "Show memory information for the host system", - RunE: showMemory, -} - -func showMemory(cmd *cobra.Command, args []string) error { - mem := info.Memory - fmt.Printf("%v\n", mem) - return nil -} - -var cpuCommand = &cobra.Command{ - Use: "cpu", - Short: "Show CPU information for the host system", - RunE: showCPU, -} - -func showCPU(cmd *cobra.Command, args []string) error { - cpu := info.CPU - fmt.Printf("%v\n", cpu) - - for _, proc := range cpu.Processors { - fmt.Printf(" %v\n", proc) - for _, core := range proc.Cores { - fmt.Printf(" %v\n", core) - } - if len(proc.Capabilities) > 0 { - // pretty-print the (large) block of capability strings into rows - // of 6 capability strings - rows := int(math.Ceil(float64(len(proc.Capabilities)) / float64(6))) - for row := 1; row < rows; row = row + 1 { - rowStart := (row * 6) - 1 - rowEnd := int(math.Min(float64(rowStart+6), float64(len(proc.Capabilities)))) - rowElems := proc.Capabilities[rowStart:rowEnd] - capStr := strings.Join(rowElems, " ") - if row == 1 { - fmt.Printf(" capabilities: [%s\n", capStr) - } else if rowEnd < len(proc.Capabilities) { - fmt.Printf(" %s\n", capStr) - } else { - fmt.Printf(" %s]\n", capStr) - } - } - } - } - return nil -} - -var blockCommand = &cobra.Command{ - Use: "block", - Short: "Show block storage information for the host system", - RunE: showBlock, -} - -func showBlock(cmd *cobra.Command, args []string) error { - block := info.Block - fmt.Printf("%v\n", block) - - for _, disk := range block.Disks { - fmt.Printf(" %v\n", disk) - for _, part := range disk.Partitions { - fmt.Printf(" %v\n", part) - } - } - return nil -} - -var topologyCommand = &cobra.Command{ - Use: "topology", - Short: "Show topology information for the host system", - RunE: showTopology, -} - -func showTopology(cmd *cobra.Command, args []string) error { - topology := info.Topology - fmt.Printf("%v\n", topology) - - for _, node := range topology.Nodes { - fmt.Printf(" %v\n", node) - for _, cache := range node.Caches { - fmt.Printf(" %v\n", cache) - } - } - return nil -} - -var netCommand = &cobra.Command{ - Use: "net", - Short: "Show network information for the host system", - RunE: showNetwork, -} - -func showNetwork(cmd *cobra.Command, args []string) error { - net := info.Network - fmt.Printf("%v\n", net) - - for _, nic := range net.NICs { - fmt.Printf(" %v\n", nic) - } - return nil -} - -var gpuCommand = &cobra.Command{ - Use: "gpu", - Short: "Show graphics/GPU information for the host system", - RunE: showGPU, -} - -func showGPU(cmd *cobra.Command, args []string) error { - gpu := info.GPU - fmt.Printf("%v\n", gpu) - - for _, card := range gpu.GraphicsCards { - fmt.Printf(" %v\n", card) - } - return nil + cmd.Execute(version, buildHash, buildDate) }