cmd/output: list now lists all outputs everytime, even if not concrete - also allows to show outputs even if the env was never computes (based on UX feedback)

Signed-off-by: Sam Alba <sam.alba@gmail.com>
This commit is contained in:
Sam Alba 2021-06-01 14:14:02 +02:00
parent 03d58e074f
commit 10d1b01f2a
2 changed files with 15 additions and 38 deletions

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"os" "os"
"strings"
"text/tabwriter" "text/tabwriter"
"go.dagger.io/dagger/client" "go.dagger.io/dagger/client"
@ -37,19 +36,15 @@ var listCmd = &cobra.Command{
workspace := common.CurrentWorkspace(ctx) workspace := common.CurrentWorkspace(ctx)
st := common.CurrentEnvironmentState(ctx, workspace) st := common.CurrentEnvironmentState(ctx, workspace)
ListOutputs(ctx, st, viper.GetBool("all")) ListOutputs(ctx, st, true)
}, },
} }
func ListOutputs(ctx context.Context, st *state.State, all bool) { func ListOutputs(ctx context.Context, st *state.State, isTTY bool) {
lg := log.Ctx(ctx).With(). lg := log.Ctx(ctx).With().
Str("environment", st.Name). Str("environment", st.Name).
Logger() Logger()
if st.Computed == "" {
lg.Fatal().Msg("cannot list environment outputs: please run `dagger up` first")
}
c, err := client.New(ctx, "", false) c, err := client.New(ctx, "", false)
if err != nil { if err != nil {
lg.Fatal().Err(err).Msg("unable to create client") lg.Fatal().Err(err).Msg("unable to create client")
@ -61,27 +56,22 @@ func ListOutputs(ctx context.Context, st *state.State, all bool) {
return err return err
} }
if !isTTY {
for _, out := range outputs {
lg.Info().Str("name", out.Path().String()).
Str("value", fmt.Sprintf("%v", out.Cue())).
Msg("output")
}
return nil
}
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0) w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
fmt.Fprintln(w, "Output\tType\tValue\tDescription") fmt.Fprintln(w, "Output\tValue\tDescription")
for _, out := range outputs { for _, out := range outputs {
valStr := "-" fmt.Fprintf(w, "%s\t%s\t%s\n",
if out.IsConcreteR() == nil {
valStr, err = out.Cue().String()
if err != nil {
return err
}
} else if !all {
continue
}
valStr = strings.ReplaceAll(valStr, "\n", "\\n")
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n",
out.Path(), out.Path(),
common.ValueType(out), common.FormatValue(out),
valStr,
common.ValueDocString(out), common.ValueDocString(out),
) )
} }
@ -94,11 +84,3 @@ func ListOutputs(ctx context.Context, st *state.State, all bool) {
lg.Fatal().Err(err).Msg("failed to query environment") lg.Fatal().Err(err).Msg("failed to query environment")
} }
} }
func init() {
listCmd.Flags().BoolP("all", "a", false, "List all outputs (include non-concrete)")
if err := viper.BindPFlags(listCmd.Flags()); err != nil {
panic(err)
}
}

View File

@ -48,12 +48,7 @@ var upCmd = &cobra.Command{
lg.Fatal().Err(err).Msg("failed to update environment") lg.Fatal().Err(err).Msg("failed to update environment")
} }
if !term.IsTerminal(int(os.Stdout.Fd())) { output.ListOutputs(ctx, st, term.IsTerminal(int(os.Stdout.Fd())))
lg.Debug().Msg("not a tty, output list disabled")
return
}
output.ListOutputs(ctx, st, false)
}, },
} }