dagger query [--format cue|json|text|yaml] (default is json)

Signed-off-by: Solomon Hykes <sh.github.6811@hykes.org>
This commit is contained in:
Solomon Hykes 2021-04-02 22:47:59 +00:00
parent 11825932cc
commit e2e56a5143
4 changed files with 40 additions and 13 deletions

View File

@ -64,12 +64,27 @@ var queryCmd = &cobra.Command{
}
}
out, err := cueVal.Source(cueOpts...)
if err != nil {
lg.Fatal().Err(err).Msg("failed to lookup source")
format := viper.GetString("format")
switch format {
case "cue":
out, err := cueVal.Source(cueOpts...)
if err != nil {
lg.Fatal().Err(err).Msg("failed to lookup source")
}
fmt.Println(string(out))
case "json":
fmt.Println(cueVal.JSON().PrettyString())
case "yaml":
lg.Fatal().Err(err).Msg("yaml format not yet implemented")
case "text":
out, err := cueVal.String()
if err != nil {
lg.Fatal().Err(err).Msg("value can't be formatted as text")
}
fmt.Println(out)
default:
lg.Fatal().Msgf("unsupported format: %q", format)
}
fmt.Println(string(out))
},
}
@ -95,12 +110,12 @@ func parseQueryFlags() []cue.Option {
func init() {
queryCmd.Flags().BoolP("concrete", "c", false, "Require the evaluation to be concrete")
queryCmd.Flags().BoolP("show-optional", "O", false, "Display optional fields")
queryCmd.Flags().BoolP("show-attributes", "A", false, "Display field attributes")
queryCmd.Flags().BoolP("show-optional", "O", false, "Display optional fields (cue format only)")
queryCmd.Flags().BoolP("show-attributes", "A", false, "Display field attributes (cue format only)")
// FIXME: implement the flags below
// queryCmd.Flags().String("revision", "latest", "Query a specific version of the deployment")
// queryCmd.Flags().StringP("format", "f", "", "Output format (json|yaml|cue|text|env)")
queryCmd.Flags().StringP("format", "f", "json", "Output format (json|yaml|cue|text|env)")
// queryCmd.Flags().BoolP("no-input", "I", false, "Exclude inputs from query")
// queryCmd.Flags().BoolP("no-output", "O", false, "Exclude outputs from query")
// queryCmd.Flags().BoolP("no-plan", "P", false, "Exclude outputs from query")

View File

@ -17,7 +17,7 @@ import (
var rootCmd = &cobra.Command{
Use: "dagger",
Short: "A system for application delivery as code (ADC)",
Short: "A programmable deployment system",
}
func init() {

View File

@ -1,6 +1,8 @@
package compiler
import (
"bytes"
"encoding/json"
"fmt"
"cuelang.org/go/cue"
@ -135,3 +137,13 @@ func (s JSON) String() string {
}
return string(s)
}
func (s JSON) PrettyString() string {
raw := s.String()
b := &bytes.Buffer{}
// If indenting fails, return raw string
if err := json.Indent(b, []byte(raw), "", " "); err != nil {
return raw
}
return b.String()
}

View File

@ -51,7 +51,7 @@ test::cli::newdir() {
foo: "value"
bar: "another value"
}' \
"$dagger" "${DAGGER_BINARY_ARGS[@]}" query -d "simple" -c
"$dagger" "${DAGGER_BINARY_ARGS[@]}" query -f cue -d "simple" -c
}
test::cli::query() {
@ -69,14 +69,14 @@ test::cli::query() {
foo: "value"
bar: "another value"
}' \
"$dagger" "${DAGGER_BINARY_ARGS[@]}" query -d "simple" -c
"$dagger" "${DAGGER_BINARY_ARGS[@]}" query -f cue -d "simple" -c
test::one "CLI: query: target" --stdout='"value"' \
"$dagger" "${DAGGER_BINARY_ARGS[@]}" query -d "simple" foo
"$dagger" "${DAGGER_BINARY_ARGS[@]}" query -f cue -d "simple" foo
test::one "CLI: query: initialize nonconcrete" \
"$dagger" "${DAGGER_BINARY_ARGS[@]}" new --plan-dir "$d"/cli/nonconcrete nonconcrete
test::one "CLI: query: non concrete" --exit=1 \
"$dagger" "${DAGGER_BINARY_ARGS[@]}" query -d "nonconcrete" -c
"$dagger" "${DAGGER_BINARY_ARGS[@]}" query -f cue -d "nonconcrete" -c
}