feat: finish ps command
Some checks reported errors
continuous-integration/drone/push Build was killed
Some checks reported errors
continuous-integration/drone/push Build was killed
This commit is contained in:
parent
ce5a03d579
commit
ff0b0b5ad8
2
TODO.md
2
TODO.md
@ -23,7 +23,7 @@
|
||||
- [ ] `config`
|
||||
- [ ] `cp`
|
||||
- [ ] `logs`
|
||||
- [ ] `ps` (WIP: decentral1se)
|
||||
- [x] `ps`
|
||||
- [ ] `restore`
|
||||
- [x] `rm`
|
||||
- [ ] `run`
|
||||
|
@ -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
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user