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
Rework cobra CLI #41
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
ghwc/vendor/ | ||
vendor/*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lolol