cmd/dagger: share code for input/output management

Signed-off-by: Sam Alba <sam.alba@gmail.com>
This commit is contained in:
Sam Alba 2021-05-31 11:08:26 +02:00
parent 5ab66cc5ef
commit ba5078ef26
2 changed files with 44 additions and 41 deletions

View File

@ -2,10 +2,12 @@ package common
import ( import (
"context" "context"
"strings"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/client" "go.dagger.io/dagger/client"
"go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/environment" "go.dagger.io/dagger/environment"
"go.dagger.io/dagger/solver" "go.dagger.io/dagger/solver"
"go.dagger.io/dagger/state" "go.dagger.io/dagger/state"
@ -97,3 +99,43 @@ func EnvironmentUp(ctx context.Context, state *state.State, noCache bool) *envir
} }
return result return result
} }
// ValueType returns the String representation of the cue value
func ValueType(val *compiler.Value) string {
if val.HasAttr("artifact") {
return "dagger.#Artifact"
}
if val.HasAttr("secret") {
return "dagger.#Secret"
}
return val.Cue().IncompleteKind().String()
}
// 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" "context"
"fmt" "fmt"
"os" "os"
"strings"
"text/tabwriter" "text/tabwriter"
"go.dagger.io/dagger/client" "go.dagger.io/dagger/client"
@ -75,10 +74,10 @@ var listCmd = &cobra.Command{
fmt.Fprintf(w, "%s\t%s\t%s\t%t\t%s\n", fmt.Fprintf(w, "%s\t%s\t%s\t%t\t%s\n",
inp.Path(), inp.Path(),
getType(inp), common.ValueType(inp),
valStr, valStr,
isUserSet(st, inp), isUserSet(st, inp),
getDocString(inp), common.ValueDocString(inp),
) )
} }
@ -103,44 +102,6 @@ func isUserSet(env *state.State, val *compiler.Value) bool {
return false 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() { func init() {
listCmd.Flags().BoolP("all", "a", false, "List all inputs (include non-overridable)") listCmd.Flags().BoolP("all", "a", false, "List all inputs (include non-overridable)")