Implements dagger project init

Signed-off-by: Richard Jones <richard@dagger.io>
This commit is contained in:
Richard Jones 2022-03-03 16:15:36 -07:00
parent a21281d2eb
commit 7bcf9a9402
No known key found for this signature in database
GPG Key ID: CFB3A382EB166F4C
7 changed files with 75 additions and 26 deletions

View File

@ -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)

View File

@ -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)
}

View File

@ -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,
)
}

View File

@ -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 {

View File

@ -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
}
}

View File

@ -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 {

16
tests/project.bats Normal file
View File

@ -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"' ]
}