/
list.go
238 lines (196 loc) · 5.58 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package cmd
import (
"os"
"path/filepath"
"strconv"
"github.com/covexo/devspace/pkg/devspace/config/configutil"
"github.com/covexo/devspace/pkg/util/log"
"github.com/covexo/devspace/pkg/util/yamlutil"
"github.com/spf13/cobra"
)
// ListCmd holds the information needed for the list command
type ListCmd struct {
flags *ListCmdFlags
workdir string
}
// ListCmdFlags holds the possible flags for the list command
type ListCmdFlags struct {
}
func init() {
cmd := &ListCmd{
flags: &ListCmdFlags{},
}
listCmd := &cobra.Command{
Use: "list",
Short: "Lists configuration",
Long: `
#######################################################
#################### devspace list ####################
#######################################################
Lists the following configurations:
* Sync paths (sync)
* Forwarded ports (port)
#######################################################
`,
Args: cobra.NoArgs,
}
rootCmd.AddCommand(listCmd)
listSyncCmd := &cobra.Command{
Use: "sync",
Short: "Lists sync configuration",
Long: `
#######################################################
################# devspace list sync ##################
#######################################################
Lists the sync configuration
#######################################################
`,
Args: cobra.NoArgs,
Run: cmd.RunListSync,
}
listCmd.AddCommand(listSyncCmd)
listPortCmd := &cobra.Command{
Use: "port",
Short: "Lists port forwarding configuration",
Long: `
#######################################################
################ devspace list port ###################
#######################################################
Lists the port forwarding configuration
#######################################################
`,
Args: cobra.NoArgs,
Run: cmd.RunListPort,
}
listCmd.AddCommand(listPortCmd)
listPackageCmd := &cobra.Command{
Use: "package",
Short: "Lists all added packages",
Long: `
#######################################################
############### devspace list package #################
#######################################################
Lists the packages that were added to the devspace
#######################################################
`,
Args: cobra.NoArgs,
Run: cmd.RunListPackage,
}
listCmd.AddCommand(listPackageCmd)
}
// RunListPackage runs the list sync command logic
func (cmd *ListCmd) RunListPackage(cobraCmd *cobra.Command, args []string) {
headerColumnNames := []string{
"Name",
"Version",
"Repository",
}
values := [][]string{}
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
requirementsFile := filepath.Join(cwd, "chart", "requirements.yaml")
_, err = os.Stat(requirementsFile)
if os.IsNotExist(err) == false {
yamlContents := map[interface{}]interface{}{}
err = yamlutil.ReadYamlFromFile(requirementsFile, yamlContents)
if err != nil {
log.Fatalf("Error parsing %s: %v", requirementsFile, err)
}
if dependencies, ok := yamlContents["dependencies"]; ok {
if dependenciesArr, ok := dependencies.([]interface{}); ok {
for _, dependency := range dependenciesArr {
if dependencyMap, ok := dependency.(map[interface{}]interface{}); ok {
values = append(values, []string{
dependencyMap["name"].(string),
dependencyMap["version"].(string),
dependencyMap["repository"].(string),
})
}
}
}
}
}
log.PrintTable(headerColumnNames, values)
}
// RunListSync runs the list sync command logic
func (cmd *ListCmd) RunListSync(cobraCmd *cobra.Command, args []string) {
config := configutil.GetConfig(false)
if len(*config.DevSpace.Sync) == 0 {
log.Write("No sync paths are configured. Run `devspace add sync` to add new sync path\n")
return
}
headerColumnNames := []string{
"Type",
"Selector",
"Local Path",
"Container Path",
"Excluded Paths",
}
syncPaths := make([][]string, 0, len(*config.DevSpace.Sync))
// Transform values into string arrays
for _, value := range *config.DevSpace.Sync {
selector := ""
for k, v := range *value.LabelSelector {
if len(selector) > 0 {
selector += ", "
}
selector += k + "=" + *v
}
excludedPaths := ""
if value.ExcludePaths != nil {
for _, v := range *value.ExcludePaths {
if len(excludedPaths) > 0 {
excludedPaths += ", "
}
excludedPaths += v
}
}
syncPaths = append(syncPaths, []string{
*value.ResourceType,
selector,
*value.LocalSubPath,
*value.ContainerPath,
excludedPaths,
})
}
log.PrintTable(headerColumnNames, syncPaths)
}
// RunListPort runs the list port command logic
func (cmd *ListCmd) RunListPort(cobraCmd *cobra.Command, args []string) {
config := configutil.GetConfig(false)
if len(*config.DevSpace.PortForwarding) == 0 {
log.Write("No ports are forwarded. Run `devspace add port` to add a port that should be forwarded\n")
return
}
headerColumnNames := []string{
"Type",
"Selector",
"Ports (Local:Remote)",
}
portForwards := make([][]string, 0, len(*config.DevSpace.PortForwarding))
// Transform values into string arrays
for _, value := range *config.DevSpace.PortForwarding {
selector := ""
for k, v := range *value.LabelSelector {
if len(selector) > 0 {
selector += ", "
}
selector += k + "=" + *v
}
portMappings := ""
for _, v := range *value.PortMappings {
if len(portMappings) > 0 {
portMappings += ", "
}
portMappings += strconv.Itoa(*v.LocalPort) + ":" + strconv.Itoa(*v.RemotePort)
}
portForwards = append(portForwards, []string{
*value.ResourceType,
selector,
portMappings,
})
}
log.PrintTable(headerColumnNames, portForwards)
}