Merge pull request #261 from dagger/client-api-cleanup

deployment API cleanup
This commit is contained in:
Andrea Luzzardi 2021-04-02 15:43:31 -07:00 committed by GitHub
commit 6486e159c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 18 additions and 68 deletions

View File

@ -10,23 +10,6 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
// GetCurrentDeployment returns the current selected deployment based on its abs path
func GetCurrentDeployment(ctx context.Context, store *dagger.Store) *dagger.Deployment {
lg := log.Ctx(ctx)
st := GetCurrentDeploymentState(ctx, store)
deployment, err := dagger.NewDeployment(st)
if err != nil {
lg.
Fatal().
Err(err).
Interface("deploymentState", st).
Msg("failed to init deployment")
}
return deployment
}
func GetCurrentDeploymentState(ctx context.Context, store *dagger.Store) *dagger.DeploymentState { func GetCurrentDeploymentState(ctx context.Context, store *dagger.Store) *dagger.DeploymentState {
lg := log.Ctx(ctx) lg := log.Ctx(ctx)
@ -60,14 +43,14 @@ func GetCurrentDeploymentState(ctx context.Context, store *dagger.Store) *dagger
// Re-compute a deployment (equivalent to `dagger up`). // Re-compute a deployment (equivalent to `dagger up`).
// If printOutput is true, print the JSON-encoded computed state to standard output // If printOutput is true, print the JSON-encoded computed state to standard output
func DeploymentUp(ctx context.Context, deployment *dagger.Deployment, printOutput bool) { func DeploymentUp(ctx context.Context, state *dagger.DeploymentState, printOutput bool) {
lg := log.Ctx(ctx) lg := log.Ctx(ctx)
c, err := dagger.NewClient(ctx, "") c, err := dagger.NewClient(ctx, "")
if err != nil { if err != nil {
lg.Fatal().Err(err).Msg("unable to create client") lg.Fatal().Err(err).Msg("unable to create client")
} }
output, err := c.Do(ctx, deployment, func(ctx context.Context, deployment *dagger.Deployment, s dagger.Solver) error { output, err := c.Do(ctx, state, func(ctx context.Context, deployment *dagger.Deployment, s dagger.Solver) error {
log.Ctx(ctx).Debug().Msg("bringing deployment up") log.Ctx(ctx).Debug().Msg("bringing deployment up")
return deployment.Up(ctx, s, nil) return deployment.Up(ctx, s, nil)
}) })

View File

@ -127,12 +127,7 @@ var computeCmd = &cobra.Command{
} }
} }
deployment, err := dagger.NewDeployment(st) common.DeploymentUp(ctx, st, true)
if err != nil {
lg.Fatal().Err(err).Msg("unable to initialize deployment")
}
common.DeploymentUp(ctx, deployment, true)
}, },
} }

View File

@ -1,10 +1,6 @@
package cmd package cmd
import ( import (
"dagger.io/go/cmd/dagger/cmd/common"
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -21,25 +17,7 @@ var downCmd = &cobra.Command{
} }
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
lg := logger.New() panic("not implemented")
ctx := lg.WithContext(cmd.Context())
store, err := dagger.DefaultStore()
if err != nil {
lg.Fatal().Err(err).Msg("failed to load store")
}
deployment := common.GetCurrentDeployment(ctx, store)
// TODO: Implement options: --no-cache
if err := deployment.Down(ctx, nil); err != nil {
lg.
Fatal().
Err(err).
Str("deploymentName", deployment.Name()).
Str("deploymentId", deployment.ID()).
Msg("failed to up the deployment")
}
}, },
} }

View File

@ -62,16 +62,8 @@ var newCmd = &cobra.Command{
Str("deploymentName", st.Name). Str("deploymentName", st.Name).
Msg("deployment created") Msg("deployment created")
deployment, err := dagger.NewDeployment(st)
if err != nil {
lg.
Fatal().
Err(err).
Msg("failed to initialize deployment")
}
if viper.GetBool("up") { if viper.GetBool("up") {
common.DeploymentUp(ctx, deployment, false) common.DeploymentUp(ctx, st, false)
} }
}, },
} }

View File

@ -35,11 +35,11 @@ var queryCmd = &cobra.Command{
lg.Fatal().Err(err).Msg("failed to load store") lg.Fatal().Err(err).Msg("failed to load store")
} }
deployment := common.GetCurrentDeployment(ctx, store) state := common.GetCurrentDeploymentState(ctx, store)
lg = lg.With(). lg = lg.With().
Str("deploymentName", deployment.Name()). Str("deploymentName", state.Name).
Str("deploymentId", deployment.ID()). Str("deploymentId", state.ID).
Logger() Logger()
cuePath := cue.MakePath() cuePath := cue.MakePath()
@ -51,7 +51,7 @@ var queryCmd = &cobra.Command{
if err != nil { if err != nil {
lg.Fatal().Err(err).Msg("unable to create client") lg.Fatal().Err(err).Msg("unable to create client")
} }
output, err := c.Do(ctx, deployment, nil) output, err := c.Do(ctx, state, nil)
if err != nil { if err != nil {
lg.Fatal().Err(err).Msg("failed to query deployment") lg.Fatal().Err(err).Msg("failed to query deployment")
} }

View File

@ -28,10 +28,10 @@ var upCmd = &cobra.Command{
lg.Fatal().Err(err).Msg("failed to load store") lg.Fatal().Err(err).Msg("failed to load store")
} }
deployment := common.GetCurrentDeployment(ctx, store) state := common.GetCurrentDeploymentState(ctx, store)
// TODO: Implement options: --no-cache // TODO: Implement options: --no-cache
common.DeploymentUp(ctx, deployment, false) common.DeploymentUp(ctx, state, false)
}, },
} }

View File

@ -64,10 +64,15 @@ func NewClient(ctx context.Context, host string) (*Client, error) {
type ClientDoFunc func(context.Context, *Deployment, Solver) error type ClientDoFunc func(context.Context, *Deployment, Solver) error
// FIXME: return completed *Route, instead of *compiler.Value // FIXME: return completed *Route, instead of *compiler.Value
func (c *Client) Do(ctx context.Context, deployment *Deployment, fn ClientDoFunc) (*compiler.Value, error) { func (c *Client) Do(ctx context.Context, state *DeploymentState, fn ClientDoFunc) (*compiler.Value, error) {
lg := log.Ctx(ctx) lg := log.Ctx(ctx)
eg, gctx := errgroup.WithContext(ctx) eg, gctx := errgroup.WithContext(ctx)
deployment, err := NewDeployment(state)
if err != nil {
return nil, err
}
// Spawn print function // Spawn print function
events := make(chan *bk.SolveStatus) events := make(chan *bk.SolveStatus)
eg.Go(func() error { eg.Go(func() error {
@ -85,10 +90,7 @@ func (c *Client) Do(ctx context.Context, deployment *Deployment, fn ClientDoFunc
}) })
// Spawn output retriever // Spawn output retriever
var ( var out *compiler.Value
out *compiler.Value
err error
)
eg.Go(func() error { eg.Go(func() error {
defer outr.Close() defer outr.Close()
out, err = c.outputfn(gctx, outr) out, err = c.outputfn(gctx, outr)