@@ -3,9 +3,12 @@ package cmd
33import (
44 "fmt"
55 "os"
6+ "path/filepath"
67 "strconv"
78 "strings"
89
10+ helmClient "github.com/covexo/devspace/pkg/devspace/clients/helm"
11+ "github.com/covexo/devspace/pkg/devspace/clients/kubectl"
912 "github.com/covexo/devspace/pkg/devspace/config/configutil"
1013 "github.com/covexo/devspace/pkg/devspace/config/v1"
1114 "github.com/covexo/devspace/pkg/util/log"
@@ -14,10 +17,11 @@ import (
1417
1518// AddCmd holds the information needed for the add command
1619type AddCmd struct {
17- flags * AddCmdFlags
18- syncFlags * addSyncCmdFlags
19- portFlags * addPortCmdFlags
20- dsConfig * v1.DevSpaceConfig
20+ flags * AddCmdFlags
21+ syncFlags * addSyncCmdFlags
22+ portFlags * addPortCmdFlags
23+ packageFlags * addPackageFlags
24+ dsConfig * v1.DevSpaceConfig
2125}
2226
2327// AddCmdFlags holds the possible flags for the add command
@@ -37,11 +41,17 @@ type addPortCmdFlags struct {
3741 Selector string
3842}
3943
44+ type addPackageFlags struct {
45+ AppVersion string
46+ ChartVersion string
47+ }
48+
4049func init () {
4150 cmd := & AddCmd {
42- flags : & AddCmdFlags {},
43- syncFlags : & addSyncCmdFlags {},
44- portFlags : & addPortCmdFlags {},
51+ flags : & AddCmdFlags {},
52+ syncFlags : & addSyncCmdFlags {},
53+ portFlags : & addPortCmdFlags {},
54+ packageFlags : & addPackageFlags {},
4555 }
4656
4757 addCmd := & cobra.Command {
@@ -93,7 +103,7 @@ func init() {
93103
94104 addPortCmd := & cobra.Command {
95105 Use : "port" ,
96- Short : "Lists port forwarding configuration" ,
106+ Short : "Add a new port forward configuration" ,
97107 Long : `
98108 #######################################################
99109 ################ devspace add port ####################
@@ -111,6 +121,96 @@ func init() {
111121 addPortCmd .Flags ().StringVar (& cmd .portFlags .Selector , "selector" , "" , "Comma separated key=value selector list (e.g. release=test)" )
112122
113123 addCmd .AddCommand (addPortCmd )
124+
125+ addPackageCmd := & cobra.Command {
126+ Use : "package" ,
127+ Short : "Add a helm chart" ,
128+ Long : `
129+ #######################################################
130+ ############### devspace add package ##################
131+ #######################################################
132+ Adds an existing helm chart to the devspace
133+ (run 'devspace add package' to display all available
134+ helm charts)
135+
136+ Examples:
137+ devspace add package
138+ devspace add package mysql
139+ devspace add package mysql --app-version=5.7.14
140+ devspace add package mysql --chart-version=0.10.3
141+ #######################################################
142+ ` ,
143+ Run : cmd .RunAddPackage ,
144+ }
145+
146+ addPackageCmd .Flags ().StringVar (& cmd .packageFlags .AppVersion , "app-version" , "" , "App version" )
147+ addPackageCmd .Flags ().StringVar (& cmd .packageFlags .ChartVersion , "chart-version" , "" , "Chart version" )
148+
149+ addCmd .AddCommand (addPackageCmd )
150+ }
151+
152+ // RunAddPackage executes the add package command logic
153+ func (cmd * AddCmd ) RunAddPackage (cobraCmd * cobra.Command , args []string ) {
154+ kubectl , err := kubectl .NewClient ()
155+ if err != nil {
156+ log .Fatalf ("Unable to create new kubectl client: %v" , err )
157+ }
158+
159+ helm , err := helmClient .NewClient (kubectl , false )
160+ if err != nil {
161+ log .Fatalf ("Error initializing helm client: %v" , err )
162+ }
163+
164+ if len (args ) != 1 {
165+ helm .PrintAllAvailableCharts ()
166+ return
167+ }
168+
169+ log .StartWait ("Search Chart" )
170+ repo , version , err := helm .SearchChart (args [0 ], cmd .packageFlags .ChartVersion , cmd .packageFlags .AppVersion )
171+ log .StopWait ()
172+
173+ if err != nil {
174+ log .Fatal (err )
175+ }
176+
177+ log .Done ("Chart found" )
178+ entry := "- name: \" " + version .GetName () + "\" \n " +
179+ " version: \" " + version .GetVersion () + "\" \n " +
180+ " repository: \" " + repo .URL + "\" \n "
181+
182+ cwd , err := os .Getwd ()
183+ if err != nil {
184+ log .Fatal (err )
185+ }
186+
187+ requirementsFile := filepath .Join (cwd , "chart" , "requirements.yaml" )
188+
189+ _ , err = os .Stat (requirementsFile )
190+ if os .IsNotExist (err ) {
191+ entry = "dependencies:\n " + entry
192+ }
193+
194+ f , err := os .OpenFile (requirementsFile , os .O_APPEND | os .O_WRONLY | os .O_CREATE , 0600 )
195+ if err != nil {
196+ log .Fatal (err )
197+ }
198+
199+ defer f .Close ()
200+
201+ if _ , err = f .WriteString (entry ); err != nil {
202+ log .Fatal (err )
203+ }
204+
205+ log .StartWait ("Building chart" )
206+ err = helm .BuildDependencies (filepath .Join (cwd , "chart" ))
207+ log .StopWait ()
208+
209+ if err != nil {
210+ log .Fatal (err )
211+ }
212+
213+ log .Donef ("Successfully added %s as chart dependency" , version .GetName ())
114214}
115215
116216// RunAddSync executes the add sync command logic
0 commit comments