From fb9aae38efb68e4f2f67127532cfc83c758ba267 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Thu, 1 Apr 2021 16:45:58 -0700 Subject: [PATCH 1/3] cmd/new: added support for plan-git option Signed-off-by: Sam Alba --- cmd/dagger/cmd/new.go | 45 +++++++++++++++++++++++++++++++++++-------- dagger/input.go | 12 +++++++++++- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/cmd/dagger/cmd/new.go b/cmd/dagger/cmd/new.go index 9bfc86b2..f2f9d3b4 100644 --- a/cmd/dagger/cmd/new.go +++ b/cmd/dagger/cmd/new.go @@ -2,6 +2,7 @@ package cmd import ( "context" + "net/url" "os" "path/filepath" @@ -94,20 +95,48 @@ func getNewDeploymentName(ctx context.Context) string { return currentDir } -// FIXME: Implement options: --plan-* func getPlanSource(ctx context.Context) dagger.Input { lg := log.Ctx(ctx) - planDir := viper.GetString("plan-dir") - if planDir == "" { - var err error - planDir, err = os.Getwd() - if err != nil { - lg.Fatal().Err(err).Msg("cannot get current working directory") + src := dagger.Input{} + checkFirstSet := func() { + if src.Type != dagger.InputTypeEmpty { + lg.Fatal().Msg("only one of those options can be set: --plan-dir, --plan-git, --plan-package, --plan-file") } } - return dagger.DirInput(planDir, []string{"*.cue", "cue.mod"}) + planDir := viper.GetString("plan-dir") + planGit := viper.GetString("plan-git") + + if planDir != "" { + checkFirstSet() + src = dagger.DirInput(planDir, []string{"*.cue", "cue.mod"}) + } + + if planGit != "" { + checkFirstSet() + + u, err := url.Parse(planGit) + if err != nil { + lg.Fatal().Err(err).Str("url", planGit).Msg("cannot get current working directory") + } + ref := u.Fragment // eg. #main + u.Fragment = "" + remote := u.String() + + src = dagger.GitInput(remote, ref, "") + } + + if src.Type == dagger.InputTypeEmpty { + var err error + wd, err := os.Getwd() + if err != nil { + lg.Fatal().Err(err).Msg("cannot get current working directory") + } + return dagger.DirInput(wd, []string{"*.cue", "cue.mod"}) + } + + return src } func init() { diff --git a/dagger/input.go b/dagger/input.go index d37ee82b..cdef3a99 100644 --- a/dagger/input.go +++ b/dagger/input.go @@ -28,6 +28,7 @@ const ( InputTypeText InputType = "text" InputTypeJSON InputType = "json" InputTypeYAML InputType = "yaml" + InputTypeEmpty InputType = "" ) type Input struct { @@ -111,7 +112,16 @@ func GitInput(remote, ref, dir string) Input { } func (git gitInput) Compile() (*compiler.Value, error) { - panic("NOT IMPLEMENTED") + ref := "HEAD" + if git.Ref != "" { + ref = git.Ref + } + + return compiler.Compile("", fmt.Sprintf( + `#compute: [{do:"fetch-git", remote:"%s", ref:"%s"}]`, + git.Remote, + ref, + )) } // An input artifact loaded from a docker container From 6cbea26f7f5b12bb296ab27a2bb7b0a6946affe9 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Thu, 1 Apr 2021 16:50:05 -0700 Subject: [PATCH 2/3] cmd/plan/git: allow for optional ref (default to HEAD) Signed-off-by: Sam Alba --- cmd/dagger/cmd/input/git.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/cmd/dagger/cmd/input/git.go b/cmd/dagger/cmd/input/git.go index b3afc73a..64cc92eb 100644 --- a/cmd/dagger/cmd/input/git.go +++ b/cmd/dagger/cmd/input/git.go @@ -8,9 +8,9 @@ import ( ) var gitCmd = &cobra.Command{ - Use: "git TARGET REMOTE REF [SUBDIR]", + Use: "git TARGET REMOTE [REF] [SUBDIR]", Short: "Add a git repository as input artifact", - Args: cobra.RangeArgs(3, 4), + Args: cobra.RangeArgs(2, 4), PreRun: func(cmd *cobra.Command, args []string) { // Fix Viper bug for duplicate flags: // https://github.com/spf13/viper/issues/233 @@ -22,12 +22,17 @@ var gitCmd = &cobra.Command{ lg := logger.New() ctx := lg.WithContext(cmd.Context()) + ref := "HEAD" + if len(args) > 2 { + ref = args[2] + } + subDir := "" if len(args) > 3 { subDir = args[3] } - updateDeploymentInput(ctx, args[0], dagger.GitInput(args[1], args[2], subDir)) + updateDeploymentInput(ctx, args[0], dagger.GitInput(args[1], ref, subDir)) }, } From c3846100b37097c3aefbe6655ebdb046475e4e55 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Thu, 1 Apr 2021 17:09:22 -0700 Subject: [PATCH 3/3] 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") +}