mirror of
https://gitea.elkins.co/Networking/ccl.git
synced 2025-03-09 12:41:40 -05:00
Add json output flag for ls subcommand: ccl ls -J
This commit is contained in:
parent
04e5c44565
commit
ab92b45ba0
50
cmd/ls.go
50
cmd/ls.go
@ -24,6 +24,7 @@ THE SOFTWARE.
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"text/tabwriter"
|
||||
|
||||
@ -35,8 +36,18 @@ var (
|
||||
lsCountAll bool
|
||||
lsCountRunning bool
|
||||
lsCountNotRunning bool
|
||||
lsJsonFormat bool
|
||||
)
|
||||
|
||||
type lsContainerObj struct {
|
||||
Category string `json:"category"`
|
||||
StartGroup int `json:"group"`
|
||||
Name string `json:"name"`
|
||||
Image string `json:"image"`
|
||||
Created bool `json:"created"`
|
||||
Running bool `json:"running"`
|
||||
}
|
||||
|
||||
// lsCmd represents the ls command
|
||||
var lsCmd = &cobra.Command{
|
||||
Use: "ls",
|
||||
@ -52,8 +63,7 @@ ccl ls default sub # multiple ok
|
||||
ccl ls squid`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
conts := config.Union(args, contMask)
|
||||
w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 2, ' ', 0)
|
||||
defer w.Flush()
|
||||
w := cmd.OutOrStdout()
|
||||
|
||||
if lsCountAll {
|
||||
fmt.Fprintf(w, "%d\n", len(conts))
|
||||
@ -79,9 +89,36 @@ ccl ls squid`,
|
||||
}
|
||||
}
|
||||
|
||||
if lsJsonFormat {
|
||||
out := make([]lsContainerObj, 0)
|
||||
for _, c := range conts {
|
||||
run, cre := false, false
|
||||
if conn != nil {
|
||||
run, cre = c.IsRunning(), c.IsCreated()
|
||||
}
|
||||
out = append(out, lsContainerObj{
|
||||
Category: c.Category,
|
||||
StartGroup: c.StartGroup,
|
||||
Name: c.Name,
|
||||
Image: c.Image,
|
||||
Running: run,
|
||||
Created: cre,
|
||||
})
|
||||
}
|
||||
val, err := json.Marshal(out)
|
||||
if err != nil {
|
||||
fmt.Fprintf(cmd.OutOrStderr(), "Error marshalling results: %s", err)
|
||||
}
|
||||
fmt.Fprintf(w, string(val))
|
||||
return
|
||||
}
|
||||
|
||||
tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
|
||||
defer tw.Flush()
|
||||
|
||||
if conn != nil {
|
||||
titlemsg := "CATEGORY\tGROUP\tNAME\tIMAGE\tCREATED\tRUNNING"
|
||||
fmt.Fprintf(w, "%s\n", titlemsg)
|
||||
fmt.Fprintf(tw, "%s\n", titlemsg)
|
||||
for _, c := range conts {
|
||||
data := []interface{}{c.Category, c.StartGroup, c.Name, c.Image}
|
||||
if c.IsCreated() {
|
||||
@ -94,14 +131,14 @@ ccl ls squid`,
|
||||
} else {
|
||||
data = append(data, "")
|
||||
}
|
||||
fmt.Fprintf(w, "%s\t%5d\t%s\t%s\t%s\t%s\n", data...)
|
||||
fmt.Fprintf(tw, "%s\t%5d\t%s\t%s\t%s\t%s\n", data...)
|
||||
}
|
||||
} else {
|
||||
titlemsg := "CATEGORY\tGROUP\tNAME\tIMAGE"
|
||||
fmt.Fprintf(w, "%s\n", titlemsg)
|
||||
fmt.Fprintf(tw, "%s\n", titlemsg)
|
||||
for _, c := range conts {
|
||||
data := []interface{}{c.Category, c.StartGroup, c.Name, c.Image}
|
||||
fmt.Fprintf(w, "%s\t%5d\t%s\t%s\n", data...)
|
||||
fmt.Fprintf(tw, "%s\t%5d\t%s\t%s\n", data...)
|
||||
}
|
||||
}
|
||||
},
|
||||
@ -111,6 +148,7 @@ 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.Flags().BoolVarP(&lsJsonFormat, "json", "J", false, "Output results as a json array")
|
||||
lsCmd.MarkFlagsMutuallyExclusive("count", "running", "not-running")
|
||||
rootCmd.AddCommand(lsCmd)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user