abra/cli/app/ps.go

70 lines
1.6 KiB
Go
Raw Normal View History

package app
import (
"fmt"
"strings"
abraFormatter "coopcloud.tech/abra/cli/formatter"
"coopcloud.tech/abra/cli/internal"
2021-09-05 19:37:03 +00:00
"coopcloud.tech/abra/pkg/client"
"coopcloud.tech/abra/pkg/config"
"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"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
var appPsCommand = &cli.Command{
2021-09-04 23:34:56 +00:00
Name: "ps",
Usage: "Check app status",
Aliases: []string{"p"},
Action: func(c *cli.Context) error {
app := internal.ValidateApp(c)
2021-08-28 14:00:16 +00:00
cl, err := client.New(app.Server)
2021-08-28 14:00:16 +00:00
if err != nil {
logrus.Fatal(err)
}
2021-08-28 14:00:16 +00:00
filters := filters.NewArgs()
filters.Add("name", app.StackName())
2021-08-28 14:00:16 +00:00
containers, err := cl.ContainerList(c.Context, types.ContainerListOptions{Filters: filters})
if err != nil {
logrus.Fatal(err)
}
2021-08-28 14:00:16 +00:00
tableCol := []string{"id", "image", "command", "created", "status", "ports", "names"}
2021-08-28 14:00:16 +00:00
table := abraFormatter.CreateTable(tableCol)
for _, container := range containers {
2021-08-28 14:00:16 +00:00
tableRow := []string{
abraFormatter.ShortenID(container.ID),
abraFormatter.RemoveSha(container.Image),
abraFormatter.Truncate(container.Command),
abraFormatter.HumanDuration(container.Created),
container.Status,
formatter.DisplayablePorts(container.Ports),
strings.Join(container.Names, ", "),
}
2021-08-28 14:00:16 +00:00
table.Append(tableRow)
}
2021-08-28 14:00:16 +00:00
table.Render()
return nil
},
BashComplete: func(c *cli.Context) {
appNames, err := config.GetAppNames()
if err != nil {
logrus.Warn(err)
}
if c.NArg() > 0 {
return
}
for _, a := range appNames {
fmt.Println(a)
}
},
}