From ddd391c8b0091693f4686a77ca077802187bfb35 Mon Sep 17 00:00:00 2001 From: "Joel D. Elkins" Date: Wed, 27 Jul 2022 21:48:47 -0500 Subject: [PATCH] Mask disabled category from action by default --- cmd/create.go | 2 +- cmd/ls.go | 2 +- cmd/nouns.go | 2 +- cmd/pull.go | 2 +- cmd/recreate.go | 2 +- cmd/restart.go | 2 +- cmd/rm.go | 2 +- cmd/root.go | 18 ++++++++++++++---- cmd/show.go | 8 ++++---- cmd/start.go | 2 +- cmd/stop.go | 2 +- cmd/update.go | 2 +- internal/pkg/config/config.go | 33 ++++++++++++++++++++++++++------- 13 files changed, 54 insertions(+), 25 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index 6c0040a..a9fcde5 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -37,7 +37,7 @@ var createCmd = &cobra.Command{ Long: `Create containers specified by the arguments. Arguments can be either container names or categories. Multiple arguments are supported.`, Run: func(_ *cobra.Command, args []string) { - conts := config.Union(args) + conts := config.Union(args, contMask) for _, cont := range conts { fmt.Fprintln(output, "CREATE", cont.Name) for _, c := range cont.CreateCommands() { diff --git a/cmd/ls.go b/cmd/ls.go index 773b12b..28b69be 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -43,7 +43,7 @@ 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) + conts := config.Union(args, contMask) w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 2, ' ', 0) defer w.Flush() diff --git a/cmd/nouns.go b/cmd/nouns.go index 6462569..7459916 100644 --- a/cmd/nouns.go +++ b/cmd/nouns.go @@ -28,7 +28,7 @@ import ( func validNouns(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) { validArgs := []string{} - for _, c := range config.Union([]string{}) { + for _, c := range config.Containers { validArgs = append(validArgs, c.Name) } validArgs = append(validArgs, config.Categories()...) diff --git a/cmd/pull.go b/cmd/pull.go index 992a97c..3f58801 100644 --- a/cmd/pull.go +++ b/cmd/pull.go @@ -38,7 +38,7 @@ var pullCmd = &cobra.Command{ affected: the old image will still remain, though untagged, and any defined containers will still use it.`, Run: func(cmd *cobra.Command, args []string) { - conts := config.Union(args) + conts := config.Union(args, contMask) for _, cont := range conts { fmt.Fprintln(output, "PULL", cont.Name) for _, cmd := range cont.PullCommands() { diff --git a/cmd/recreate.go b/cmd/recreate.go index 0ce8da0..ec012d7 100644 --- a/cmd/recreate.go +++ b/cmd/recreate.go @@ -37,7 +37,7 @@ var recreateCmd = &cobra.Command{ Long: `Recreate container images, stopping and restarting if necessary. Arguments can be one or more container names or categories. If empty, "all" is assumed.`, Run: func(_ *cobra.Command, args []string) { - conts := config.Union(args) + conts := config.Union(args, contMask) for _, cont := range conts { fmt.Fprintln(output, "RECREATE", cont.Name) for _, c := range cont.RecreateCommands() { diff --git a/cmd/restart.go b/cmd/restart.go index 877de4c..a63655d 100644 --- a/cmd/restart.go +++ b/cmd/restart.go @@ -37,7 +37,7 @@ var restartCmd = &cobra.Command{ Long: `Stop configured containers (if running), then restart them. Arguments can be one or more container names or categories. If empty, "all" is assumed.`, Run: func(_ *cobra.Command, args []string) { - conts := config.Union(args) + conts := config.Union(args, contMask) for _, cont := range conts { fmt.Fprintln(output, "RESTART", cont.Name) for _, c := range cont.RestartCommands() { diff --git a/cmd/rm.go b/cmd/rm.go index ddab8e4..4222684 100644 --- a/cmd/rm.go +++ b/cmd/rm.go @@ -38,7 +38,7 @@ var rmCmd = &cobra.Command{ Long: `Remove defined containers or categories if they have been created. If running, they will first be stopped.`, Run: func(cmd *cobra.Command, args []string) { - conts := config.Union(args) + conts := config.Union(args, contMask) for _, cont := range conts { fmt.Fprintln(output, "REMOVE", cont.Name) for _, cmd := range cont.DestroyCommands() { diff --git a/cmd/root.go b/cmd/root.go index c4e36e0..b9f752a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -42,13 +42,22 @@ to define, start, stop, or update the container images. Configuration is read from a toml configuration file, and the utility uses this information to execute the necessary podman commands.`, ValidArgsFunction: cobra.NoFileCompletions, + PersistentPreRun: func(*cobra.Command, []string) { + if incDisabled { + contMask = []string{} + } else { + contMask = []string{"disabled"} + } + }, } var ( - output io.Writer - verbose bool - fake bool - socket string + output io.Writer + verbose bool + fake bool + socket string + incDisabled bool + contMask []string ) // Execute adds all child commands to the root command and sets flags appropriately. @@ -82,5 +91,6 @@ func init() { rootCmd.PersistentFlags().StringVarP(&config.ConfigFile, "config", "c", config.CONFIG_FILE_DEFAULT, "pathname of config file") rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "show additional info from command execution") rootCmd.PersistentFlags().BoolVarP(&fake, "no-action", "n", false, "do not actually execute commands") + rootCmd.PersistentFlags().BoolVarP(&incDisabled, "include-disabled", "a", false, "include category=disabled containers in actions") rootCmd.PersistentFlags().StringVarP(&socket, "socket", "k", "unix:///run/podman/podman.sock", "socket address to which to connect") } diff --git a/cmd/show.go b/cmd/show.go index 0ff2f93..b72129c 100644 --- a/cmd/show.go +++ b/cmd/show.go @@ -33,9 +33,9 @@ import ( // showCmd represents the show command var showCmd = &cobra.Command{ - Use: "show", - Aliases: []string{"dump", "info"}, - Short: "Show container details in toml format", + Use: "show", + Aliases: []string{"dump", "info"}, + Short: "Show container details in toml format", ValidArgsFunction: validNouns, Long: `Output a toml format for the listed containers, which should be adequate to use as a ccl config file.`, @@ -44,7 +44,7 @@ to use as a ccl config file.`, Containers []container.Container } - conts := config.Union(args) + conts := config.Union(args, contMask) output, err := toml.Marshal(parse{conts}) if err != nil { fmt.Println("could not marshal containers:", err) diff --git a/cmd/start.go b/cmd/start.go index 35c161e..50fe819 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -37,7 +37,7 @@ var startCmd = &cobra.Command{ Long: `Start configured containers. They must be created first. Arguments can be one or more container names or categories. If empty, "all" is assumed.`, Run: func(_ *cobra.Command, args []string) { - conts := config.Union(args) + conts := config.Union(args, contMask) for _, cont := range conts { fmt.Fprintln(output, "START", cont.Name) for _, c := range cont.StartCommands() { diff --git a/cmd/stop.go b/cmd/stop.go index e50d6cf..72bc2c9 100644 --- a/cmd/stop.go +++ b/cmd/stop.go @@ -37,7 +37,7 @@ var stopCmd = &cobra.Command{ Long: `Stop configured containers if running. Arguments can be one or more container names or categories. If empty, "all" is assumed.`, Run: func(_ *cobra.Command, args []string) { - conts := config.Union(args) + conts := config.Union(args, contMask) for _, cont := range conts { fmt.Fprintln(output, "STOP", cont.Name) for _, c := range cont.StopCommands() { diff --git a/cmd/update.go b/cmd/update.go index 9d7f9bc..f4474ba 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -37,7 +37,7 @@ var updateCmd = &cobra.Command{ Long: `Update container images, stopping and restarting if necessary. Arguments can be one or more container names or categories. If empty, "all" is assumed.`, Run: func(_ *cobra.Command, args []string) { - conts := config.Union(args) + conts := config.Union(args, contMask) for _, cont := range conts { fmt.Fprintln(output, "UPDATE", cont.Name) for _, c := range cont.UpdateCommands() { diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index 0df3320..d32f609 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -19,9 +19,9 @@ const ( type command string var ( - ConfigFile string = CONFIG_FILE_DEFAULT - Networks = []network.Network{} - Containers = []container.Container{} + ConfigFile = CONFIG_FILE_DEFAULT + Networks = []network.Network{} + Containers = []container.Container{} ) func Categories() []string { @@ -33,17 +33,24 @@ func Categories() []string { for _, c := range gs.Values() { categories = append(categories, c.(string)) } + slices.Sort(categories) return categories } -func Union(ids []string) (conts []container.Container) { +func Union(ids []string, catMask []string) (conts []container.Container) { if len(ids) == 0 { - return Containers + ids = []string{"all"} } h := hashset.New() for _, id := range ids { + if j := slices.Index(catMask, id); j >= 0 { + catMask = slices.Delete(catMask, j, j+1) + } for _, c := range Containers { - if id == "all" || c.Name == id || c.Category == id { + if (id == "all" || c.Category == id) && !slices.Contains(catMask, c.Category) { + h.Add(c.Name) + } + if c.Name == id { h.Add(c.Name) } } @@ -56,8 +63,17 @@ func Union(ids []string) (conts []container.Container) { if len(conts) == 0 { log.WithFields(log.Fields{ "ids": ids, - }).Warnln("No matching containers") + }).Warnln("No matching containers. If disabled, try adding -a") } + slices.SortFunc(conts, func(a, b container.Container) bool { + if a.Category < b.Category { + return true + } + if a.Category > b.Category { + return false + } + return a.Name <= b.Name + }) return } @@ -81,5 +97,8 @@ func Init(conn context.Context) error { for i := range p.Containers { p.Containers[i].Init(conn, Networks) } + slices.SortFunc(Containers, func(a, b container.Container) bool { + return a.Name < b.Name + }) return nil }