Merge pull request #343 from samalba/312-list-current

cmd/list: list show active deployment
This commit is contained in:
Sam Alba 2021-04-22 11:50:09 -07:00 committed by GitHub
commit d2d0734eaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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"
}