Skip to content

Commit 6da94a6

Browse files
committed
Initial implementation for devspace add & remove package
1 parent dc4d1ac commit 6da94a6

4 files changed

Lines changed: 307 additions & 11 deletions

File tree

cmd/add.go

Lines changed: 108 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package cmd
33
import (
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
1619
type 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+
4049
func 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

cmd/remove.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package cmd
22

33
import (
4+
"os"
5+
"path/filepath"
46
"strconv"
57
"strings"
68

79
"github.com/covexo/devspace/pkg/devspace/config/configutil"
810
"github.com/covexo/devspace/pkg/devspace/config/v1"
911
"github.com/covexo/devspace/pkg/util/log"
12+
"github.com/covexo/devspace/pkg/util/yamlutil"
1013
"github.com/spf13/cobra"
1114
)
1215

@@ -102,6 +105,71 @@ func init() {
102105
removePortCmd.Flags().BoolVar(&cmd.portFlags.RemoveAll, "all", false, "Remove all configured ports")
103106

104107
removeCmd.AddCommand(removePortCmd)
108+
109+
removePackageCmd := &cobra.Command{
110+
Use: "package",
111+
Short: "Removes forwarded ports from a devspace",
112+
Long: `
113+
#######################################################
114+
############## devspace remove package ################
115+
#######################################################
116+
Removes a package from the devspace:
117+
devspace remove package mysql
118+
#######################################################
119+
`,
120+
Args: cobra.MaximumNArgs(1),
121+
Run: cmd.RunRemovePackage,
122+
}
123+
124+
removeCmd.AddCommand(removePackageCmd)
125+
}
126+
127+
// RunRemovePackage executes the remove package command logic
128+
func (cmd *RemoveCmd) RunRemovePackage(cobraCmd *cobra.Command, args []string) {
129+
cwd, err := os.Getwd()
130+
if err != nil {
131+
log.Fatal(err)
132+
}
133+
134+
requirementsPath := filepath.Join(cwd, "chart", "requirements.yaml")
135+
yamlContents := map[interface{}]interface{}{}
136+
137+
err = yamlutil.ReadYamlFromFile(requirementsPath, yamlContents)
138+
if err != nil {
139+
log.Fatal(err)
140+
}
141+
142+
if dependencies, ok := yamlContents["dependencies"]; ok {
143+
dependenciesArr, ok := dependencies.([]interface{})
144+
if ok == false {
145+
log.Fatalf("Error parsing yaml: %v", dependencies)
146+
}
147+
148+
for key, dependency := range dependenciesArr {
149+
dependencyMap, ok := dependency.(map[interface{}]interface{})
150+
if ok == false {
151+
log.Fatalf("Error parsing yaml: %v", dependencies)
152+
}
153+
154+
if name, ok := dependencyMap["name"]; ok {
155+
if name == args[0] {
156+
dependenciesArr = append(dependenciesArr[:key], dependenciesArr[key+1:]...)
157+
yamlContents["dependencies"] = dependenciesArr
158+
159+
err = yamlutil.WriteYamlToFile(yamlContents, requirementsPath)
160+
if err != nil {
161+
log.Fatal(err)
162+
}
163+
164+
// Rebuild dependencies
165+
166+
break
167+
}
168+
}
169+
}
170+
}
171+
172+
log.Donef("Successfully removed dependency %s", args[0])
105173
}
106174

107175
// RunRemoveSync executes the remove sync command logic

cmd/status.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,14 @@ func (cmd *StatusCmd) RunStatus(cobraCmd *cobra.Command, args []string) {
7676
"NAMESPACE",
7777
"INFO",
7878
}
79-
cmd.kubectl, err = kubectl.NewClient()
8079

80+
cmd.kubectl, err = kubectl.NewClient()
8181
if err != nil {
8282
log.Fatalf("Unable to create new kubectl client: %s", err.Error())
8383
}
8484

8585
// Check if tiller server is there
8686
tillerStatus, err := cmd.getTillerStatus()
87-
8887
if err != nil {
8988
values = append(values, []string{
9089
"Tiller",
@@ -100,7 +99,6 @@ func (cmd *StatusCmd) RunStatus(cobraCmd *cobra.Command, args []string) {
10099

101100
values = append(values, tillerStatus)
102101
cmd.helm, err = helmClient.NewClient(cmd.kubectl, false)
103-
104102
if err != nil {
105103
log.Fatalf("Error initializing helm client: %s", err.Error())
106104
}

0 commit comments

Comments
 (0)