mirror of
https://gitea.elkins.co/Networking/ccl.git
synced 2025-03-09 04:31:38 -05:00
Allow for connection failures for some commands
This commit is contained in:
parent
dd7adc00e1
commit
d58402ab6f
35
cmd/ls.go
35
cmd/ls.go
@ -47,21 +47,30 @@ ccl ls squid`,
|
||||
w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 2, ' ', 0)
|
||||
defer w.Flush()
|
||||
|
||||
titlemsg := "CATEGORY\tNAME\tIMAGE\tCREATED\tRUNNING"
|
||||
fmt.Fprintf(w, "%s\n", titlemsg)
|
||||
for _, c := range conts {
|
||||
data := []interface{}{c.Category, c.Name, c.Image}
|
||||
if c.IsCreated() {
|
||||
data = append(data, " ✔")
|
||||
} else {
|
||||
data = append(data, "")
|
||||
if conn != nil {
|
||||
titlemsg := "CATEGORY\tNAME\tIMAGE\tCREATED\tRUNNING"
|
||||
fmt.Fprintf(w, "%s\n", titlemsg)
|
||||
for _, c := range conts {
|
||||
data := []interface{}{c.Category, c.Name, c.Image}
|
||||
if c.IsCreated() {
|
||||
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() {
|
||||
data = append(data, " ✔")
|
||||
} else {
|
||||
data = append(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...)
|
||||
}
|
||||
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", data...)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
33
cmd/root.go
33
cmd/root.go
@ -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)
|
||||
}
|
||||
err = config.Init(conn)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Warning: Could not initialize configuration:", err)
|
||||
ConnectMust()
|
||||
} else {
|
||||
if err := Connect(); err != nil {
|
||||
log.WithField("error", err).Warnln("Connection failed")
|
||||
}
|
||||
}
|
||||
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 {
|
||||
|
@ -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()
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user