diff --git a/cmd/root.go b/cmd/root.go index b9f752a..de10ffc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -30,6 +30,7 @@ import ( "gitea.elkins.co/Networking/ccl/internal/pkg/config" "github.com/containers/podman/v4/pkg/bindings" "github.com/spf13/cobra" + "golang.org/x/exp/slices" ) // rootCmd represents the base command when called without any subcommands @@ -42,12 +43,24 @@ 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 { + PersistentPreRun: func(cmd *cobra.Command, args []string) { + if !incDisabled { contMask = []string{"disabled"} } + + requireConn := []string{"create", "ls", "pull", "recreate", "restart", "rm", "show", "start", "stop", "update"} + if slices.Contains(requireConn, cmd.Name()) { + // connect to podman + conn, err := bindings.NewConnection(context.Background(), socket) + if err != nil { + fmt.Fprintln(os.Stderr, "Could not connect:", err) + os.Exit(1) + } + err = config.Init(conn) + if err != nil { + fmt.Fprintln(os.Stderr, "Warning: Could not initialize configuration:", err) + } + } }, } @@ -76,17 +89,6 @@ func init() { } else { output = io.Discard } - - // connect to podman - conn, err := bindings.NewConnection(context.Background(), socket) - if err != nil { - fmt.Fprintln(os.Stderr, "Could not connect:", err) - os.Exit(1) - } - err = config.Init(conn) - if err != nil { - fmt.Fprintln(os.Stderr, "Warning: Could not initialize configuration:", err) - } }) 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")