feat: first steps towards watchable ps output
See coop-cloud/organising#178.
This commit is contained in:
parent
454ee696d6
commit
d4d4da19b7
@ -3,6 +3,7 @@ package app
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
abraFormatter "coopcloud.tech/abra/cli/formatter"
|
abraFormatter "coopcloud.tech/abra/cli/formatter"
|
||||||
"coopcloud.tech/abra/cli/internal"
|
"coopcloud.tech/abra/cli/internal"
|
||||||
@ -15,48 +16,33 @@ import (
|
|||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var watch bool
|
||||||
|
var watchFlag = &cli.BoolFlag{
|
||||||
|
Name: "watch",
|
||||||
|
Aliases: []string{"w"},
|
||||||
|
Value: false,
|
||||||
|
Usage: "Watch status by polling repeatedly",
|
||||||
|
Destination: &watch,
|
||||||
|
}
|
||||||
|
|
||||||
var appPsCommand = &cli.Command{
|
var appPsCommand = &cli.Command{
|
||||||
Name: "ps",
|
Name: "ps",
|
||||||
Usage: "Check app status",
|
Usage: "Check app status",
|
||||||
Aliases: []string{"p"},
|
Aliases: []string{"p"},
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
watchFlag,
|
||||||
|
},
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
app := internal.ValidateApp(c)
|
if !watch {
|
||||||
|
showPSOutput(c)
|
||||||
cl, err := client.New(app.Server)
|
return nil
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
filters := filters.NewArgs()
|
// TODO: how do we make this update in-place in an x-platform way?
|
||||||
filters.Add("name", app.StackName())
|
for {
|
||||||
|
showPSOutput(c)
|
||||||
containers, err := cl.ContainerList(c.Context, types.ContainerListOptions{Filters: filters})
|
time.Sleep(2 * time.Second)
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tableCol := []string{"image", "created", "status", "ports", "names"}
|
|
||||||
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,
|
|
||||||
formatter.DisplayablePorts(container.Ports),
|
|
||||||
strings.Join(containerNames, "\n"),
|
|
||||||
}
|
|
||||||
table.Append(tableRow)
|
|
||||||
}
|
|
||||||
|
|
||||||
table.Render()
|
|
||||||
return nil
|
|
||||||
},
|
},
|
||||||
BashComplete: func(c *cli.Context) {
|
BashComplete: func(c *cli.Context) {
|
||||||
appNames, err := config.GetAppNames()
|
appNames, err := config.GetAppNames()
|
||||||
@ -71,3 +57,43 @@ var appPsCommand = &cli.Command{
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
tableCol := []string{"image", "created", "status", "ports", "names"}
|
||||||
|
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,
|
||||||
|
formatter.DisplayablePorts(container.Ports),
|
||||||
|
strings.Join(containerNames, "\n"),
|
||||||
|
}
|
||||||
|
table.Append(tableRow)
|
||||||
|
}
|
||||||
|
|
||||||
|
table.Render()
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user