Merge pull request #235 from dagger/cli-query
Implemented cli query command
This commit is contained in:
commit
e0afc6304d
@ -65,9 +65,12 @@ func DeploymentUp(ctx context.Context, deployment *dagger.Deployment) {
|
||||
if err != nil {
|
||||
lg.Fatal().Err(err).Msg("unable to create client")
|
||||
}
|
||||
output, err := c.Up(ctx, deployment)
|
||||
output, err := c.Do(ctx, deployment, 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)
|
||||
})
|
||||
if err != nil {
|
||||
lg.Fatal().Err(err).Msg("failed to compute")
|
||||
lg.Fatal().Err(err).Msg("failed to up deployment")
|
||||
}
|
||||
fmt.Println(output.JSON())
|
||||
}
|
||||
|
@ -3,18 +3,20 @@ package cmd
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"cuelang.org/go/cue"
|
||||
"dagger.io/go/cmd/dagger/cmd/common"
|
||||
"dagger.io/go/cmd/dagger/logger"
|
||||
"dagger.io/go/dagger"
|
||||
"dagger.io/go/dagger/compiler"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var queryCmd = &cobra.Command{
|
||||
Use: "query [EXPR] [flags]",
|
||||
Use: "query [TARGET] [flags]",
|
||||
Short: "Query the contents of a deployment",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
// Fix Viper bug for duplicate flags:
|
||||
// https://github.com/spf13/viper/issues/233
|
||||
@ -26,6 +28,8 @@ var queryCmd = &cobra.Command{
|
||||
lg := logger.New()
|
||||
ctx := lg.WithContext(cmd.Context())
|
||||
|
||||
cueOpts := parseQueryFlags()
|
||||
|
||||
store, err := dagger.DefaultStore()
|
||||
if err != nil {
|
||||
lg.Fatal().Err(err).Msg("failed to load store")
|
||||
@ -33,31 +37,73 @@ var queryCmd = &cobra.Command{
|
||||
|
||||
deployment := common.GetCurrentDeployment(ctx, store)
|
||||
|
||||
expr := args[0]
|
||||
|
||||
out, err := deployment.Query(ctx, expr, nil)
|
||||
if err != nil {
|
||||
lg.
|
||||
Fatal().
|
||||
Err(err).
|
||||
lg = lg.With().
|
||||
Str("deploymentName", deployment.Name()).
|
||||
Str("deploymentId", deployment.ID()).
|
||||
Msg("failed to query deployment")
|
||||
Logger()
|
||||
|
||||
cuePath := cue.MakePath()
|
||||
if len(args) > 0 {
|
||||
cuePath = cue.ParsePath(args[0])
|
||||
}
|
||||
|
||||
fmt.Println(out)
|
||||
// TODO: Implement options: --no-*, --format, --revision
|
||||
c, err := dagger.NewClient(ctx, "")
|
||||
if err != nil {
|
||||
lg.Fatal().Err(err).Msg("unable to create client")
|
||||
}
|
||||
output, err := c.Do(ctx, deployment, nil)
|
||||
if err != nil {
|
||||
lg.Fatal().Err(err).Msg("failed to query deployment")
|
||||
}
|
||||
|
||||
cueVal := output.LookupPath(cuePath)
|
||||
|
||||
if viper.GetBool("concrete") {
|
||||
if err := cueVal.IsConcreteR(); err != nil {
|
||||
lg.Fatal().Err(compiler.Err(err)).Msg("not concrete")
|
||||
}
|
||||
}
|
||||
|
||||
out, err := cueVal.Source(cueOpts...)
|
||||
if err != nil {
|
||||
lg.Fatal().Err(err).Msg("failed to lookup source")
|
||||
}
|
||||
|
||||
fmt.Println(string(out))
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
queryCmd.Flags().String("revision", "latest", "Query a specific version of the deployment")
|
||||
queryCmd.Flags().StringP("format", "f", "", "Output format (json|yaml|cue|text|env)")
|
||||
func parseQueryFlags() []cue.Option {
|
||||
opts := []cue.Option{
|
||||
cue.Definitions(true),
|
||||
}
|
||||
|
||||
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")
|
||||
if viper.GetBool("concrete") {
|
||||
opts = append(opts, cue.Concrete(true))
|
||||
}
|
||||
|
||||
if viper.GetBool("show-optional") {
|
||||
opts = append(opts, cue.Optional(true))
|
||||
}
|
||||
|
||||
if viper.GetBool("show-attributes") {
|
||||
opts = append(opts, cue.Attributes(true))
|
||||
}
|
||||
|
||||
return opts
|
||||
}
|
||||
|
||||
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")
|
||||
|
||||
// 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().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")
|
||||
|
||||
if err := viper.BindPFlags(queryCmd.Flags()); err != nil {
|
||||
panic(err)
|
||||
|
@ -61,8 +61,10 @@ func NewClient(ctx context.Context, host string) (*Client, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
type ClientDoFunc func(context.Context, *Deployment, Solver) error
|
||||
|
||||
// FIXME: return completed *Route, instead of *compiler.Value
|
||||
func (c *Client) Up(ctx context.Context, deployment *Deployment) (*compiler.Value, error) {
|
||||
func (c *Client) Do(ctx context.Context, deployment *Deployment, fn ClientDoFunc) (*compiler.Value, error) {
|
||||
lg := log.Ctx(ctx)
|
||||
eg, gctx := errgroup.WithContext(ctx)
|
||||
|
||||
@ -79,7 +81,7 @@ func (c *Client) Up(ctx context.Context, deployment *Deployment) (*compiler.Valu
|
||||
outr, outw := io.Pipe()
|
||||
eg.Go(func() error {
|
||||
defer outw.Close()
|
||||
return c.buildfn(gctx, deployment, events, outw)
|
||||
return c.buildfn(gctx, deployment, fn, events, outw)
|
||||
})
|
||||
|
||||
// Spawn output retriever
|
||||
@ -96,7 +98,7 @@ func (c *Client) Up(ctx context.Context, deployment *Deployment) (*compiler.Valu
|
||||
return out, eg.Wait()
|
||||
}
|
||||
|
||||
func (c *Client) buildfn(ctx context.Context, deployment *Deployment, ch chan *bk.SolveStatus, w io.WriteCloser) error {
|
||||
func (c *Client) buildfn(ctx context.Context, deployment *Deployment, fn ClientDoFunc, ch chan *bk.SolveStatus, w io.WriteCloser) error {
|
||||
lg := log.Ctx(ctx)
|
||||
|
||||
// Scan local dirs to grant access
|
||||
@ -138,10 +140,11 @@ func (c *Client) buildfn(ctx context.Context, deployment *Deployment, ch chan *b
|
||||
}
|
||||
|
||||
// Compute output overlay
|
||||
lg.Debug().Msg("computing deployment")
|
||||
if err := deployment.Up(ctx, s, nil); err != nil {
|
||||
if fn != nil {
|
||||
if err := fn(ctx, deployment, s); err != nil {
|
||||
return nil, compiler.Err(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Export deployment to a cue directory
|
||||
// FIXME: this should be elsewhere
|
||||
@ -149,9 +152,14 @@ func (c *Client) buildfn(ctx context.Context, deployment *Deployment, ch chan *b
|
||||
span, _ := opentracing.StartSpanFromContext(ctx, "Deployment.Export")
|
||||
defer span.Finish()
|
||||
|
||||
stateSource, err := deployment.State().Source()
|
||||
if err != nil {
|
||||
return nil, compiler.Err(err)
|
||||
}
|
||||
|
||||
st := llb.Scratch().File(
|
||||
llb.Mkfile("state.cue", 0600, deployment.State().JSON()),
|
||||
llb.WithCustomName("[internal] serializing state to JSON"),
|
||||
llb.Mkfile("state.cue", 0600, stateSource),
|
||||
llb.WithCustomName("[internal] serializing state to CUE"),
|
||||
)
|
||||
ref, err := s.Solve(ctx, st)
|
||||
if err != nil {
|
||||
|
@ -204,11 +204,11 @@ func (v *Value) Validate() error {
|
||||
}
|
||||
|
||||
// Return cue source for this value
|
||||
func (v *Value) Source() ([]byte, error) {
|
||||
func (v *Value) Source(opts ...cue.Option) ([]byte, error) {
|
||||
v.cc.rlock()
|
||||
defer v.cc.runlock()
|
||||
|
||||
return cueformat.Node(v.val.Eval().Syntax())
|
||||
return cueformat.Node(v.val.Eval().Syntax(opts...))
|
||||
}
|
||||
|
||||
func (v *Value) IsEmptyStruct() bool {
|
||||
|
@ -308,10 +308,6 @@ func (d *Deployment) Down(ctx context.Context, _ *DownOpts) error {
|
||||
panic("NOT IMPLEMENTED")
|
||||
}
|
||||
|
||||
func (d *Deployment) Query(ctx context.Context, expr interface{}, o *QueryOpts) (*compiler.Value, error) {
|
||||
panic("NOT IMPLEMENTED")
|
||||
}
|
||||
|
||||
type QueryOpts struct{}
|
||||
|
||||
func newTaskFunc(inst *cue.Instance, runner cueflow.RunnerFunc) cueflow.TaskFunc {
|
||||
|
@ -1,12 +1,15 @@
|
||||
package test
|
||||
|
||||
import "dagger.io/dagger"
|
||||
import (
|
||||
"dagger.io/dagger"
|
||||
"dagger.io/llb"
|
||||
)
|
||||
|
||||
// Set to `--input-dir=./tests/dockerbuild/testdata`
|
||||
TestData: dagger.#Artifact
|
||||
|
||||
TestInlinedDockerfile: #compute: [
|
||||
dagger.#DockerBuild & {
|
||||
llb.#DockerBuild & {
|
||||
dockerfile: """
|
||||
FROM alpine:latest@sha256:ab00606a42621fb68f2ed6ad3c88be54397f981a7b70a79db3d1172b11c4367d
|
||||
RUN echo hello world
|
||||
@ -15,51 +18,51 @@ TestInlinedDockerfile: #compute: [
|
||||
]
|
||||
|
||||
TestOpChaining: #compute: [
|
||||
dagger.#DockerBuild & {
|
||||
llb.#DockerBuild & {
|
||||
dockerfile: """
|
||||
FROM alpine:latest@sha256:ab00606a42621fb68f2ed6ad3c88be54397f981a7b70a79db3d1172b11c4367d
|
||||
RUN echo foobar > /output
|
||||
"""
|
||||
},
|
||||
dagger.#Exec & {
|
||||
llb.#Exec & {
|
||||
args: ["sh", "-c", "test $(cat /output) = foobar"]
|
||||
},
|
||||
]
|
||||
|
||||
TestBuildContext: #compute: [
|
||||
dagger.#DockerBuild & {
|
||||
llb.#DockerBuild & {
|
||||
context: TestData
|
||||
},
|
||||
dagger.#Exec & {
|
||||
llb.#Exec & {
|
||||
args: ["sh", "-c", "test $(cat /dir/foo) = foobar"]
|
||||
},
|
||||
]
|
||||
|
||||
TestBuildContextAndDockerfile: #compute: [
|
||||
dagger.#DockerBuild & {
|
||||
llb.#DockerBuild & {
|
||||
context: TestData
|
||||
dockerfile: """
|
||||
FROM alpine:latest@sha256:ab00606a42621fb68f2ed6ad3c88be54397f981a7b70a79db3d1172b11c4367d
|
||||
COPY foo /override
|
||||
"""
|
||||
},
|
||||
dagger.#Exec & {
|
||||
llb.#Exec & {
|
||||
args: ["sh", "-c", "test $(cat /override) = foobar"]
|
||||
},
|
||||
]
|
||||
|
||||
TestDockerfilePath: #compute: [
|
||||
dagger.#DockerBuild & {
|
||||
llb.#DockerBuild & {
|
||||
context: TestData
|
||||
dockerfilePath: "./dockerfilepath/Dockerfile.custom"
|
||||
},
|
||||
dagger.#Exec & {
|
||||
llb.#Exec & {
|
||||
args: ["sh", "-c", "test $(cat /test) = dockerfilePath"]
|
||||
},
|
||||
]
|
||||
|
||||
TestBuildArgs: #compute: [
|
||||
dagger.#DockerBuild & {
|
||||
llb.#DockerBuild & {
|
||||
dockerfile: """
|
||||
FROM alpine:latest@sha256:ab00606a42621fb68f2ed6ad3c88be54397f981a7b70a79db3d1172b11c4367d
|
||||
ARG TEST=foo
|
||||
@ -71,7 +74,7 @@ TestBuildArgs: #compute: [
|
||||
|
||||
// FIXME: this doesn't test anything beside not crashing
|
||||
TestBuildLabels: #compute: [
|
||||
dagger.#DockerBuild & {
|
||||
llb.#DockerBuild & {
|
||||
dockerfile: """
|
||||
FROM alpine:latest@sha256:ab00606a42621fb68f2ed6ad3c88be54397f981a7b70a79db3d1172b11c4367d
|
||||
"""
|
||||
@ -81,7 +84,7 @@ TestBuildLabels: #compute: [
|
||||
|
||||
// FIXME: this doesn't test anything beside not crashing
|
||||
TestBuildPlatform: #compute: [
|
||||
dagger.#DockerBuild & {
|
||||
llb.#DockerBuild & {
|
||||
dockerfile: """
|
||||
FROM alpine:latest@sha256:ab00606a42621fb68f2ed6ad3c88be54397f981a7b70a79db3d1172b11c4367d
|
||||
"""
|
||||
|
@ -1,7 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"dagger.io/dagger"
|
||||
"dagger.io/llb"
|
||||
"dagger.io/alpine"
|
||||
)
|
||||
|
||||
@ -10,11 +10,11 @@ TestPushContainer: {
|
||||
random: {
|
||||
string
|
||||
#compute: [
|
||||
dagger.#Load & {from: alpine.#Image},
|
||||
dagger.#Exec & {
|
||||
llb.#Load & {from: alpine.#Image},
|
||||
llb.#Exec & {
|
||||
args: ["sh", "-c", "echo -n $RANDOM > /rand"]
|
||||
},
|
||||
dagger.#Export & {
|
||||
llb.#Export & {
|
||||
source: "/rand"
|
||||
},
|
||||
]
|
||||
@ -24,11 +24,11 @@ TestPushContainer: {
|
||||
push: {
|
||||
ref: "daggerio/ci-test:\(random)"
|
||||
#compute: [
|
||||
dagger.#WriteFile & {
|
||||
llb.#WriteFile & {
|
||||
content: random
|
||||
dest: "/rand"
|
||||
},
|
||||
dagger.#PushContainer & {
|
||||
llb.#PushContainer & {
|
||||
"ref": ref
|
||||
},
|
||||
]
|
||||
@ -36,15 +36,15 @@ TestPushContainer: {
|
||||
|
||||
// Pull the image back
|
||||
pull: #compute: [
|
||||
dagger.#FetchContainer & {
|
||||
llb.#FetchContainer & {
|
||||
ref: push.ref
|
||||
},
|
||||
]
|
||||
|
||||
// Check the content
|
||||
check: #compute: [
|
||||
dagger.#Load & {from: alpine.#Image},
|
||||
dagger.#Exec & {
|
||||
llb.#Load & {from: alpine.#Image},
|
||||
llb.#Exec & {
|
||||
args: [
|
||||
"sh", "-c", #"""
|
||||
test "$(cat /src/rand)" = "\#(random)"
|
||||
|
Reference in New Issue
Block a user