cmd/list: list show active deployment

Signed-off-by: Sam Alba <sam.alba@gmail.com>
This commit is contained in:
Sam Alba 2021-04-19 17:54:44 -07:00
parent 5e90c1a11e
commit 825f182ff3

View File

@ -1,10 +1,17 @@
package cmd package cmd
import ( import (
"context"
"fmt" "fmt"
"os"
"os/user"
"path"
"strings"
"text/tabwriter"
"dagger.io/go/cmd/dagger/logger" "dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger" "dagger.io/go/dagger"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -36,9 +43,16 @@ var listCmd = &cobra.Command{
Msg("cannot list deployments") Msg("cannot list deployments")
} }
deploymentID := getCurrentDeploymentID(ctx, store)
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent)
for _, r := range deployments { 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) 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"
}