Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
use uitable package
Browse files Browse the repository at this point in the history
  • Loading branch information
hico-horiuchi committed Feb 23, 2016
1 parent bd1f9bf commit 3b98274
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 169 deletions.
1 change: 1 addition & 0 deletions Gomfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
gom 'github.com/fatih/color'
gom 'github.com/hico-horiuchi/uitable'
gom 'github.com/mattn/go-isatty'
gom 'github.com/spf13/cobra'
gom 'github.com/tcnksm/go-latest'
Expand Down
52 changes: 28 additions & 24 deletions ohgi/aggregates.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,51 @@
package ohgi

import (
"fmt"
"strconv"
"strings"

"github.com/hico-horiuchi/ohgi/sensu"
)

func GetAggregates(api *sensu.API, limit int, offset int) string {
var line string

aggregates, err := api.GetAggregates(limit, offset)
checkError(err)

if len(aggregates) == 0 {
return "No aggregates\n"
}

print := []byte(bold("CHECK ISSUES\n"))
table := newUitable()
table.AddRow(bold("CHECK"), bold("ISSUES"))
for _, aggregate := range aggregates {
line = fillSpace(aggregate.Check, 30) + strconv.Itoa(len(aggregate.Issued)) + "\n"
print = append(print, line...)
table.AddRow(
aggregate.Check,
strconv.Itoa(len(aggregate.Issued)),
)
}

return string(print)
return fmt.Sprintln(table)
}

func GetAggregatesCheck(api *sensu.API, check string, age int) string {
var line string

issues, err := api.GetAggregatesCheck(check, age)
checkError(err)

if len(issues) == 0 {
return "No aggregates\n"
}

print := []byte(bold("TIMESTAMP ISSUED\n"))
table := newUitable()
table.AddRow(bold("TIMESTAMP"), bold("ISSUED"))
for _, issued := range issues {
line = utoa(issued) + " " + strconv.FormatInt(issued, 10) + "\n"
print = append(print, line...)
table.AddRow(
utoa(issued),
strconv.FormatInt(issued, 10),
)
}

return string(print)
return fmt.Sprintln(table)
}

func DeleteAggregatesCheck(api *sensu.API, check string) string {
Expand All @@ -53,8 +56,6 @@ func DeleteAggregatesCheck(api *sensu.API, check string) string {
}

func GetAggregatesCheckIssued(api *sensu.API, check string, issued string, summarize string, results bool) string {
var print []byte

if issued == "latest" {
issues, err := api.GetAggregatesCheck(check, -1)
checkError(err)
Expand All @@ -71,29 +72,32 @@ func GetAggregatesCheckIssued(api *sensu.API, check string, issued string, summa
aggregate, err := api.GetAggregatesCheckIssued(check, i, summarize, results)
checkError(err)

table := newUitable()
if summarize != "" {
for output, j := range aggregate.Outputs {
output = strings.Replace(output, "\n", " ", -1)
print = append(print, (bold(fillSpace(output, 50)) + strconv.Itoa(j) + "\n")...)
table.AddRow(
bold(strings.Replace(output, "\n", " ", -1)),
strconv.Itoa(j),
)
}

return string(print)
return fmt.Sprintln(table)
}

print = append(print, (bold("OK ") + fgColor(strconv.Itoa(aggregate.Ok), 0) + "\n")...)
print = append(print, (bold("WARNING ") + fgColor(strconv.Itoa(aggregate.Warning), 1) + "\n")...)
print = append(print, (bold("CRITICAL ") + fgColor(strconv.Itoa(aggregate.Critical), 2) + "\n")...)
print = append(print, (bold("UNKNOWN ") + fgColor(strconv.Itoa(aggregate.Unknown), 3) + "\n")...)
print = append(print, (bold("TOTAL ") + strconv.Itoa(aggregate.Total) + "\n")...)
table.AddRow(bold("OK:"), fgColor(strconv.Itoa(aggregate.Ok), 0))
table.AddRow(bold("WARNING:"), fgColor(strconv.Itoa(aggregate.Warning), 1))
table.AddRow(bold("CRITICAL:"), fgColor(strconv.Itoa(aggregate.Critical), 2))
table.AddRow(bold("UNKNOWN:"), fgColor(strconv.Itoa(aggregate.Unknown), 3))
table.AddRow(bold("TOTAL:"), strconv.Itoa(aggregate.Total))

if results {
clients := make([]string, len(aggregate.Results))
for j, result := range aggregate.Results {
clients[j] = result.Client
}

print = append(print, (bold("CLIENTS ") + strings.Join(clients, ", ") + "\n")...)
table.AddRow(bold("CLIENTS:"), strings.Join(clients, ", "))
}

return string(print)
return fmt.Sprintln(table)
}
46 changes: 26 additions & 20 deletions ohgi/checks.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ohgi

import (
"fmt"
"regexp"
"strconv"
"strings"
Expand All @@ -9,28 +10,30 @@ import (
)

func GetChecks(api *sensu.API) string {
var line string

checks, err := api.GetChecks()
checkError(err)

if len(checks) == 0 {
return "No checks\n"
}

print := []byte(bold("NAME COMMAND INTERVAL\n"))
table := newUitable()
table.AddRow(bold("NAME"), bold("COMMAND"), bold("INTERVAL"))
for _, check := range checks {
line = fillSpace(check.Name, 30) + fillSpace(check.Command, 60) + strconv.Itoa(check.Interval) + "\n"
print = append(print, line...)
table.AddRow(
check.Name,
check.Command,
strconv.Itoa(check.Interval),
)
}

return string(print)
return fmt.Sprintln(table)
}

func GetChecksWildcard(api *sensu.API, pattern string) string {
var match []string
var matches []int
var line string
var check sensu.CheckStruct

checks, err := api.GetChecks()
checkError(err)
Expand All @@ -47,27 +50,30 @@ func GetChecksWildcard(api *sensu.API, pattern string) string {
return "No checks that matches " + pattern + "\n"
}

print := []byte(bold("NAME COMMAND INTERVAL\n"))
table := newUitable()
table.AddRow(bold("NAME"), bold("COMMAND"), bold("INTERVAL"))
for _, i := range matches {
check := checks[i]
line = fillSpace(check.Name, 30) + fillSpace(check.Command, 60) + strconv.Itoa(check.Interval) + "\n"
print = append(print, line...)
check = checks[i]
table.AddRow(
check.Name,
check.Command,
strconv.Itoa(check.Interval),
)
}

return string(print)
return fmt.Sprintln(table)
}

func GetChecksCheck(api *sensu.API, name string) string {
var print []byte

check, err := api.GetChecksCheck(name)
checkError(err)

print = append(print, (bold("NAME ") + check.Name + "\n")...)
print = append(print, (bold("COMMAND ") + check.Command + "\n")...)
print = append(print, (bold("SUBSCRIBERS ") + strings.Join(check.Subscribers, ", ") + "\n")...)
print = append(print, (bold("INTERVAL ") + strconv.Itoa(check.Interval) + "\n")...)
print = append(print, (bold("HANDLERS ") + strings.Join(check.Handlers, ", ") + "\n")...)
table := newUitable()
table.AddRow(bold("NAME:"), check.Name)
table.AddRow(bold("COMMAND:"), check.Command)
table.AddRow(bold("SUBSCRIBERS:"), strings.Join(check.Subscribers, ", "))
table.AddRow(bold("INTERVAL:"), strconv.Itoa(check.Interval))
table.AddRow(bold("HANDLERS:"), strings.Join(check.Handlers, ", "))

return string(print)
return fmt.Sprintln(table)
}
46 changes: 26 additions & 20 deletions ohgi/clients.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
package ohgi

import (
"fmt"
"regexp"
"strings"

"github.com/hico-horiuchi/ohgi/sensu"
)

func GetClients(api *sensu.API, limit int, offset int) string {
var line string

clients, err := api.GetClients(limit, offset)
checkError(err)

if len(clients) == 0 {
return "No clients\n"
}

print := []byte(bold("NAME ADDRESS TIMESTAMP\n"))
table := newUitable()
table.AddRow(bold("NAME"), bold("ADDRESS"), bold("TIMESTAMP"))
for _, client := range clients {
line = fillSpace(client.Name, 40) + fillSpace(client.Address, 40) + utoa(client.Timestamp) + "\n"
print = append(print, line...)
table.AddRow(
client.Name,
client.Address,
client.Timestamp,
)
}

return string(print)
return fmt.Sprintln(table)
}

func GetClientsWildcard(api *sensu.API, pattern string) string {
var match []string
var matches []int
var line string
var client sensu.ClientStruct

clients, err := api.GetClients(-1, -1)
checkError(err)
Expand All @@ -46,29 +49,32 @@ func GetClientsWildcard(api *sensu.API, pattern string) string {
return "No clients that matches " + pattern + "\n"
}

print := []byte(bold("NAME ADDRESS TIMESTAMP\n"))
table := newUitable()
table.AddRow(bold("NAME"), bold("ADDRESS"), bold("TIMESTAMP"))
for _, i := range matches {
client := clients[i]
line = fillSpace(client.Name, 40) + fillSpace(client.Address, 40) + utoa(client.Timestamp) + "\n"
print = append(print, line...)
client = clients[i]
table.AddRow(
client.Name,
client.Address,
client.Timestamp,
)
}

return string(print)
return fmt.Sprintln(table)
}

func GetClientsClient(api *sensu.API, name string) string {
var print []byte

client, err := api.GetClientsClient(name)
checkError(err)

print = append(print, (bold("NAME ") + client.Name + "\n")...)
print = append(print, (bold("ADDRESS ") + client.Address + "\n")...)
print = append(print, (bold("SUBSCRIPTIONS ") + strings.Join(client.Subscriptions, ", ") + "\n")...)
print = append(print, (bold("TIMESTAMP ") + utoa(client.Timestamp) + "\n")...)
print = append(print, (bold("VERSION ") + client.Version + "\n")...)
table := newUitable()
table.AddRow(bold("NAME:"), client.Name)
table.AddRow(bold("ADDRESS:"), client.Address)
table.AddRow(bold("SUBSCRIPTIONS:"), strings.Join(client.Subscriptions, ", "))
table.AddRow(bold("TIMESTAMP:"), utoa(client.Timestamp))
table.AddRow(bold("VERSION:"), client.Version)

return string(print)
return fmt.Sprintln(table)
}

func PostClients(api *sensu.API, name string, address string, subscriptions []string) string {
Expand Down
Loading

0 comments on commit 3b98274

Please sign in to comment.