github.com/jaypipes/ghw@v0.21.1/cmd/ghwc/commands/cpu.go (about) 1 // 2 // Use and distribution licensed under the Apache license version 2. 3 // 4 // See the COPYING file in the root project directory for full text. 5 // 6 7 package commands 8 9 import ( 10 "fmt" 11 "math" 12 "strings" 13 14 "github.com/jaypipes/ghw" 15 "github.com/pkg/errors" 16 "github.com/spf13/cobra" 17 ) 18 19 // cpuCmd represents the install command 20 var cpuCmd = &cobra.Command{ 21 Use: "cpu", 22 Short: "Show CPU information for the host system", 23 RunE: showCPU, 24 } 25 26 // showCPU show CPU information for the host system. 27 func showCPU(cmd *cobra.Command, args []string) error { 28 cpu, err := ghw.CPU() 29 if err != nil { 30 return errors.Wrap(err, "error getting CPU info") 31 } 32 33 switch outputFormat { 34 case outputFormatHuman: 35 fmt.Printf("%v\n", cpu) 36 37 for _, proc := range cpu.Processors { 38 fmt.Printf(" %v\n", proc) 39 for _, core := range proc.Cores { 40 fmt.Printf(" %v\n", core) 41 } 42 if len(proc.Capabilities) > 0 { 43 // pretty-print the (large) block of capability strings into rows 44 // of 6 capability strings 45 rows := int(math.Ceil(float64(len(proc.Capabilities)) / float64(6))) 46 for row := 1; row < rows; row = row + 1 { 47 rowStart := (row * 6) - 1 48 rowEnd := int(math.Min(float64(rowStart+6), float64(len(proc.Capabilities)))) 49 rowElems := proc.Capabilities[rowStart:rowEnd] 50 capStr := strings.Join(rowElems, " ") 51 if row == 1 { 52 fmt.Printf(" capabilities: [%s\n", capStr) 53 } else if rowEnd < len(proc.Capabilities) { 54 fmt.Printf(" %s\n", capStr) 55 } else { 56 fmt.Printf(" %s]\n", capStr) 57 } 58 } 59 } 60 } 61 case outputFormatJSON: 62 fmt.Printf("%s\n", cpu.JSONString(pretty)) 63 case outputFormatYAML: 64 fmt.Printf("%s", cpu.YAMLString()) 65 } 66 return nil 67 } 68 69 func init() { 70 rootCmd.AddCommand(cpuCmd) 71 }