/
workspace.go
209 lines (164 loc) · 3.99 KB
/
workspace.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
package workspace
import (
"fmt"
"strconv"
"github.com/pkg/errors"
"github.com/astronomerio/astro-cli/config"
"github.com/astronomerio/astro-cli/houston"
"github.com/astronomerio/astro-cli/pkg/input"
"github.com/astronomerio/astro-cli/pkg/printutil"
)
var (
tab = printutil.Table{
Padding: []int{44, 50},
Header: []string{"NAME", "UUID"},
ColorRowCode: [2]string{"\033[1;32m", "\033[0m"},
}
)
// Create a workspace
func Create(label, desc string) error {
req := houston.Request{
Query: houston.WorkspaceCreateRequest,
Variables: map[string]interface{}{"label": label, "description": desc},
}
r, err := req.Do()
if err != nil {
return err
}
w := r.Data.CreateWorkspace
tab.AddRow([]string{w.Label, w.Uuid}, false)
tab.SuccessMsg = "\n Successfully created workspace"
tab.Print()
return nil
}
// List all workspaces
func List() error {
req := houston.Request{
Query: houston.WorkspacesGetRequest,
}
r, err := req.Do()
if err != nil {
return err
}
ws := r.Data.GetWorkspaces
c, err := config.GetCurrentContext()
for _, w := range ws {
name := w.Label
workspace := w.Uuid
if c.Workspace == w.Uuid {
tab.AddRow([]string{name, workspace}, true)
} else {
tab.AddRow([]string{name, workspace}, false)
}
}
tab.Print()
return nil
}
// Delete a workspace by uuid
func Delete(uuid string) error {
req := houston.Request{
Query: houston.WorkspaceDeleteRequest,
Variables: map[string]interface{}{"workspaceUuid": uuid},
}
_, err := req.Do()
if err != nil {
return err
}
// TODO remove tab print until houston properly returns attrs on delete
// tab.AddRow([]string{w.Label, w.Uuid}, false)
// tab.SuccessMsg = "\n Successfully deleted workspace"
// tab.Print()
fmt.Println("\n Successfully deleted workspace")
return nil
}
// GetCurrentWorkspace gets the current workspace set in context config
// Returns a string representing the current workspace and an error if it doesn't exist
func GetCurrentWorkspace() (string, error) {
c, err := config.GetCurrentContext()
if err != nil {
return "", err
}
if len(c.Workspace) == 0 {
return "", errors.New("Current workspace context not set, you can switch to a workspace with \n\tastro workspace switch WORKSPACEID")
}
return c.Workspace, nil
}
func getWorkspaceSelection() (string, error) {
tab.GetUserInput = true
req := houston.Request{
Query: houston.WorkspacesGetRequest,
}
r, err := req.Do()
if err != nil {
return "", err
}
ws := r.Data.GetWorkspaces
c, err := config.GetCurrentContext()
for _, w := range ws {
name := w.Label
workspace := w.Uuid
if c.Workspace == w.Uuid {
tab.AddRow([]string{name, workspace}, true)
} else {
tab.AddRow([]string{name, workspace}, false)
}
}
tab.Print()
in := input.InputText("\n> ")
i, err := strconv.ParseInt(
in,
10,
64,
)
if err != nil {
return "", errors.Wrapf(err, "cannot parse %s to int", in)
}
return ws[i-1].Uuid, nil
}
// Switch switches workspaces
func Switch(uuid string) error {
if len(uuid) == 0 {
_uuid, err := getWorkspaceSelection()
if err != nil {
return err
}
uuid = _uuid
}
// validate workspace
req := houston.Request{
Query: houston.WorkspacesGetRequest,
Variables: map[string]interface{}{"workspaceUuid": uuid},
}
_, err := req.Do()
if err != nil {
return errors.Wrap(err, "workspace uuid is not valid")
}
c, err := config.GetCurrentContext()
if err != nil {
return err
}
c.Workspace = uuid
err = c.SetContext()
if err != nil {
return err
}
config.PrintCurrentContext()
return nil
}
// Update an astronomer workspace
func Update(uuid string, args map[string]string) error {
// validate workspace
req := houston.Request{
Query: houston.WorkspaceUpdateRequest,
Variables: map[string]interface{}{"workspaceUuid": uuid, "payload": args},
}
r, err := req.Do()
if err != nil {
return err
}
w := r.Data.UpdateWorkspace
tab.AddRow([]string{w.Label, w.Uuid}, false)
tab.SuccessMsg = "\n Successfully updated workspace"
tab.Print()
return nil
}