Merge pull request #229 from dagger/cli-new-name

Fix "dagger new --name"
This commit is contained in:
Sam Alba 2021-04-01 11:30:22 -07:00 committed by GitHub
commit 4dcddc68c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,7 +17,7 @@ import (
var newCmd = &cobra.Command{ var newCmd = &cobra.Command{
Use: "new", Use: "new",
Short: "Create a new deployment", Short: "Create a new deployment",
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
@ -33,8 +33,21 @@ var newCmd = &cobra.Command{
lg.Fatal().Err(err).Msg("failed to load store") lg.Fatal().Err(err).Msg("failed to load store")
} }
if viper.GetString("deployment") != "" {
lg.
Fatal().
Msg("cannot use option -d,--deployment for this command")
}
name := ""
if len(args) > 0 {
name = args[0]
} else {
name = getNewDeploymentName(ctx)
}
st := &dagger.DeploymentState{ st := &dagger.DeploymentState{
Name: getNewDeploymentName(ctx), Name: name,
PlanSource: getPlanSource(ctx), PlanSource: getPlanSource(ctx),
} }
@ -65,11 +78,6 @@ var newCmd = &cobra.Command{
func getNewDeploymentName(ctx context.Context) string { func getNewDeploymentName(ctx context.Context) string {
lg := log.Ctx(ctx) lg := log.Ctx(ctx)
deploymentName := viper.GetString("deployment")
if deploymentName != "" {
return deploymentName
}
workDir, err := os.Getwd() workDir, err := os.Getwd()
if err != nil { if err != nil {
lg. lg.
@ -90,16 +98,19 @@ func getNewDeploymentName(ctx context.Context) string {
func getPlanSource(ctx context.Context) dagger.Input { func getPlanSource(ctx context.Context) dagger.Input {
lg := log.Ctx(ctx) lg := log.Ctx(ctx)
wd, err := os.Getwd() planDir := viper.GetString("plan-dir")
if planDir == "" {
var err error
planDir, err = os.Getwd()
if err != nil { if err != nil {
lg.Fatal().Err(err).Msg("cannot get current working directory") lg.Fatal().Err(err).Msg("cannot get current working directory")
} }
}
return dagger.DirInput(wd, []string{"*.cue", "cue.mod"}) return dagger.DirInput(planDir, []string{"*.cue", "cue.mod"})
} }
func init() { func init() {
newCmd.Flags().StringP("name", "n", "", "Specify a deployment name")
newCmd.Flags().BoolP("up", "u", false, "Bring the deployment online") newCmd.Flags().BoolP("up", "u", false, "Bring the deployment online")
newCmd.Flags().String("plan-dir", "", "Load plan from a local directory") newCmd.Flags().String("plan-dir", "", "Load plan from a local directory")