Allow for connection failures for some commands

This commit is contained in:
Joel Elkins 2022-07-29 22:10:12 -05:00
parent dd7adc00e1
commit d58402ab6f
3 changed files with 50 additions and 23 deletions

View File

@ -47,21 +47,30 @@ ccl ls squid`,
w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 2, ' ', 0) w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 2, ' ', 0)
defer w.Flush() defer w.Flush()
titlemsg := "CATEGORY\tNAME\tIMAGE\tCREATED\tRUNNING" if conn != nil {
fmt.Fprintf(w, "%s\n", titlemsg) titlemsg := "CATEGORY\tNAME\tIMAGE\tCREATED\tRUNNING"
for _, c := range conts { fmt.Fprintf(w, "%s\n", titlemsg)
data := []interface{}{c.Category, c.Name, c.Image} for _, c := range conts {
if c.IsCreated() { data := []interface{}{c.Category, c.Name, c.Image}
data = append(data, " ✔") if c.IsCreated() {
} else { data = append(data, " ✔")
data = append(data, "") } else {
data = append(data, "")
}
if c.IsRunning() {
data = append(data, " ✔")
} else {
data = append(data, "")
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", data...)
} }
if c.IsRunning() { } else {
data = append(data, " ✔") titlemsg := "CATEGORY\tNAME\tIMAGE"
} else { fmt.Fprintf(w, "%s\n", titlemsg)
data = append(data, "") for _, c := range conts {
data := []interface{}{c.Category, c.Name, c.Image}
fmt.Fprintf(w, "%s\t%s\t%s\n", data...)
} }
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", data...)
} }
}, },
} }

View File

@ -29,6 +29,7 @@ import (
"gitea.elkins.co/Networking/ccl/internal/pkg/config" "gitea.elkins.co/Networking/ccl/internal/pkg/config"
"github.com/containers/podman/v4/pkg/bindings" "github.com/containers/podman/v4/pkg/bindings"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
) )
@ -48,23 +49,24 @@ execute the necessary podman commands.`,
contMask = []string{"disabled"} contMask = []string{"disabled"}
} }
requireConn := []string{"create", "ls", "pull", "recreate", "restart", "rm", "show", "start", "stop", "update"} requireConn := []string{"create", "pull", "recreate", "restart", "rm", "show", "start", "stop", "update"}
if slices.Contains(requireConn, cmd.Name()) { if slices.Contains(requireConn, cmd.Name()) {
// connect to podman // connect to podman
conn, err := bindings.NewConnection(context.Background(), socket) ConnectMust()
if err != nil { } else {
fmt.Fprintln(os.Stderr, "Could not connect:", err) if err := Connect(); err != nil {
os.Exit(1) log.WithField("error", err).Warnln("Connection failed")
}
err = config.Init(conn)
if err != nil {
fmt.Fprintln(os.Stderr, "Warning: Could not initialize configuration:", err)
} }
} }
err := config.Init(conn)
if err != nil {
fmt.Fprintln(os.Stderr, "Warning: Could not initialize configuration:", err)
}
}, },
} }
var ( var (
conn context.Context
output io.Writer output io.Writer
verbose bool verbose bool
fake bool fake bool
@ -82,6 +84,19 @@ func Execute() {
} }
} }
func Connect() error {
var err error
conn, err = bindings.NewConnection(context.WithValue(context.Background(), "output", output), socket)
return err
}
func ConnectMust() {
if err := Connect(); err != nil {
log.WithField("error", err).Errorf("Could not connect")
os.Exit(1)
}
}
func init() { func init() {
cobra.OnInitialize(func() { cobra.OnInitialize(func() {
if verbose { if verbose {

View File

@ -86,10 +86,13 @@ func (c *Container) Init(conn context.Context, nets []network.Network) error {
} }
} }
c.conn = conn
if !c.Umask.Valid { if !c.Umask.Valid {
c.Umask.SetValid(0o022) c.Umask.SetValid(0o022)
} }
if conn == nil {
return fmt.Errorf("conn is nil: %s", c.Name)
}
c.conn = conn
return c.populateCData() return c.populateCData()
} }