cmd/output: added support for listing outputs

Signed-off-by: Sam Alba <sam.alba@gmail.com>
This commit is contained in:
Sam Alba 2021-05-31 15:25:02 +02:00
parent 6e3ec02ceb
commit 1a59c9c40b
2 changed files with 83 additions and 3 deletions

View File

@ -0,0 +1,81 @@
package output
import (
"context"
"fmt"
"os"
"strings"
"text/tabwriter"
"go.dagger.io/dagger/client"
"go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/environment"
"go.dagger.io/dagger/solver"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var listCmd = &cobra.Command{
Use: "list [TARGET] [flags]",
Short: "List the outputs of an environment",
Args: cobra.MaximumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags:
// https://github.com/spf13/viper/issues/233
if err := viper.BindPFlags(cmd.Flags()); err != nil {
panic(err)
}
},
Run: func(cmd *cobra.Command, args []string) {
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
workspace := common.CurrentWorkspace(ctx)
st := common.CurrentEnvironmentState(ctx, workspace)
lg = lg.With().
Str("environment", st.Name).
Logger()
c, err := client.New(ctx, "", false)
if err != nil {
lg.Fatal().Err(err).Msg("unable to create client")
}
_, err = c.Do(ctx, st, func(ctx context.Context, env *environment.Environment, s solver.Solver) error {
outputs, err := env.ScanOutputs(ctx)
if err != nil {
return err
}
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
fmt.Fprintln(w, "Output\tType\tValue\tDescription")
for _, out := range outputs {
isConcrete := (out.IsConcreteR() == nil)
valStr := "-"
if isConcrete {
valStr, _ = out.Cue().String()
}
valStr = strings.ReplaceAll(valStr, "\n", "\\n")
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n",
out.Path(),
common.ValueType(out),
valStr,
common.ValueDocString(out),
)
}
w.Flush()
return nil
})
if err != nil {
lg.Fatal().Err(err).Msg("failed to query environment")
}
},
}

View File

@ -9,7 +9,6 @@ var Cmd = &cobra.Command{
} }
func init() { func init() {
Cmd.AddCommand( // Cmd.AddCommand(dirCmd)
dirCmd, Cmd.AddCommand(listCmd)
)
} }