11package cmd
22
33import (
4- "path/filepath"
5- "strconv"
6- "strings"
7-
8- "github.com/covexo/devspace/pkg/devspace/config/configutil"
9- "github.com/covexo/devspace/pkg/devspace/config/v1"
10- helmClient "github.com/covexo/devspace/pkg/devspace/helm"
11- "github.com/covexo/devspace/pkg/devspace/kubectl"
4+ "github.com/covexo/devspace/pkg/devspace/configure"
125 "github.com/covexo/devspace/pkg/util/log"
13- "github.com/covexo/devspace/pkg/util/yamlutil"
146 "github.com/spf13/cobra"
157)
168
179// RemoveCmd holds the information needed for the remove command
1810type RemoveCmd struct {
19- syncFlags * removeSyncCmdFlags
20- portFlags * removePortCmdFlags
21- packageFlags * removePackageCmdFlags
11+ syncFlags * removeSyncCmdFlags
12+ portFlags * removePortCmdFlags
13+ packageFlags * removePackageCmdFlags
14+ deploymentFlags * removeDeploymentCmdFlags
2215}
2316
2417type removeSyncCmdFlags struct {
@@ -38,11 +31,16 @@ type removePackageCmdFlags struct {
3831 Deployment string
3932}
4033
34+ type removeDeploymentCmdFlags struct {
35+ RemoveAll bool
36+ }
37+
4138func init () {
4239 cmd := & RemoveCmd {
43- syncFlags : & removeSyncCmdFlags {},
44- portFlags : & removePortCmdFlags {},
45- packageFlags : & removePackageCmdFlags {},
40+ syncFlags : & removeSyncCmdFlags {},
41+ portFlags : & removePortCmdFlags {},
42+ packageFlags : & removePackageCmdFlags {},
43+ deploymentFlags : & removeDeploymentCmdFlags {},
4644 }
4745
4846 removeCmd := & cobra.Command {
@@ -131,213 +129,60 @@ func init() {
131129 removePackageCmd .Flags ().BoolVar (& cmd .packageFlags .RemoveAll , "all" , false , "Remove all packages" )
132130 removePackageCmd .Flags ().StringVarP (& cmd .packageFlags .Deployment , "deployment" , "d" , "" , "The deployment name to use" )
133131 removeCmd .AddCommand (removePackageCmd )
134- }
135-
136- // RunRemovePackage executes the remove package command logic
137- func (cmd * RemoveCmd ) RunRemovePackage (cobraCmd * cobra.Command , args []string ) {
138- config := configutil .GetConfig ()
139- if config .DevSpace .Deployments == nil || (len (* config .DevSpace .Deployments ) != 1 && cmd .packageFlags .Deployment == "" ) {
140- log .Fatalf ("Please specify the deployment via the -d flag" )
141- }
142-
143- var deploymentConfig * v1.DeploymentConfig
144- for _ , deployConfig := range * config .DevSpace .Deployments {
145- if cmd .packageFlags .Deployment == "" || cmd .packageFlags .Deployment == * deployConfig .Name {
146- if deployConfig .Helm == nil || deployConfig .Helm .ChartPath == nil {
147- log .Fatalf ("Selected deployment %s is not a valid helm deployment" , * deployConfig .Name )
148- }
149132
150- deploymentConfig = deployConfig
151- break
152- }
153- }
154-
155- if deploymentConfig == nil {
156- log .Fatalf ("Deployment %s not found" , cmd .packageFlags .Deployment )
133+ removeDeploymentCmd := & cobra.Command {
134+ Use : "deployment" ,
135+ Short : "Removes one or all deployments from the devspace" ,
136+ Long : `
137+ #######################################################
138+ ############ devspace remove deployment ###############
139+ #######################################################
140+ Removes one or all deployments from a devspace:
141+ devspace remove deployment devspace-default
142+ devspace remove deployment --all
143+ #######################################################
144+ ` ,
145+ Args : cobra .MaximumNArgs (1 ),
146+ Run : cmd .RunRemoveDeployment ,
157147 }
158148
159- chartPath , err := filepath .Abs (* deploymentConfig .Helm .ChartPath )
160- if err != nil {
161- log .Fatal (err )
162- }
149+ removeDeploymentCmd .Flags ().BoolVar (& cmd .deploymentFlags .RemoveAll , "all" , false , "Remove all deployments" )
150+ removeCmd .AddCommand (removeDeploymentCmd )
151+ }
163152
164- if len (args ) == 0 && cmd .packageFlags .RemoveAll == false {
165- log .Fatal ("You need to specify a package name or the --all flag" )
153+ // RunRemoveDeployment executes the specified deployment
154+ func (cmd * RemoveCmd ) RunRemoveDeployment (cobraCmd * cobra.Command , args []string ) {
155+ name := ""
156+ if len (args ) > 0 {
157+ name = args [0 ]
166158 }
167159
168- requirementsPath := filepath .Join (chartPath , "requirements.yaml" )
169- yamlContents := map [interface {}]interface {}{}
170-
171- err = yamlutil .ReadYamlFromFile (requirementsPath , yamlContents )
160+ err := configure .RemoveDeployment (cmd .deploymentFlags .RemoveAll , name )
172161 if err != nil {
173162 log .Fatal (err )
174163 }
175-
176- if dependencies , ok := yamlContents ["dependencies" ]; ok {
177- dependenciesArr , ok := dependencies .([]interface {})
178- if ok == false {
179- log .Fatalf ("Error parsing yaml: %v" , dependencies )
180- }
181-
182- if cmd .packageFlags .RemoveAll == false {
183- for key , dependency := range dependenciesArr {
184- dependencyMap , ok := dependency .(map [interface {}]interface {})
185- if ok == false {
186- log .Fatalf ("Error parsing yaml: %v" , dependencies )
187- }
188-
189- if name , ok := dependencyMap ["name" ]; ok {
190- if name == args [0 ] {
191- dependenciesArr = append (dependenciesArr [:key ], dependenciesArr [key + 1 :]... )
192- yamlContents ["dependencies" ] = dependenciesArr
193-
194- cmd .rebuildDependencies (chartPath , yamlContents )
195- break
196- }
197- }
198- }
199-
200- log .Donef ("Successfully removed dependency %s" , args [0 ])
201- return
202- }
203-
204- yamlContents ["dependencies" ] = []interface {}{}
205-
206- cmd .rebuildDependencies (chartPath , yamlContents )
207- log .Done ("Successfully removed all dependencies" )
208- return
209- }
210-
211- log .Done ("No dependencies found" )
212164}
213165
214- func (cmd * RemoveCmd ) rebuildDependencies (chartPath string , newYamlContents map [interface {}]interface {}) {
215- err := yamlutil .WriteYamlToFile (newYamlContents , filepath .Join (chartPath , "requirements.yaml" ))
216- if err != nil {
217- log .Fatal (err )
218- }
219-
220- // Rebuild dependencies
221- kubectl , err := kubectl .NewClient ()
222- if err != nil {
223- log .Fatalf ("Unable to create new kubectl client: %v" , err )
224- }
225-
226- helm , err := helmClient .NewClient (kubectl , log .GetInstance (), false )
227- if err != nil {
228- log .Fatalf ("Error initializing helm client: %v" , err )
229- }
230-
231- log .StartWait ("Update chart dependencies" )
232- err = helm .UpdateDependencies (chartPath )
233- log .StopWait ()
234-
166+ // RunRemovePackage executes the remove package command logic
167+ func (cmd * RemoveCmd ) RunRemovePackage (cobraCmd * cobra.Command , args []string ) {
168+ err := configure .RemovePackage (cmd .packageFlags .RemoveAll , cmd .packageFlags .Deployment , args , log .GetInstance ())
235169 if err != nil {
236170 log .Fatal (err )
237171 }
238172}
239173
240174// RunRemoveSync executes the remove sync command logic
241175func (cmd * RemoveCmd ) RunRemoveSync (cobraCmd * cobra.Command , args []string ) {
242- config := configutil .GetConfig ()
243- labelSelectorMap , err := parseSelectors (cmd .syncFlags .Selector )
244-
176+ err := configure .RemoveSyncPath (cmd .syncFlags .RemoveAll , cmd .syncFlags .LocalPath , cmd .syncFlags .ContainerPath , cmd .syncFlags .Selector )
245177 if err != nil {
246- log .Fatalf ("Error parsing selectors: %s" , err .Error ())
247- }
248-
249- if len (labelSelectorMap ) == 0 && cmd .syncFlags .RemoveAll == false && cmd .syncFlags .LocalPath == "" && cmd .syncFlags .ContainerPath == "" {
250- log .Errorf ("You have to specify at least one of the supported flags" )
251- cobraCmd .Help ()
252-
253- return
254- }
255-
256- if config .DevSpace .Sync != nil && len (* config .DevSpace .Sync ) > 0 {
257- newSyncPaths := make ([]* v1.SyncConfig , 0 , len (* config .DevSpace .Sync )- 1 )
258-
259- for _ , v := range * config .DevSpace .Sync {
260- if cmd .syncFlags .RemoveAll ||
261- cmd .syncFlags .LocalPath == * v .LocalSubPath ||
262- cmd .syncFlags .ContainerPath == * v .ContainerPath ||
263- isMapEqual (labelSelectorMap , * v .LabelSelector ) {
264- continue
265- }
266-
267- newSyncPaths = append (newSyncPaths , v )
268- }
269-
270- config .DevSpace .Sync = & newSyncPaths
271-
272- err = configutil .SaveConfig ()
273- if err != nil {
274- log .Fatalf ("Couldn't save config file: %s" , err .Error ())
275- }
178+ log .Fatal (err )
276179 }
277180}
278181
279182// RunRemovePort executes the remove port command logic
280183func (cmd * RemoveCmd ) RunRemovePort (cobraCmd * cobra.Command , args []string ) {
281- config := configutil .GetConfig ()
282-
283- labelSelectorMap , err := parseSelectors (cmd .portFlags .Selector )
184+ err := configure .RemovePort (cmd .portFlags .RemoveAll , cmd .portFlags .Selector , args )
284185 if err != nil {
285- log .Fatalf ("Error parsing selectors: %s" , err .Error ())
286- }
287-
288- argPorts := ""
289- if len (args ) == 1 {
290- argPorts = args [0 ]
291- }
292-
293- if len (labelSelectorMap ) == 0 && cmd .portFlags .RemoveAll == false && argPorts == "" {
294- log .Errorf ("You have to specify at least one of the supported flags" )
295- cobraCmd .Help ()
296-
297- return
298- }
299-
300- ports := strings .Split (argPorts , "," )
301-
302- if config .DevSpace .Ports != nil && len (* config .DevSpace .Ports ) > 0 {
303- newPortForwards := make ([]* v1.PortForwardingConfig , 0 , len (* config .DevSpace .Ports )- 1 )
304-
305- for _ , v := range * config .DevSpace .Ports {
306- if cmd .portFlags .RemoveAll || isMapEqual (labelSelectorMap , * v .LabelSelector ) {
307- continue
308- }
309-
310- newPortMappings := []* v1.PortMapping {}
311-
312- for _ , pm := range * v .PortMappings {
313- if containsPort (strconv .Itoa (* pm .LocalPort ), ports ) || containsPort (strconv .Itoa (* pm .RemotePort ), ports ) {
314- continue
315- }
316-
317- newPortMappings = append (newPortMappings , pm )
318- }
319-
320- if len (newPortMappings ) > 0 {
321- v .PortMappings = & newPortMappings
322- newPortForwards = append (newPortForwards , v )
323- }
324- }
325-
326- config .DevSpace .Ports = & newPortForwards
327-
328- err = configutil .SaveConfig ()
329- if err != nil {
330- log .Fatalf ("Couldn't save config file: %s" , err .Error ())
331- }
332- }
333- }
334-
335- func containsPort (port string , ports []string ) bool {
336- for _ , v := range ports {
337- if strings .TrimSpace (v ) == port {
338- return true
339- }
186+ log .Fatal (err )
340187 }
341-
342- return false
343188}
0 commit comments