From 7bcf9a94026cd62493fdbd61355e36627297c59b Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Thu, 3 Mar 2022 16:15:36 -0700 Subject: [PATCH] Implements dagger project init Signed-off-by: Richard Jones --- cmd/dagger/cmd/mod/get.go | 2 +- cmd/dagger/cmd/{ => project}/init.go | 33 +++++++++++++--------------- cmd/dagger/cmd/project/root.go | 29 ++++++++++++++++++++++++ cmd/dagger/cmd/root.go | 3 ++- pkg/pkg.go | 17 +++++++++----- plan/plan.go | 1 + tests/project.bats | 16 ++++++++++++++ 7 files changed, 75 insertions(+), 26 deletions(-) rename cmd/dagger/cmd/{ => project}/init.go (62%) create mode 100644 cmd/dagger/cmd/project/root.go create mode 100644 tests/project.bats diff --git a/cmd/dagger/cmd/mod/get.go b/cmd/dagger/cmd/mod/get.go index 16b2d015..e668c918 100644 --- a/cmd/dagger/cmd/mod/get.go +++ b/cmd/dagger/cmd/mod/get.go @@ -27,7 +27,7 @@ var getCmd = &cobra.Command{ var err error cueModPath := pkg.GetCueModParent() - err = pkg.CueModInit(ctx, cueModPath) + err = pkg.CueModInit(ctx, cueModPath, "") if err != nil { lg.Fatal().Err(err).Msg("failed to initialize cue.mod") panic(err) diff --git a/cmd/dagger/cmd/init.go b/cmd/dagger/cmd/project/init.go similarity index 62% rename from cmd/dagger/cmd/init.go rename to cmd/dagger/cmd/project/init.go index aa575fb4..ce3b261f 100644 --- a/cmd/dagger/cmd/init.go +++ b/cmd/dagger/cmd/project/init.go @@ -1,6 +1,7 @@ -package cmd +package project import ( + "fmt" "os" "github.com/spf13/cobra" @@ -9,10 +10,12 @@ import ( "go.dagger.io/dagger/pkg" ) +var sep = string(os.PathSeparator) + var initCmd = &cobra.Command{ - Use: "init", - Short: "Initialize a new empty project", - Args: cobra.NoArgs, + Use: fmt.Sprintf("init [path%sto%sproject]", sep, sep), + Short: "Initialize a new empty project.", + Args: cobra.MaximumNArgs(1), PreRun: func(cmd *cobra.Command, args []string) { // Fix Viper bug for duplicate flags: // https://github.com/spf13/viper/issues/233 @@ -24,30 +27,24 @@ var initCmd = &cobra.Command{ lg := logger.New() ctx := lg.WithContext(cmd.Context()) - dir := viper.GetString("project") - if dir == "" { - cwd, err := os.Getwd() - if err != nil { - lg. - Fatal(). - Err(err). - Msg("failed to get current working dir") - } - dir = cwd + dir := "." + if len(args) > 0 { + dir = args[0] } - err := pkg.CueModInit(ctx, dir) + name := viper.GetString("name") + + err := pkg.CueModInit(ctx, dir, name) if err != nil { lg.Fatal().Err(err).Msg("failed to initialize project") } - // TODO: Add telemtry for init - // <-common.TrackProjectCommand(ctx, cmd, project, nil) - + // FIXME: Add telemtry for init }, } func init() { + initCmd.Flags().StringP("name", "n", "", "project name") if err := viper.BindPFlags(initCmd.Flags()); err != nil { panic(err) } diff --git a/cmd/dagger/cmd/project/root.go b/cmd/dagger/cmd/project/root.go new file mode 100644 index 00000000..733fe173 --- /dev/null +++ b/cmd/dagger/cmd/project/root.go @@ -0,0 +1,29 @@ +package project + +import ( + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +var Cmd = &cobra.Command{ + Use: "project", + Short: "Manage a Dagger project", + // Args: cobra.NoArgs, + PreRun: func(cmd *cobra.Command, args []string) { + // Fix Viper bug for duplicate flags: + // https://github.com/spf13/viper/issues/233 + if err := viper.BindPFlags(cmd.Flags()); err != nil { + panic(err) + } + }, +} + +func init() { + if err := viper.BindPFlags(Cmd.Flags()); err != nil { + panic(err) + } + + Cmd.AddCommand( + initCmd, + ) +} diff --git a/cmd/dagger/cmd/root.go b/cmd/dagger/cmd/root.go index 570dc8e1..41dbe6b7 100644 --- a/cmd/dagger/cmd/root.go +++ b/cmd/dagger/cmd/root.go @@ -8,6 +8,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" "go.dagger.io/dagger/cmd/dagger/cmd/mod" + "go.dagger.io/dagger/cmd/dagger/cmd/project" "go.dagger.io/dagger/cmd/dagger/logger" "go.opentelemetry.io/otel" @@ -40,12 +41,12 @@ func init() { } rootCmd.AddCommand( - initCmd, upCmd, versionCmd, docCmd, mod.Cmd, doCmd, + project.Cmd, ) if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil { diff --git a/pkg/pkg.go b/pkg/pkg.go index 412dd3b8..9385871e 100644 --- a/pkg/pkg.go +++ b/pkg/pkg.go @@ -57,7 +57,7 @@ func Vendor(ctx context.Context, p string) error { }() // ensure cue module is initialized - if err := CueModInit(ctx, p); err != nil { + if err := CueModInit(ctx, p, ""); err != nil { return err } @@ -165,11 +165,16 @@ func GetCueModParent() string { return parentDir } -func CueModInit(ctx context.Context, parentDir string) error { +func CueModInit(ctx context.Context, parentDir, module string) error { lg := log.Ctx(ctx) - modDir := path.Join(parentDir, "cue.mod") - if err := os.Mkdir(modDir, 0755); err != nil { + absParentDir, err := filepath.Abs(parentDir) + if err != nil { + return err + } + + modDir := path.Join(absParentDir, "cue.mod") + if err := os.MkdirAll(modDir, 0755); err != nil { if !errors.Is(err, os.ErrExist) { return err } @@ -183,8 +188,8 @@ func CueModInit(ctx context.Context, parentDir string) error { } lg.Debug().Str("mod", parentDir).Msg("initializing cue.mod") - - if err := os.WriteFile(modFile, []byte("module: \"\"\n"), 0600); err != nil { + contents := fmt.Sprintf(`module: "%s"`, module) + if err := os.WriteFile(modFile, []byte(contents), 0600); err != nil { return err } } diff --git a/plan/plan.go b/plan/plan.go index 97ee83b4..c8b0ee82 100644 --- a/plan/plan.go +++ b/plan/plan.go @@ -39,6 +39,7 @@ type Config struct { func Load(ctx context.Context, cfg Config) (*Plan, error) { log.Ctx(ctx).Debug().Interface("args", cfg.Args).Msg("loading plan") + // FIXME: move vendoring to explicit project update command if cfg.Vendor { // FIXME: vendoring path if err := pkg.Vendor(ctx, ""); err != nil { diff --git a/tests/project.bats b/tests/project.bats new file mode 100644 index 00000000..cb62cd1d --- /dev/null +++ b/tests/project.bats @@ -0,0 +1,16 @@ +setup() { + load 'helpers' + + common_setup +} + +@test "project init" { + cd "$TESTDIR" + # mkdir -p ./project/init + "$DAGGER" project init ./project/init --name "github.com/foo/bar" + test -d ./project/init/cue.mod/pkg + test -d ./project/init/cue.mod/usr + test -f ./project/init/cue.mod/module.cue + contents=$(cat ./project/init/cue.mod/module.cue) + [ "$contents" == 'module: "github.com/foo/bar"' ] +} \ No newline at end of file