|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + survey "github.com/AlecAivazis/survey/v2" |
| 7 | + "github.com/aryansharma9917/codewise-cli/pkg/config" |
| 8 | + "github.com/aryansharma9917/codewise-cli/pkg/env" |
| 9 | + "github.com/spf13/cobra" |
| 10 | +) |
| 11 | + |
| 12 | +var interactive bool |
| 13 | + |
| 14 | +var envCreateCmd = &cobra.Command{ |
| 15 | + Use: "create <name>", |
| 16 | + Short: "Create a new environment", |
| 17 | + Args: cobra.ExactArgs(1), |
| 18 | + Run: func(cmd *cobra.Command, args []string) { |
| 19 | + name := args[0] |
| 20 | + |
| 21 | + if interactive { |
| 22 | + if err := createEnvInteractive(name); err != nil { |
| 23 | + fmt.Println("error:", err) |
| 24 | + return |
| 25 | + } |
| 26 | + fmt.Println("environment", name, "created") |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + if err := env.CreateEnv(name, env.CreateOptions{}); err != nil { |
| 31 | + fmt.Println("error:", err) |
| 32 | + return |
| 33 | + } |
| 34 | + fmt.Println("environment", name, "created") |
| 35 | + }, |
| 36 | +} |
| 37 | + |
| 38 | +func init() { |
| 39 | + envCreateCmd.Flags().BoolVarP(&interactive, "interactive", "i", false, "Enable interactive mode") |
| 40 | + envCmd.AddCommand(envCreateCmd) |
| 41 | +} |
| 42 | + |
| 43 | +func createEnvInteractive(name string) error { |
| 44 | + cfg, _ := config.ReadConfig() |
| 45 | + |
| 46 | + defaultNs := fallback(cfg.Defaults.Namespace, name) |
| 47 | + defaultCtx := fallback(cfg.Defaults.Context, "") |
| 48 | + defaultRepo := fallback(cfg.Defaults.RepoURL, "") |
| 49 | + defaultBranch := fallback(cfg.Defaults.Branch, "main") |
| 50 | + defaultImage := fallback(cfg.Defaults.Image, "codewise") |
| 51 | + defaultTag := fallback(cfg.Defaults.ImageTag, "latest") |
| 52 | + |
| 53 | + answers := struct { |
| 54 | + Namespace string |
| 55 | + Context string |
| 56 | + Repo string |
| 57 | + Branch string |
| 58 | + Image string |
| 59 | + Tag string |
| 60 | + }{} |
| 61 | + |
| 62 | + qs := []*survey.Question{ |
| 63 | + {Name: "Namespace", Prompt: &survey.Input{Message: fmt.Sprintf("Namespace (default: %s)", defaultNs)}}, |
| 64 | + {Name: "Context", Prompt: &survey.Input{Message: fmt.Sprintf("Kubernetes context (default: %s)", defaultCtx)}}, |
| 65 | + {Name: "Repo", Prompt: &survey.Input{Message: fmt.Sprintf("GitOps repo (default: %s)", defaultRepo)}}, |
| 66 | + {Name: "Branch", Prompt: &survey.Input{Message: fmt.Sprintf("GitOps branch (default: %s)", defaultBranch)}}, |
| 67 | + {Name: "Image", Prompt: &survey.Input{Message: fmt.Sprintf("Image repository (default: %s)", defaultImage)}}, |
| 68 | + {Name: "Tag", Prompt: &survey.Input{Message: fmt.Sprintf("Image tag (default: %s)", defaultTag)}}, |
| 69 | + } |
| 70 | + |
| 71 | + if err := survey.Ask(qs, &answers); err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + k8s := env.K8sConfig{ |
| 76 | + Namespace: fallback(answers.Namespace, defaultNs), |
| 77 | + Context: fallback(answers.Context, defaultCtx), |
| 78 | + } |
| 79 | + |
| 80 | + helm := env.HelmConfig{ |
| 81 | + Release: name, |
| 82 | + Chart: "./helm/chart", |
| 83 | + Values: "./values.yaml", |
| 84 | + } |
| 85 | + |
| 86 | + gitops := env.GitOpsConfig{ |
| 87 | + Repo: fallback(answers.Repo, defaultRepo), |
| 88 | + Path: "", |
| 89 | + Branch: fallback(answers.Branch, defaultBranch), |
| 90 | + } |
| 91 | + |
| 92 | + values := env.ValuesConfig{} |
| 93 | + values.Image.Repository = fallback(answers.Image, defaultImage) |
| 94 | + values.Image.Tag = fallback(answers.Tag, defaultTag) |
| 95 | + |
| 96 | + return env.CreateEnvFromParts(name, k8s, helm, gitops, values) |
| 97 | +} |
| 98 | + |
| 99 | +func fallback(input, def string) string { |
| 100 | + if input != "" { |
| 101 | + return input |
| 102 | + } |
| 103 | + return def |
| 104 | +} |
0 commit comments