abra/cli/app/ps.go

95 lines
2.3 KiB
Go
Raw Normal View History

package app
import (
"strings"
"time"
"coopcloud.tech/abra/cli/internal"
2021-12-11 23:17:39 +00:00
"coopcloud.tech/abra/pkg/autocomplete"
2021-09-05 19:37:03 +00:00
"coopcloud.tech/abra/pkg/client"
2021-12-27 03:00:37 +00:00
"coopcloud.tech/abra/pkg/config"
2021-12-28 00:24:23 +00:00
"coopcloud.tech/abra/pkg/formatter"
stack "coopcloud.tech/abra/pkg/upstream/stack"
"github.com/buger/goterm"
2021-12-28 00:24:23 +00:00
dockerFormatter "github.com/docker/cli/cli/command/formatter"
"github.com/docker/docker/api/types"
2021-08-28 14:00:16 +00:00
"github.com/docker/docker/api/types/filters"
2021-12-27 03:00:37 +00:00
dockerClient "github.com/docker/docker/client"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
var appPsCommand = &cli.Command{
Name: "ps",
Usage: "Check app status",
Description: "This command shows a more detailed status output of a specific deployed app.",
Aliases: []string{"p"},
Flags: []cli.Flag{
internal.WatchFlag,
},
2021-12-11 23:17:39 +00:00
BashComplete: autocomplete.AppNameComplete,
Action: func(c *cli.Context) error {
2021-12-27 03:00:37 +00:00
app := internal.ValidateApp(c)
cl, err := client.New(app.Server)
if err != nil {
logrus.Fatal(err)
}
isDeployed, _, err := stack.IsDeployed(c.Context, cl, app.StackName())
if err != nil {
logrus.Fatal(err)
}
if !isDeployed {
logrus.Fatalf("%s is not deployed?", app.Name)
}
if !internal.Watch {
2021-12-27 03:00:37 +00:00
showPSOutput(c, app, cl)
return nil
}
2021-08-28 14:00:16 +00:00
goterm.Clear()
for {
goterm.MoveCursor(1, 1)
2021-12-27 03:00:37 +00:00
showPSOutput(c, app, cl)
goterm.Flush()
time.Sleep(2 * time.Second)
}
},
}
// showPSOutput renders ps output.
2021-12-27 03:00:37 +00:00
func showPSOutput(c *cli.Context, app config.App, cl *dockerClient.Client) {
filters := filters.NewArgs()
filters.Add("name", app.StackName())
containers, err := cl.ContainerList(c.Context, types.ContainerListOptions{Filters: filters})
if err != nil {
logrus.Fatal(err)
}
2021-12-23 20:14:15 +00:00
tableCol := []string{"image", "created", "status", "state", "ports"}
2021-12-28 00:24:23 +00:00
table := formatter.CreateTable(tableCol)
for _, container := range containers {
var containerNames []string
for _, containerName := range container.Names {
trimmed := strings.TrimPrefix(containerName, "/")
containerNames = append(containerNames, trimmed)
}
tableRow := []string{
2021-12-28 00:24:23 +00:00
formatter.RemoveSha(container.Image),
formatter.HumanDuration(container.Created),
container.Status,
2021-12-23 20:14:15 +00:00
container.State,
2021-12-28 00:24:23 +00:00
dockerFormatter.DisplayablePorts(container.Ports),
}
table.Append(tableRow)
}
table.Render()
}