From 825f182ff312744ff5ce2237ab986b007e089751 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Mon, 19 Apr 2021 17:54:44 -0700 Subject: [PATCH] cmd/list: list show active deployment Signed-off-by: Sam Alba --- cmd/dagger/cmd/list.go | 54 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/cmd/dagger/cmd/list.go b/cmd/dagger/cmd/list.go index d265dc1e..c72157a1 100644 --- a/cmd/dagger/cmd/list.go +++ b/cmd/dagger/cmd/list.go @@ -1,10 +1,17 @@ package cmd import ( + "context" "fmt" + "os" + "os/user" + "path" + "strings" + "text/tabwriter" "dagger.io/go/cmd/dagger/logger" "dagger.io/go/dagger" + "github.com/rs/zerolog/log" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -36,9 +43,16 @@ var listCmd = &cobra.Command{ Msg("cannot list deployments") } + deploymentID := getCurrentDeploymentID(ctx, store) + w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent) for _, r := range deployments { - fmt.Println(r.Name) + line := fmt.Sprintf("%s\t%s\t", r.Name, formatPlanSource(r.PlanSource)) + if r.ID == deploymentID { + line = fmt.Sprintf("%s- active deployment", line) + } + fmt.Fprintln(w, line) } + w.Flush() }, } @@ -47,3 +61,41 @@ func init() { panic(err) } } + +func getCurrentDeploymentID(ctx context.Context, store *dagger.Store) string { + lg := log.Ctx(ctx) + + wd, err := os.Getwd() + if err != nil { + lg.Warn().Err(err).Msg("cannot get current working directory") + return "" + } + st, _ := store.LookupDeploymentByPath(ctx, wd) + if len(st) == 1 { + return st[0].ID + } + return "" +} + +func formatPath(p string) string { + usr, _ := user.Current() + dir := usr.HomeDir + + if strings.HasPrefix(p, dir) { + return path.Join("~", p[len(dir):]) + } + return p +} + +func formatPlanSource(i dagger.Input) string { + switch i.Type { + case dagger.InputTypeDir: + return formatPath(i.Dir.Path) + case dagger.InputTypeGit: + return i.Git.Remote + case dagger.InputTypeDocker: + return i.Docker.Ref + } + + return "no plan" +}