From 866cdd1f29f6c845c872b067a117ccee6eb06927 Mon Sep 17 00:00:00 2001 From: cellarspoon Date: Fri, 31 Dec 2021 12:59:31 +0100 Subject: [PATCH] feat: service name in ps output --- cli/app/ps.go | 4 +++- pkg/service/service.go | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/cli/app/ps.go b/cli/app/ps.go index 3aefd06e..e1215cf8 100644 --- a/cli/app/ps.go +++ b/cli/app/ps.go @@ -9,6 +9,7 @@ import ( "coopcloud.tech/abra/pkg/client" "coopcloud.tech/abra/pkg/config" "coopcloud.tech/abra/pkg/formatter" + "coopcloud.tech/abra/pkg/service" stack "coopcloud.tech/abra/pkg/upstream/stack" "github.com/buger/goterm" dockerFormatter "github.com/docker/cli/cli/command/formatter" @@ -70,7 +71,7 @@ func showPSOutput(c *cli.Context, app config.App, cl *dockerClient.Client) { logrus.Fatal(err) } - tableCol := []string{"image", "created", "status", "state", "ports"} + tableCol := []string{"service name", "image", "created", "status", "state", "ports"} table := formatter.CreateTable(tableCol) for _, container := range containers { @@ -81,6 +82,7 @@ func showPSOutput(c *cli.Context, app config.App, cl *dockerClient.Client) { } tableRow := []string{ + service.ContainerToServiceName(container.Names, app.StackName()), formatter.RemoveSha(container.Image), formatter.HumanDuration(container.Created), container.Status, diff --git a/pkg/service/service.go b/pkg/service/service.go index b957e772..3d92d821 100644 --- a/pkg/service/service.go +++ b/pkg/service/service.go @@ -67,3 +67,12 @@ func GetService(c context.Context, cl *client.Client, filters filters.Args, prom return services[0], nil } + +// ContainerToServiceName converts a container name to a service name. +func ContainerToServiceName(containerNames []string, stackName string) string { + containerName := strings.Join(containerNames, "") + trimmed := strings.TrimPrefix(containerName, "/") + stackNameServiceName := strings.Split(trimmed, ".")[0] + splitter := fmt.Sprintf("%s_", stackName) + return strings.Split(stackNameServiceName, splitter)[1] +}