up: store outputs and merge in query

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi
2021-04-06 17:43:12 -07:00
parent e82ef9dfce
commit 5381d0bfe1
9 changed files with 314 additions and 218 deletions

View File

@@ -2,7 +2,6 @@ package common
import (
"context"
"fmt"
"os"
"dagger.io/go/dagger"
@@ -61,22 +60,19 @@ func GetCurrentDeploymentState(ctx context.Context, store *dagger.Store) *dagger
}
// Re-compute a deployment (equivalent to `dagger up`).
// If printOutput is true, print the JSON-encoded computed state to standard output
func DeploymentUp(ctx context.Context, state *dagger.DeploymentState, printOutput bool) {
func DeploymentUp(ctx context.Context, state *dagger.DeploymentState) *dagger.DeploymentResult {
lg := log.Ctx(ctx)
c, err := dagger.NewClient(ctx, "")
if err != nil {
lg.Fatal().Err(err).Msg("unable to create client")
}
output, err := c.Do(ctx, state, func(ctx context.Context, deployment *dagger.Deployment, s dagger.Solver) error {
result, err := c.Do(ctx, state, func(ctx context.Context, deployment *dagger.Deployment, s dagger.Solver) error {
log.Ctx(ctx).Debug().Msg("bringing deployment up")
return deployment.Up(ctx, s, nil)
return deployment.Up(ctx, s)
})
if err != nil {
lg.Fatal().Err(err).Msg("failed to up deployment")
}
if printOutput {
fmt.Println(output.JSON())
}
return result
}

View File

@@ -3,6 +3,7 @@ package cmd
import (
"encoding/json"
"errors"
"fmt"
"os"
"strings"
@@ -127,7 +128,14 @@ var computeCmd = &cobra.Command{
}
}
common.DeploymentUp(ctx, st, true)
result := common.DeploymentUp(ctx, st)
cueVal, err := result.Merge()
if err != nil {
lg.Fatal().Err(err).Msg("failed to merge result")
}
fmt.Println(cueVal.JSON())
},
}

View File

@@ -63,7 +63,7 @@ var newCmd = &cobra.Command{
Msg("deployment created")
if viper.GetBool("up") {
common.DeploymentUp(ctx, st, false)
common.DeploymentUp(ctx, st)
}
},
}

View File

@@ -51,12 +51,36 @@ var queryCmd = &cobra.Command{
if err != nil {
lg.Fatal().Err(err).Msg("unable to create client")
}
output, err := c.Do(ctx, state, nil)
result, err := c.Do(ctx, state, nil)
if err != nil {
lg.Fatal().Err(err).Msg("failed to query deployment")
}
cueVal := output.LookupPath(cuePath)
cueVal := compiler.EmptyStruct()
if !viper.GetBool("no-plan") {
if err := cueVal.FillPath(cue.MakePath(), result.Plan()); err != nil {
lg.Fatal().Err(err).Msg("failed to merge plan")
}
}
if !viper.GetBool("no-input") {
if err := cueVal.FillPath(cue.MakePath(), result.Input()); err != nil {
lg.Fatal().Err(err).Msg("failed to merge plan with output")
}
}
if !viper.GetBool("no-computed") && state.Computed != "" {
computed, err := compiler.DecodeJSON("", []byte(state.Computed))
if err != nil {
lg.Fatal().Err(err).Msg("failed to decode json")
}
if err := cueVal.FillPath(cue.MakePath(), computed); err != nil {
lg.Fatal().Err(err).Msg("failed to merge plan with computed")
}
}
cueVal = cueVal.LookupPath(cuePath)
if viper.GetBool("concrete") {
if err := cueVal.IsConcreteR(); err != nil {
@@ -116,9 +140,9 @@ func init() {
// FIXME: implement the flags below
// queryCmd.Flags().String("revision", "latest", "Query a specific version of the deployment")
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")
queryCmd.Flags().BoolP("no-plan", "P", false, "Exclude plan from query")
queryCmd.Flags().BoolP("no-input", "I", false, "Exclude inputs from query")
queryCmd.Flags().BoolP("no-computed", "C", false, "Exclude computed values from query")
if err := viper.BindPFlags(queryCmd.Flags()); err != nil {
panic(err)

View File

@@ -31,7 +31,11 @@ var upCmd = &cobra.Command{
state := common.GetCurrentDeploymentState(ctx, store)
// TODO: Implement options: --no-cache
common.DeploymentUp(ctx, state, true)
result := common.DeploymentUp(ctx, state)
state.Computed = result.Computed().JSON().String()
if err := store.UpdateDeployment(ctx, state, nil); err != nil {
lg.Fatal().Err(err).Msg("failed to update deployment")
}
},
}