Merge pull request #1685 from talentedmrjones/dagger-project-init
Implements dagger project init
This commit is contained in:
commit
1f5a89091c
@ -27,7 +27,7 @@ var getCmd = &cobra.Command{
|
|||||||
var err error
|
var err error
|
||||||
|
|
||||||
cueModPath := pkg.GetCueModParent()
|
cueModPath := pkg.GetCueModParent()
|
||||||
err = pkg.CueModInit(ctx, cueModPath)
|
err = pkg.CueModInit(ctx, cueModPath, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lg.Fatal().Err(err).Msg("failed to initialize cue.mod")
|
lg.Fatal().Err(err).Msg("failed to initialize cue.mod")
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package cmd
|
package project
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -9,10 +10,12 @@ import (
|
|||||||
"go.dagger.io/dagger/pkg"
|
"go.dagger.io/dagger/pkg"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var sep = string(os.PathSeparator)
|
||||||
|
|
||||||
var initCmd = &cobra.Command{
|
var initCmd = &cobra.Command{
|
||||||
Use: "init",
|
Use: fmt.Sprintf("init [path%sto%sproject]", sep, sep),
|
||||||
Short: "Initialize a new empty project",
|
Short: "Initialize a new empty project.",
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.MaximumNArgs(1),
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
// Fix Viper bug for duplicate flags:
|
// Fix Viper bug for duplicate flags:
|
||||||
// https://github.com/spf13/viper/issues/233
|
// https://github.com/spf13/viper/issues/233
|
||||||
@ -24,30 +27,24 @@ var initCmd = &cobra.Command{
|
|||||||
lg := logger.New()
|
lg := logger.New()
|
||||||
ctx := lg.WithContext(cmd.Context())
|
ctx := lg.WithContext(cmd.Context())
|
||||||
|
|
||||||
dir := viper.GetString("project")
|
dir := "."
|
||||||
if dir == "" {
|
if len(args) > 0 {
|
||||||
cwd, err := os.Getwd()
|
dir = args[0]
|
||||||
if err != nil {
|
|
||||||
lg.
|
|
||||||
Fatal().
|
|
||||||
Err(err).
|
|
||||||
Msg("failed to get current working dir")
|
|
||||||
}
|
|
||||||
dir = cwd
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err := pkg.CueModInit(ctx, dir)
|
name := viper.GetString("name")
|
||||||
|
|
||||||
|
err := pkg.CueModInit(ctx, dir, name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lg.Fatal().Err(err).Msg("failed to initialize project")
|
lg.Fatal().Err(err).Msg("failed to initialize project")
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Add telemtry for init
|
// FIXME: Add telemtry for init
|
||||||
// <-common.TrackProjectCommand(ctx, cmd, project, nil)
|
|
||||||
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
initCmd.Flags().StringP("name", "n", "", "project name")
|
||||||
if err := viper.BindPFlags(initCmd.Flags()); err != nil {
|
if err := viper.BindPFlags(initCmd.Flags()); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
29
cmd/dagger/cmd/project/root.go
Normal file
29
cmd/dagger/cmd/project/root.go
Normal 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,
|
||||||
|
)
|
||||||
|
}
|
@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"go.dagger.io/dagger/cmd/dagger/cmd/mod"
|
"go.dagger.io/dagger/cmd/dagger/cmd/mod"
|
||||||
|
"go.dagger.io/dagger/cmd/dagger/cmd/project"
|
||||||
"go.dagger.io/dagger/cmd/dagger/logger"
|
"go.dagger.io/dagger/cmd/dagger/logger"
|
||||||
|
|
||||||
"go.opentelemetry.io/otel"
|
"go.opentelemetry.io/otel"
|
||||||
@ -40,12 +41,12 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rootCmd.AddCommand(
|
rootCmd.AddCommand(
|
||||||
initCmd,
|
|
||||||
upCmd,
|
upCmd,
|
||||||
versionCmd,
|
versionCmd,
|
||||||
docCmd,
|
docCmd,
|
||||||
mod.Cmd,
|
mod.Cmd,
|
||||||
doCmd,
|
doCmd,
|
||||||
|
project.Cmd,
|
||||||
)
|
)
|
||||||
|
|
||||||
if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
|
if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
|
||||||
|
17
pkg/pkg.go
17
pkg/pkg.go
@ -57,7 +57,7 @@ func Vendor(ctx context.Context, p string) error {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// ensure cue module is initialized
|
// ensure cue module is initialized
|
||||||
if err := CueModInit(ctx, p); err != nil {
|
if err := CueModInit(ctx, p, ""); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -165,11 +165,16 @@ func GetCueModParent() string {
|
|||||||
return parentDir
|
return parentDir
|
||||||
}
|
}
|
||||||
|
|
||||||
func CueModInit(ctx context.Context, parentDir string) error {
|
func CueModInit(ctx context.Context, parentDir, module string) error {
|
||||||
lg := log.Ctx(ctx)
|
lg := log.Ctx(ctx)
|
||||||
|
|
||||||
modDir := path.Join(parentDir, "cue.mod")
|
absParentDir, err := filepath.Abs(parentDir)
|
||||||
if err := os.Mkdir(modDir, 0755); err != nil {
|
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) {
|
if !errors.Is(err, os.ErrExist) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -183,8 +188,8 @@ func CueModInit(ctx context.Context, parentDir string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
lg.Debug().Str("mod", parentDir).Msg("initializing cue.mod")
|
lg.Debug().Str("mod", parentDir).Msg("initializing cue.mod")
|
||||||
|
contents := fmt.Sprintf(`module: "%s"`, module)
|
||||||
if err := os.WriteFile(modFile, []byte("module: \"\"\n"), 0600); err != nil {
|
if err := os.WriteFile(modFile, []byte(contents), 0600); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,6 +39,7 @@ type Config struct {
|
|||||||
func Load(ctx context.Context, cfg Config) (*Plan, error) {
|
func Load(ctx context.Context, cfg Config) (*Plan, error) {
|
||||||
log.Ctx(ctx).Debug().Interface("args", cfg.Args).Msg("loading plan")
|
log.Ctx(ctx).Debug().Interface("args", cfg.Args).Msg("loading plan")
|
||||||
|
|
||||||
|
// FIXME: move vendoring to explicit project update command
|
||||||
if cfg.Vendor {
|
if cfg.Vendor {
|
||||||
// FIXME: vendoring path
|
// FIXME: vendoring path
|
||||||
if err := pkg.Vendor(ctx, ""); err != nil {
|
if err := pkg.Vendor(ctx, ""); err != nil {
|
||||||
|
16
tests/project.bats
Normal file
16
tests/project.bats
Normal 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"' ]
|
||||||
|
}
|
Reference in New Issue
Block a user