From c3846100b37097c3aefbe6655ebdb046475e4e55 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Thu, 1 Apr 2021 17:09:22 -0700 Subject: [PATCH] cmd/plan: implemented git and dir Signed-off-by: Sam Alba --- cmd/dagger/cmd/plan/dir.go | 8 +++++--- cmd/dagger/cmd/plan/git.go | 22 +++++++++++++++++----- cmd/dagger/cmd/plan/root.go | 26 +++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/cmd/dagger/cmd/plan/dir.go b/cmd/dagger/cmd/plan/dir.go index ee3410bd..754ae9f0 100644 --- a/cmd/dagger/cmd/plan/dir.go +++ b/cmd/dagger/cmd/plan/dir.go @@ -1,6 +1,8 @@ package plan import ( + "dagger.io/go/cmd/dagger/logger" + "dagger.io/go/dagger" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -17,10 +19,10 @@ var dirCmd = &cobra.Command{ } }, Run: func(cmd *cobra.Command, args []string) { - // lg := logger.New() - // ctx := lg.WithContext(cmd.Context()) + lg := logger.New() + ctx := lg.WithContext(cmd.Context()) - panic("not implemented") + updateDeploymentPlan(ctx, dagger.DirInput(args[0], []string{"*.cue", "cue.mod"})) }, } diff --git a/cmd/dagger/cmd/plan/git.go b/cmd/dagger/cmd/plan/git.go index 8f9c3a1f..92e39a9d 100644 --- a/cmd/dagger/cmd/plan/git.go +++ b/cmd/dagger/cmd/plan/git.go @@ -1,14 +1,16 @@ package plan import ( + "dagger.io/go/cmd/dagger/logger" + "dagger.io/go/dagger" "github.com/spf13/cobra" "github.com/spf13/viper" ) var gitCmd = &cobra.Command{ - Use: "git REMOTE REF [SUBDIR]", + Use: "git REMOTE [REF] [SUBDIR]", Short: "Load plan from a git package", - Args: cobra.MinimumNArgs(2), + Args: cobra.RangeArgs(1, 3), PreRun: func(cmd *cobra.Command, args []string) { // Fix Viper bug for duplicate flags: // https://github.com/spf13/viper/issues/233 @@ -17,10 +19,20 @@ var gitCmd = &cobra.Command{ } }, Run: func(cmd *cobra.Command, args []string) { - // lg := logger.New() - // ctx := lg.WithContext(cmd.Context()) + lg := logger.New() + ctx := lg.WithContext(cmd.Context()) - panic("not implemented") + ref := "HEAD" + if len(args) > 1 { + ref = args[1] + } + + subDir := "" + if len(args) > 2 { + subDir = args[2] + } + + updateDeploymentPlan(ctx, dagger.GitInput(args[0], ref, subDir)) }, } diff --git a/cmd/dagger/cmd/plan/root.go b/cmd/dagger/cmd/plan/root.go index 6e713d34..17e4a154 100644 --- a/cmd/dagger/cmd/plan/root.go +++ b/cmd/dagger/cmd/plan/root.go @@ -1,6 +1,13 @@ package plan -import "github.com/spf13/cobra" +import ( + "context" + + "dagger.io/go/cmd/dagger/cmd/common" + "dagger.io/go/dagger" + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" +) // Cmd exposes the top-level command var Cmd = &cobra.Command{ @@ -16,3 +23,20 @@ func init() { fileCmd, ) } + +func updateDeploymentPlan(ctx context.Context, planSource dagger.Input) { + lg := log.Ctx(ctx) + + store, err := dagger.DefaultStore() + if err != nil { + lg.Fatal().Err(err).Msg("failed to load store") + } + + st := common.GetCurrentDeploymentState(ctx, store) + st.PlanSource = planSource + + if err := store.UpdateDeployment(ctx, st, nil); err != nil { + lg.Fatal().Err(err).Str("deploymentId", st.ID).Str("deploymentName", st.Name).Msg("cannot update deployment") + } + lg.Info().Str("deploymentId", st.ID).Str("deploymentName", st.Name).Msg("updated deployment") +}