Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework cobra CLI #41

Merged
merged 4 commits into from Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
@@ -1 +1 @@
ghwc/vendor/
vendor/*/
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -11,7 +11,7 @@ $(GOVENDOR):
go get -u github.com/kardianos/govendor

ghwc/vendor/vendor.json: $(GOVENDOR)
cd ghwc; govendor sync
govendor sync

$(GOMETALINTER):
go get -u github.com/alecthomas/gometalinter
Expand Down
38 changes: 38 additions & 0 deletions 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)
}
58 changes: 58 additions & 0 deletions 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)
}
35 changes: 35 additions & 0 deletions 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)
}
31 changes: 31 additions & 0 deletions 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)
}
35 changes: 35 additions & 0 deletions 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)
}
93 changes: 93 additions & 0 deletions 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: `
__
.-----. | |--. .--.--.--.
| _ | | | | | | |
|___ | |__|__| |________|
|_____|
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol, this makes me think of someone giving someone else the middle finger.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lolol


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")
}
38 changes: 38 additions & 0 deletions 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)
}
35 changes: 35 additions & 0 deletions 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{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. I'd wanted to do something like this. Thanks for adding it! :)

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)
}