abra/cli/app/ps.go
decentral1se ff0b0b5ad8
Some checks reported errors
continuous-integration/drone/push Build was killed
feat: finish ps command
2021-08-28 16:00:16 +02:00

72 lines
1.7 KiB
Go

package app
import (
"context"
"errors"
"strings"
abraFormatter "coopcloud.tech/abra/cli/formatter"
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/client"
"coopcloud.tech/abra/config"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
var appPsCommand = &cli.Command{
Name: "ps",
Action: func(c *cli.Context) error {
appName := c.Args().First()
if appName == "" {
internal.ShowSubcommandHelpAndError(c, errors.New("no app name provided"))
}
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
host := appFiles[appName].Server
ctx := context.Background()
cl, err := client.NewClientWithContext(host)
if err != nil {
logrus.Fatal(err)
}
appEnv, err := config.GetApp(appFiles, appName)
if err != nil {
logrus.Fatal(err)
}
filters := filters.NewArgs()
filters.Add("name", appEnv.StackName())
containers, err := cl.ContainerList(ctx, types.ContainerListOptions{Filters: filters})
if err != nil {
logrus.Fatal(err)
}
tableCol := []string{"ID", "Image", "Command", "Created", "Status", "Ports", "Names"}
table := abraFormatter.CreateTable(tableCol)
for _, container := range containers {
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, ","),
}
table.Append(tableRow)
}
table.Render()
return nil
},
}