Merge pull request #530 from samalba/env-outputs

Support Outputs list
This commit is contained in:
Andrea Luzzardi
2021-06-01 19:47:47 -07:00
committed by GitHub
18 changed files with 303 additions and 98 deletions

View File

@@ -2,10 +2,13 @@ package common
import (
"context"
"fmt"
"strings"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"go.dagger.io/dagger/client"
"go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/environment"
"go.dagger.io/dagger/solver"
"go.dagger.io/dagger/state"
@@ -97,3 +100,49 @@ func EnvironmentUp(ctx context.Context, state *state.State, noCache bool) *envir
}
return result
}
// FormatValue returns the String representation of the cue value
func FormatValue(val *compiler.Value) string {
if val.HasAttr("artifact") {
return "dagger.#Artifact"
}
if val.HasAttr("secret") {
return "dagger.#Secret"
}
if val.IsConcreteR() != nil {
return val.Cue().IncompleteKind().String()
}
// value representation in Cue
valStr := fmt.Sprintf("%v", val.Cue())
// escape \n
return strings.ReplaceAll(valStr, "\n", "\\n")
}
// ValueDocString returns the value doc from the comment lines
func ValueDocString(val *compiler.Value) string {
docs := []string{}
for _, c := range val.Cue().Doc() {
docs = append(docs, strings.TrimSpace(c.Text()))
}
doc := strings.Join(docs, " ")
lines := strings.Split(doc, "\n")
// Strip out FIXME, TODO, and INTERNAL comments
docs = []string{}
for _, line := range lines {
if strings.HasPrefix(line, "FIXME: ") ||
strings.HasPrefix(line, "TODO: ") ||
strings.HasPrefix(line, "INTERNAL: ") {
continue
}
if len(line) == 0 {
continue
}
docs = append(docs, line)
}
if len(docs) == 0 {
return "-"
}
return strings.Join(docs, " ")
}

View File

@@ -4,7 +4,6 @@ import (
"context"
"fmt"
"os"
"strings"
"text/tabwriter"
"go.dagger.io/dagger/client"
@@ -53,18 +52,20 @@ var listCmd = &cobra.Command{
}
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
fmt.Fprintln(w, "Input\tType\tValue\tSet by user\tDescription")
fmt.Fprintln(w, "Input\tValue\tSet by user\tDescription")
for _, inp := range inputs {
isConcrete := (inp.IsConcreteR() == nil)
_, hasDefault := inp.Default()
valStr := "-"
if isConcrete {
valStr, _ = inp.Cue().String()
}
if hasDefault {
valStr = fmt.Sprintf("%s (default)", valStr)
}
// valStr := "-"
// if isConcrete {
// valStr, _ = inp.Cue().String()
// }
// if hasDefault {
// valStr = fmt.Sprintf("%s (default)", valStr)
// }
// valStr = strings.ReplaceAll(valStr, "\n", "\\n")
if !viper.GetBool("all") {
// skip input that is not overridable
@@ -73,12 +74,11 @@ var listCmd = &cobra.Command{
}
}
fmt.Fprintf(w, "%s\t%s\t%s\t%t\t%s\n",
fmt.Fprintf(w, "%s\t%s\t%t\t%s\n",
inp.Path(),
getType(inp),
valStr,
common.FormatValue(inp),
isUserSet(st, inp),
getDocString(inp),
common.ValueDocString(inp),
)
}
@@ -103,44 +103,6 @@ func isUserSet(env *state.State, val *compiler.Value) bool {
return false
}
func getType(val *compiler.Value) string {
if val.HasAttr("artifact") {
return "dagger.#Artifact"
}
if val.HasAttr("secret") {
return "dagger.#Secret"
}
return val.Cue().IncompleteKind().String()
}
func getDocString(val *compiler.Value) string {
docs := []string{}
for _, c := range val.Cue().Doc() {
docs = append(docs, strings.TrimSpace(c.Text()))
}
doc := strings.Join(docs, " ")
lines := strings.Split(doc, "\n")
// Strip out FIXME, TODO, and INTERNAL comments
docs = []string{}
for _, line := range lines {
if strings.HasPrefix(line, "FIXME: ") ||
strings.HasPrefix(line, "TODO: ") ||
strings.HasPrefix(line, "INTERNAL: ") {
continue
}
if len(line) == 0 {
continue
}
docs = append(docs, line)
}
if len(docs) == 0 {
return "-"
}
return strings.Join(docs, " ")
}
func init() {
listCmd.Flags().BoolP("all", "a", false, "List all inputs (include non-overridable)")

View File

@@ -0,0 +1,86 @@
package output
import (
"context"
"fmt"
"os"
"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"
"go.dagger.io/dagger/state"
"github.com/rs/zerolog/log"
"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)
ListOutputs(ctx, st, true)
},
}
func ListOutputs(ctx context.Context, st *state.State, isTTY bool) {
lg := log.Ctx(ctx).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
}
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)
fmt.Fprintln(w, "Output\tValue\tDescription")
for _, out := range outputs {
fmt.Fprintf(w, "%s\t%s\t%s\n",
out.Path(),
common.FormatValue(out),
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() {
Cmd.AddCommand(
dirCmd,
)
// Cmd.AddCommand(dirCmd)
Cmd.AddCommand(listCmd)
}

View File

@@ -7,6 +7,7 @@ import (
"cuelang.org/go/cue"
"go.dagger.io/dagger/client"
"go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/cmd/output"
"go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/environment"
@@ -46,6 +47,8 @@ var upCmd = &cobra.Command{
if err := workspace.Save(ctx, st); err != nil {
lg.Fatal().Err(err).Msg("failed to update environment")
}
output.ListOutputs(ctx, st, term.IsTerminal(int(os.Stdout.Fd())))
},
}