2021-08-02 01:10:41 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2021-10-14 09:51:40 +00:00
|
|
|
"time"
|
2021-08-02 01:10:41 +00:00
|
|
|
|
|
|
|
abraFormatter "coopcloud.tech/abra/cli/formatter"
|
|
|
|
"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-08-02 01:10:41 +00:00
|
|
|
"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-08-02 01:10:41 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2021-10-14 09:51:40 +00:00
|
|
|
var watch bool
|
|
|
|
var watchFlag = &cli.BoolFlag{
|
|
|
|
Name: "watch",
|
|
|
|
Aliases: []string{"w"},
|
|
|
|
Value: false,
|
|
|
|
Usage: "Watch status by polling repeatedly",
|
|
|
|
Destination: &watch,
|
|
|
|
}
|
|
|
|
|
2021-08-02 01:10:41 +00:00
|
|
|
var appPsCommand = &cli.Command{
|
2021-11-21 13:07:06 +00:00
|
|
|
Name: "ps",
|
|
|
|
Usage: "Check app status",
|
|
|
|
Description: "This command shows a more detailed status output of a specific deployed app.",
|
|
|
|
Aliases: []string{"p"},
|
2021-10-14 09:51:40 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
watchFlag,
|
|
|
|
},
|
2021-12-11 23:17:39 +00:00
|
|
|
BashComplete: autocomplete.AppNameComplete,
|
2021-08-02 01:10:41 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2021-10-14 09:51:40 +00:00
|
|
|
if !watch {
|
|
|
|
showPSOutput(c)
|
|
|
|
return nil
|
2021-08-02 01:10:41 +00:00
|
|
|
}
|
2021-08-28 14:00:16 +00:00
|
|
|
|
2021-10-14 09:51:40 +00:00
|
|
|
// TODO: how do we make this update in-place in an x-platform way?
|
|
|
|
for {
|
|
|
|
showPSOutput(c)
|
|
|
|
time.Sleep(2 * time.Second)
|
2021-08-02 01:10:41 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2021-10-14 09:51:40 +00:00
|
|
|
|
|
|
|
// showPSOutput renders ps output.
|
|
|
|
func showPSOutput(c *cli.Context) {
|
|
|
|
app := internal.ValidateApp(c)
|
|
|
|
|
|
|
|
cl, err := client.New(app.Server)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10-14 09:51:40 +00:00
|
|
|
table := abraFormatter.CreateTable(tableCol)
|
|
|
|
|
|
|
|
for _, container := range containers {
|
|
|
|
var containerNames []string
|
|
|
|
for _, containerName := range container.Names {
|
|
|
|
trimmed := strings.TrimPrefix(containerName, "/")
|
|
|
|
containerNames = append(containerNames, trimmed)
|
|
|
|
}
|
|
|
|
|
|
|
|
tableRow := []string{
|
|
|
|
abraFormatter.RemoveSha(container.Image),
|
|
|
|
abraFormatter.HumanDuration(container.Created),
|
|
|
|
container.Status,
|
2021-12-23 20:14:15 +00:00
|
|
|
container.State,
|
2021-10-14 09:51:40 +00:00
|
|
|
formatter.DisplayablePorts(container.Ports),
|
|
|
|
}
|
|
|
|
table.Append(tableRow)
|
|
|
|
}
|
|
|
|
|
|
|
|
table.Render()
|
|
|
|
}
|