Skip to content

Commit

Permalink
feat: Basic CLI arguments to control output
Browse files Browse the repository at this point in the history
  • Loading branch information
kichik committed Jan 13, 2022
1 parent daa1b93 commit 468edf9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
20 changes: 17 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ var (
builtBy = "unknown"
)

var noColor bool = false

var rootCmd = &cobra.Command{
Use: "cloud-z",
Short: "Cloud-Z gathers information on cloud instances",
Expand Down Expand Up @@ -46,13 +48,22 @@ var rootCmd = &cobra.Command{
providers.GetMemoryInfo(report)
benchmarks.AllBenchmarks(report)

report.Print()
report.Print(noColor)

fmt.Println()

submitOrViewOrNo := ask("Would you like to anonymously contribute this data to https://z.cloudsnorkel.com/? Your IP address may be logged, but instance id and other PII will not be sent.", map[rune]string{'y': "yes", 'n': "no", 'v': "view JSON"}, 'n')
var submitOrViewOrNo rune

if b, _ := cmd.Flags().GetBool("report"); b {
submitOrViewOrNo = 'y'
} else if b, _ := cmd.Flags().GetBool("no-report"); b {
submitOrViewOrNo = 'n'
} else {
submitOrViewOrNo = ask("Would you like to anonymously contribute this data to https://z.cloudsnorkel.com/? Your IP address may be logged, but instance id and other PII will not be sent.", map[rune]string{'y': "yes", 'n': "no", 'v': "view JSON"}, 'n')
}

if submitOrViewOrNo == 'v' {
report.PrintJson()
report.PrintJson(noColor)
submitOrViewOrNo = ask("Ok to submit?", map[rune]string{'y': "yes", 'n': "no"}, 'n')
}
if submitOrViewOrNo == 'y' {
Expand All @@ -62,6 +73,9 @@ var rootCmd = &cobra.Command{
}

func Execute() {
rootCmd.Flags().BoolP("report", "r", false, "Contribute anonymous report")
rootCmd.Flags().BoolP("no-report", "n", false, "Do not contribute anonymous report")
rootCmd.PersistentFlags().BoolVar(&noColor, "no-color", false, "Do not use colors to print results")
if err := rootCmd.Execute(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand Down
48 changes: 34 additions & 14 deletions reporting/print.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package reporting

import (
"encoding/json"
"fmt"
sigar "github.com/cloudfoundry/gosigar"
"github.com/hokaccha/go-prettyjson"
Expand All @@ -17,7 +18,7 @@ func int2bytes(b int) string {
return bytesize.New(float64(b)).String()
}

func (report *Report) Print() {
func (report *Report) Print(noColor bool) {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.SetAllowedRowLength(120)
Expand All @@ -28,15 +29,17 @@ func (report *Report) Print() {
t.AppendRow(table.Row{"Availability zone", report.AvailabilityZone})
t.AppendRow(table.Row{"Instance id", report.InstanceId})
t.AppendRow(table.Row{"Image id", report.ImageId})
t.SetStyle(table.StyleColoredMagentaWhiteOnBlack)
if !noColor {
t.SetStyle(table.StyleColoredMagentaWhiteOnBlack)
}
t.Render()

report.printCPU()
report.printMemory()
report.printErrors()
report.printCPU(noColor)
report.printMemory(noColor)
report.printErrors(noColor)
}

func (report *Report) printCPU() {
func (report *Report) printCPU(noColor bool) {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.SetAllowedRowLength(120)
Expand All @@ -56,11 +59,13 @@ func (report *Report) printCPU() {
t.AppendRow(table.Row{"L3 Cache", int2bytes(cpuid.CPU.Cache.L3)})
t.AppendRow(table.Row{"Cache line", fmt.Sprintf("%v", cpuid.CPU.CacheLine)})
t.AppendRow(table.Row{"Features", text.WrapSoft(strings.Join(cpuid.CPU.FeatureSet(), ", "), 80)})
t.SetStyle(table.StyleColoredMagentaWhiteOnBlack)
if !noColor {
t.SetStyle(table.StyleColoredMagentaWhiteOnBlack)
}
t.Render()
}

func (report *Report) printMemory() {
func (report *Report) printMemory(noColor bool) {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.SetAllowedRowLength(120)
Expand All @@ -79,23 +84,38 @@ func (report *Report) printMemory() {
t.AppendRow(table.Row{stickCol, "Total width", fmt.Sprintf("%v-bit", stick.TotalWidth)}, rowConfigAutoMerge)
t.AppendRow(table.Row{stickCol, "Speed", fmt.Sprintf("%v MHz", stick.MHz)}, rowConfigAutoMerge)
}
t.SetStyle(table.StyleColoredMagentaWhiteOnBlack)
if !noColor {
t.SetStyle(table.StyleColoredMagentaWhiteOnBlack)
}
t.Render()
}

func (report *Report) printErrors() {
func (report *Report) printErrors(noColor bool) {
if len(report.Errors) == 0 {
return
}

fmt.Println(text.Bold.Sprint("\nErrors:"))
if !noColor {
fmt.Println(text.Bold.Sprint("\nErrors:"))
} else {
fmt.Println("\nErrors:")
}

for _, err := range report.Errors {
fmt.Println(text.FgRed.Sprintf(" %v", err))
if !noColor {
fmt.Println(text.FgRed.Sprintf(" %v", err))
} else {
fmt.Printf(" %v\n", err)
}
}
}

func (report *Report) PrintJson() {
result, _ := prettyjson.Marshal(report)
func (report *Report) PrintJson(noColor bool) {
var result []byte
if !noColor {
result, _ = prettyjson.Marshal(report)
} else {
result, _ = json.Marshal(report)
}
fmt.Println(string(result))
}

0 comments on commit 468edf9

Please sign in to comment.