mirror of
https://gitea.elkins.co/Networking/ccl.git
synced 2025-03-10 05:01:38 -05:00
155 lines
4.4 KiB
Go
155 lines
4.4 KiB
Go
/*
|
|
Package cmd defines user commands accessible from the cli tool.
|
|
|
|
Copyright © 2022 Joel D. Elkins <joel@elkins.co>
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
package cmd
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"text/tabwriter"
|
|
|
|
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
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",
|
|
Aliases: []string{"list"},
|
|
Short: "List configured containers",
|
|
Args: cobra.OnlyValidArgs,
|
|
ValidArgsFunction: validNouns,
|
|
Long: `List configured containers. You can provide one or more names
|
|
or categories to limit the list.`,
|
|
Example: `ccl ls # same as below
|
|
ccl ls all # same as above
|
|
ccl ls default sub # multiple ok
|
|
ccl ls squid`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
conts := config.Union(args, contMask)
|
|
w := cmd.OutOrStdout()
|
|
|
|
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 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(tw, "%s\n", titlemsg)
|
|
for _, c := range conts {
|
|
data := []interface{}{c.Category, c.StartGroup, c.Name, c.Image}
|
|
if c.IsCreated() {
|
|
data = append(data, " ✓")
|
|
} else {
|
|
data = append(data, "")
|
|
}
|
|
if c.IsRunning() {
|
|
data = append(data, " ✓")
|
|
} else {
|
|
data = append(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(tw, "%s\n", titlemsg)
|
|
for _, c := range conts {
|
|
data := []interface{}{c.Category, c.StartGroup, c.Name, c.Image}
|
|
fmt.Fprintf(tw, "%s\t%5d\t%s\t%s\n", data...)
|
|
}
|
|
}
|
|
},
|
|
}
|
|
|
|
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)
|
|
}
|