feat: finish ps command
Some checks reported errors
continuous-integration/drone/push Build was killed

This commit is contained in:
decentral1se 2021-08-28 16:00:16 +02:00
parent ce5a03d579
commit ff0b0b5ad8
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
2 changed files with 30 additions and 20 deletions

View File

@ -23,7 +23,7 @@
- [ ] `config`
- [ ] `cp`
- [ ] `logs`
- [ ] `ps` (WIP: decentral1se)
- [x] `ps`
- [ ] `restore`
- [x] `rm`
- [ ] `run`

View File

@ -2,16 +2,16 @@ package app
import (
"context"
"fmt"
"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/cli/cli/command/idresolver"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/api/types/filters"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
@ -19,31 +19,41 @@ import (
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(internal.Context)
cl, err := client.NewClientWithContext(host)
if err != nil {
logrus.Fatal(err)
}
tasks, err := cl.TaskList(ctx, types.TaskListOptions{})
appEnv, err := config.GetApp(appFiles, appName)
if err != nil {
logrus.Fatal(err)
}
for _, task := range tasks {
resolver := idresolver.New(cl, false)
serviceName, err := resolver.Resolve(ctx, swarm.Service{}, task.ServiceID)
if err != nil {
return err
}
fmt.Printf("%#v\n", serviceName)
}
containers, err := cl.ContainerList(ctx, types.ContainerListOptions{})
filters := filters.NewArgs()
filters.Add("name", appEnv.StackName())
containers, err := cl.ContainerList(ctx, types.ContainerListOptions{Filters: filters})
if err != nil {
logrus.Fatal(err)
}
table := abraFormatter.CreateTable([]string{"ID", "Image", "Command", "Created", "Status", "Ports", "Names"})
var conTable [][]string
tableCol := []string{"ID", "Image", "Command", "Created", "Status", "Ports", "Names"}
table := abraFormatter.CreateTable(tableCol)
for _, container := range containers {
conRow := []string{
tableRow := []string{
abraFormatter.ShortenID(container.ID),
abraFormatter.RemoveSha(container.Image),
abraFormatter.Truncate(container.Command),
@ -52,9 +62,9 @@ var appPsCommand = &cli.Command{
formatter.DisplayablePorts(container.Ports),
strings.Join(container.Names, ","),
}
conTable = append(conTable, conRow)
table.Append(tableRow)
}
table.AppendBulk(conTable)
table.Render()
return nil
},