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