Mask disabled category from action by default

This commit is contained in:
Joel Elkins 2022-07-27 21:48:47 -05:00
parent 98ec553778
commit ddd391c8b0
No known key found for this signature in database
GPG Key ID: 133589DC38921AE2
13 changed files with 54 additions and 25 deletions

View File

@ -37,7 +37,7 @@ var createCmd = &cobra.Command{
Long: `Create containers specified by the arguments. Arguments can be either container Long: `Create containers specified by the arguments. Arguments can be either container
names or categories. Multiple arguments are supported.`, names or categories. Multiple arguments are supported.`,
Run: func(_ *cobra.Command, args []string) { Run: func(_ *cobra.Command, args []string) {
conts := config.Union(args) conts := config.Union(args, contMask)
for _, cont := range conts { for _, cont := range conts {
fmt.Fprintln(output, "CREATE", cont.Name) fmt.Fprintln(output, "CREATE", cont.Name)
for _, c := range cont.CreateCommands() { for _, c := range cont.CreateCommands() {

View File

@ -43,7 +43,7 @@ ccl ls all # same as above
ccl ls default sub # multiple ok ccl ls default sub # multiple ok
ccl ls squid`, ccl ls squid`,
Run: func(cmd *cobra.Command, args []string) { 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) w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 2, ' ', 0)
defer w.Flush() defer w.Flush()

View File

@ -28,7 +28,7 @@ import (
func validNouns(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) { func validNouns(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
validArgs := []string{} validArgs := []string{}
for _, c := range config.Union([]string{}) { for _, c := range config.Containers {
validArgs = append(validArgs, c.Name) validArgs = append(validArgs, c.Name)
} }
validArgs = append(validArgs, config.Categories()...) validArgs = append(validArgs, config.Categories()...)

View File

@ -38,7 +38,7 @@ var pullCmd = &cobra.Command{
affected: the old image will still remain, though untagged, and any defined containers affected: the old image will still remain, though untagged, and any defined containers
will still use it.`, will still use it.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
conts := config.Union(args) conts := config.Union(args, contMask)
for _, cont := range conts { for _, cont := range conts {
fmt.Fprintln(output, "PULL", cont.Name) fmt.Fprintln(output, "PULL", cont.Name)
for _, cmd := range cont.PullCommands() { for _, cmd := range cont.PullCommands() {

View File

@ -37,7 +37,7 @@ var recreateCmd = &cobra.Command{
Long: `Recreate container images, stopping and restarting if necessary. Arguments can be Long: `Recreate container images, stopping and restarting if necessary. Arguments can be
one or more container names or categories. If empty, "all" is assumed.`, one or more container names or categories. If empty, "all" is assumed.`,
Run: func(_ *cobra.Command, args []string) { Run: func(_ *cobra.Command, args []string) {
conts := config.Union(args) conts := config.Union(args, contMask)
for _, cont := range conts { for _, cont := range conts {
fmt.Fprintln(output, "RECREATE", cont.Name) fmt.Fprintln(output, "RECREATE", cont.Name)
for _, c := range cont.RecreateCommands() { for _, c := range cont.RecreateCommands() {

View File

@ -37,7 +37,7 @@ var restartCmd = &cobra.Command{
Long: `Stop configured containers (if running), then restart them. Arguments can be Long: `Stop configured containers (if running), then restart them. Arguments can be
one or more container names or categories. If empty, "all" is assumed.`, one or more container names or categories. If empty, "all" is assumed.`,
Run: func(_ *cobra.Command, args []string) { Run: func(_ *cobra.Command, args []string) {
conts := config.Union(args) conts := config.Union(args, contMask)
for _, cont := range conts { for _, cont := range conts {
fmt.Fprintln(output, "RESTART", cont.Name) fmt.Fprintln(output, "RESTART", cont.Name)
for _, c := range cont.RestartCommands() { for _, c := range cont.RestartCommands() {

View File

@ -38,7 +38,7 @@ var rmCmd = &cobra.Command{
Long: `Remove defined containers or categories if they have been created. Long: `Remove defined containers or categories if they have been created.
If running, they will first be stopped.`, If running, they will first be stopped.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
conts := config.Union(args) conts := config.Union(args, contMask)
for _, cont := range conts { for _, cont := range conts {
fmt.Fprintln(output, "REMOVE", cont.Name) fmt.Fprintln(output, "REMOVE", cont.Name)
for _, cmd := range cont.DestroyCommands() { for _, cmd := range cont.DestroyCommands() {

View File

@ -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 from a toml configuration file, and the utility uses this information to
execute the necessary podman commands.`, execute the necessary podman commands.`,
ValidArgsFunction: cobra.NoFileCompletions, ValidArgsFunction: cobra.NoFileCompletions,
PersistentPreRun: func(*cobra.Command, []string) {
if incDisabled {
contMask = []string{}
} else {
contMask = []string{"disabled"}
}
},
} }
var ( var (
output io.Writer output io.Writer
verbose bool verbose bool
fake bool fake bool
socket string socket string
incDisabled bool
contMask []string
) )
// Execute adds all child commands to the root command and sets flags appropriately. // 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().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(&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(&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") rootCmd.PersistentFlags().StringVarP(&socket, "socket", "k", "unix:///run/podman/podman.sock", "socket address to which to connect")
} }

View File

@ -33,9 +33,9 @@ import (
// showCmd represents the show command // showCmd represents the show command
var showCmd = &cobra.Command{ var showCmd = &cobra.Command{
Use: "show", Use: "show",
Aliases: []string{"dump", "info"}, Aliases: []string{"dump", "info"},
Short: "Show container details in toml format", Short: "Show container details in toml format",
ValidArgsFunction: validNouns, ValidArgsFunction: validNouns,
Long: `Output a toml format for the listed containers, which should be adequate Long: `Output a toml format for the listed containers, which should be adequate
to use as a ccl config file.`, to use as a ccl config file.`,
@ -44,7 +44,7 @@ to use as a ccl config file.`,
Containers []container.Container Containers []container.Container
} }
conts := config.Union(args) conts := config.Union(args, contMask)
output, err := toml.Marshal(parse{conts}) output, err := toml.Marshal(parse{conts})
if err != nil { if err != nil {
fmt.Println("could not marshal containers:", err) fmt.Println("could not marshal containers:", err)

View File

@ -37,7 +37,7 @@ var startCmd = &cobra.Command{
Long: `Start configured containers. They must be created first. Arguments can be Long: `Start configured containers. They must be created first. Arguments can be
one or more container names or categories. If empty, "all" is assumed.`, one or more container names or categories. If empty, "all" is assumed.`,
Run: func(_ *cobra.Command, args []string) { Run: func(_ *cobra.Command, args []string) {
conts := config.Union(args) conts := config.Union(args, contMask)
for _, cont := range conts { for _, cont := range conts {
fmt.Fprintln(output, "START", cont.Name) fmt.Fprintln(output, "START", cont.Name)
for _, c := range cont.StartCommands() { for _, c := range cont.StartCommands() {

View File

@ -37,7 +37,7 @@ var stopCmd = &cobra.Command{
Long: `Stop configured containers if running. Arguments can be Long: `Stop configured containers if running. Arguments can be
one or more container names or categories. If empty, "all" is assumed.`, one or more container names or categories. If empty, "all" is assumed.`,
Run: func(_ *cobra.Command, args []string) { Run: func(_ *cobra.Command, args []string) {
conts := config.Union(args) conts := config.Union(args, contMask)
for _, cont := range conts { for _, cont := range conts {
fmt.Fprintln(output, "STOP", cont.Name) fmt.Fprintln(output, "STOP", cont.Name)
for _, c := range cont.StopCommands() { for _, c := range cont.StopCommands() {

View File

@ -37,7 +37,7 @@ var updateCmd = &cobra.Command{
Long: `Update container images, stopping and restarting if necessary. Arguments can be Long: `Update container images, stopping and restarting if necessary. Arguments can be
one or more container names or categories. If empty, "all" is assumed.`, one or more container names or categories. If empty, "all" is assumed.`,
Run: func(_ *cobra.Command, args []string) { Run: func(_ *cobra.Command, args []string) {
conts := config.Union(args) conts := config.Union(args, contMask)
for _, cont := range conts { for _, cont := range conts {
fmt.Fprintln(output, "UPDATE", cont.Name) fmt.Fprintln(output, "UPDATE", cont.Name)
for _, c := range cont.UpdateCommands() { for _, c := range cont.UpdateCommands() {

View File

@ -19,9 +19,9 @@ const (
type command string type command string
var ( var (
ConfigFile string = CONFIG_FILE_DEFAULT ConfigFile = CONFIG_FILE_DEFAULT
Networks = []network.Network{} Networks = []network.Network{}
Containers = []container.Container{} Containers = []container.Container{}
) )
func Categories() []string { func Categories() []string {
@ -33,17 +33,24 @@ func Categories() []string {
for _, c := range gs.Values() { for _, c := range gs.Values() {
categories = append(categories, c.(string)) categories = append(categories, c.(string))
} }
slices.Sort(categories)
return categories return categories
} }
func Union(ids []string) (conts []container.Container) { func Union(ids []string, catMask []string) (conts []container.Container) {
if len(ids) == 0 { if len(ids) == 0 {
return Containers ids = []string{"all"}
} }
h := hashset.New() h := hashset.New()
for _, id := range ids { for _, id := range ids {
if j := slices.Index(catMask, id); j >= 0 {
catMask = slices.Delete(catMask, j, j+1)
}
for _, c := range Containers { 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) h.Add(c.Name)
} }
} }
@ -56,8 +63,17 @@ func Union(ids []string) (conts []container.Container) {
if len(conts) == 0 { if len(conts) == 0 {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"ids": ids, "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 return
} }
@ -81,5 +97,8 @@ func Init(conn context.Context) error {
for i := range p.Containers { for i := range p.Containers {
p.Containers[i].Init(conn, Networks) p.Containers[i].Init(conn, Networks)
} }
slices.SortFunc(Containers, func(a, b container.Container) bool {
return a.Name < b.Name
})
return nil return nil
} }