|
| 1 | +package add |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/devspace-cloud/devspace/pkg/devspace/chart" |
| 5 | + "github.com/devspace-cloud/devspace/pkg/devspace/config/configutil" |
| 6 | + "github.com/devspace-cloud/devspace/pkg/devspace/config/versions/latest" |
| 7 | + "github.com/devspace-cloud/devspace/pkg/util/log" |
| 8 | + "github.com/devspace-cloud/devspace/pkg/util/stdinutil" |
| 9 | + "github.com/mgutz/ansi" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +type componentCmd struct { |
| 14 | + Deployment string |
| 15 | +} |
| 16 | + |
| 17 | +func newComponentCmd() *cobra.Command { |
| 18 | + cmd := &componentCmd{} |
| 19 | + |
| 20 | + componentCmd := &cobra.Command{ |
| 21 | + Use: "component [name]", |
| 22 | + Short: "Add a component to the chart", |
| 23 | + Long: ` |
| 24 | +####################################################### |
| 25 | +############## devspace add component ################# |
| 26 | +####################################################### |
| 27 | +Adds a component to the chart. |
| 28 | +Run 'devspace list available-components' to see all available components |
| 29 | +
|
| 30 | +Examples: |
| 31 | +devspace add component mysql |
| 32 | +####################################################### |
| 33 | + `, |
| 34 | + Args: cobra.ExactArgs(1), |
| 35 | + Run: cmd.RunAddComponent, |
| 36 | + } |
| 37 | + |
| 38 | + componentCmd.Flags().StringVarP(&cmd.Deployment, "deployment", "d", "", "The deployment name to use") |
| 39 | + |
| 40 | + return componentCmd |
| 41 | +} |
| 42 | + |
| 43 | +// RunAddPackage executes the add package command logic |
| 44 | +func (cmd *componentCmd) RunAddComponent(cobraCmd *cobra.Command, args []string) { |
| 45 | + // Set config root |
| 46 | + configExists, err := configutil.SetDevSpaceRoot() |
| 47 | + if err != nil { |
| 48 | + log.Fatal(err) |
| 49 | + } |
| 50 | + if !configExists { |
| 51 | + log.Fatal("Couldn't find a DevSpace configuration. Please run `devspace init`") |
| 52 | + } |
| 53 | + |
| 54 | + // Get config |
| 55 | + config := configutil.GetConfig() |
| 56 | + if config.Deployments == nil || len(*config.Deployments) == 0 { |
| 57 | + log.Fatal("No deployment specified") |
| 58 | + } |
| 59 | + |
| 60 | + // get all helm deployments |
| 61 | + var helmDeployments []*latest.DeploymentConfig |
| 62 | + for _, deploy := range *config.Deployments { |
| 63 | + if deploy.Helm != nil { |
| 64 | + helmDeployments = append(helmDeployments, deploy) |
| 65 | + } |
| 66 | + } |
| 67 | + if len(helmDeployments) == 0 { |
| 68 | + log.Fatal("There is no helm deployment specified in configuration") |
| 69 | + } |
| 70 | + |
| 71 | + helmDeployment := helmDeployments[0] |
| 72 | + if cmd.Deployment != "" { |
| 73 | + found := false |
| 74 | + for _, deploy := range helmDeployments { |
| 75 | + if *deploy.Name == cmd.Deployment { |
| 76 | + helmDeployment = deploy |
| 77 | + found = true |
| 78 | + break |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if found == false { |
| 83 | + log.Fatalf("Couldn't find a deployment with name %s", cmd.Deployment) |
| 84 | + } |
| 85 | + } else if len(helmDeployments) > 1 { |
| 86 | + deployments := []string{} |
| 87 | + for _, deploy := range helmDeployments { |
| 88 | + deployments = append(deployments, *deploy.Name) |
| 89 | + } |
| 90 | + |
| 91 | + deploymentName := *stdinutil.GetFromStdin(&stdinutil.GetFromStdinParams{ |
| 92 | + Question: "Select a deployment", |
| 93 | + Options: deployments, |
| 94 | + }) |
| 95 | + |
| 96 | + for _, deploy := range helmDeployments { |
| 97 | + if *deploy.Name == deploymentName { |
| 98 | + helmDeployment = deploy |
| 99 | + break |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // Add the component to the chart |
| 105 | + err = chart.AddComponent(*helmDeployment.Helm.ChartPath, args[0]) |
| 106 | + if err != nil { |
| 107 | + log.Fatalf("Error adding component %s: %v", args[0], err) |
| 108 | + } |
| 109 | + |
| 110 | + log.Donef("Successfully added the component %s\nRun:\n- `%s` to update the application", args[0], ansi.Color("devspace deploy", "white+b")) |
| 111 | +} |
0 commit comments