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,6 +47,7 @@ ccl ls squid`,
w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 2, ' ', 0)
defer w.Flush()
if conn != nil {
titlemsg := "CATEGORY\tNAME\tIMAGE\tCREATED\tRUNNING"
fmt.Fprintf(w, "%s\n", titlemsg)
for _, c := range conts {
@ -63,6 +64,14 @@ ccl ls squid`,
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", data...)
}
} else {
titlemsg := "CATEGORY\tNAME\tIMAGE"
fmt.Fprintf(w, "%s\n", titlemsg)
for _, c := range conts {
data := []interface{}{c.Category, c.Name, c.Image}
fmt.Fprintf(w, "%s\t%s\t%s\n", data...)
}
}
},
}

View File

@ -29,6 +29,7 @@ import (
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
"github.com/containers/podman/v4/pkg/bindings"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
)
@ -48,23 +49,24 @@ execute the necessary podman commands.`,
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()) {
// connect to podman
conn, err := bindings.NewConnection(context.Background(), socket)
if err != nil {
fmt.Fprintln(os.Stderr, "Could not connect:", err)
os.Exit(1)
ConnectMust()
} else {
if err := Connect(); err != nil {
log.WithField("error", err).Warnln("Connection failed")
}
err = config.Init(conn)
}
err := config.Init(conn)
if err != nil {
fmt.Fprintln(os.Stderr, "Warning: Could not initialize configuration:", err)
}
}
},
}
var (
conn context.Context
output io.Writer
verbose 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() {
cobra.OnInitialize(func() {
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 {
c.Umask.SetValid(0o022)
}
if conn == nil {
return fmt.Errorf("conn is nil: %s", c.Name)
}
c.conn = conn
return c.populateCData()
}