ls: add counting summaries, with the idea of creating zabbix monitor

This commit is contained in:
Joel Elkins 2024-02-18 14:16:09 -06:00
parent bf1d003c1d
commit 9e599bb956
No known key found for this signature in database
GPG Key ID: 133589DC38921AE2

View File

@ -31,6 +31,12 @@ import (
"github.com/spf13/cobra"
)
var (
lsCountAll bool
lsCountRunning bool
lsCountNotRunning bool
)
// lsCmd represents the ls command
var lsCmd = &cobra.Command{
Use: "ls",
@ -49,18 +55,42 @@ ccl ls squid`,
w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 2, ' ', 0)
defer w.Flush()
if lsCountAll {
fmt.Fprintf(w, "%d\n", len(conts))
return
}
if lsCountRunning || lsCountNotRunning {
if conn != nil {
r := 0
for _, c := range conts {
if c.IsRunning() {
r += 1
}
}
if lsCountRunning {
fmt.Fprintf(w, "%d\n", r)
} else {
fmt.Fprintf(w, "%d\n", len(conts)-r)
}
return
} else {
return
}
}
if conn != nil {
titlemsg := "CATEGORY\tGROUP\tNAME\tIMAGE\tCREATED\tRUNNING"
fmt.Fprintf(w, "%s\n", titlemsg)
for _, c := range conts {
data := []interface{}{c.Category, c.StartGroup, c.Name, c.Image}
if c.IsCreated() {
data = append(data, " ✔")
data = append(data, " ")
} else {
data = append(data, "")
}
if c.IsRunning() {
data = append(data, " ✔")
data = append(data, " ")
} else {
data = append(data, "")
}
@ -78,5 +108,9 @@ ccl ls squid`,
}
func init() {
lsCmd.Flags().BoolVarP(&lsCountAll, "count", "C", false, "Return only the count of configured items")
lsCmd.Flags().BoolVarP(&lsCountRunning, "running", "R", false, "Return only the count of running items")
lsCmd.Flags().BoolVarP(&lsCountNotRunning, "not-running", "N", false, "Return only the count of stopped/failed items")
lsCmd.MarkFlagsMutuallyExclusive("count", "running", "not-running")
rootCmd.AddCommand(lsCmd)
}