/
workspace.go
122 lines (96 loc) · 2.44 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
package workspace
import (
"fmt"
"github.com/pkg/errors"
"github.com/astronomerio/astro-cli/config"
"github.com/astronomerio/astro-cli/houston"
"github.com/astronomerio/astro-cli/messages"
"github.com/astronomerio/astro-cli/pkg/httputil"
"github.com/astronomerio/astro-cli/pkg/jsonstr"
)
var (
http = httputil.NewHTTPClient()
api = houston.NewHoustonClient(http)
)
// Create a workspace
func Create(label, desc string) error {
w, err := api.CreateWorkspace(label, desc)
if err != nil {
return err
}
fmt.Printf(messages.HOUSTON_WORKSPACE_CREATE_SUCCESS, w.Label, w.Uuid)
return nil
}
// List all workspaces
func List() error {
r := " %-44s %-50s"
colorFmt := "\033[33;m"
colorTrm := "\033[0m"
ws, err := api.GetWorkspaceAll()
if err != nil {
return err
}
c, err := config.GetCurrentContext()
head := fmt.Sprintf(r, "NAME", "UUID")
fmt.Println(head)
for _, w := range ws {
name := w.Label
workspace := w.Uuid
fullStr := fmt.Sprintf(r, name, workspace)
if c.Workspace == w.Uuid {
fullStr = colorFmt + fullStr + colorTrm
}
fmt.Println(fullStr)
}
return nil
}
// Delete a workspace by uuid
func Delete(uuid string) error {
ws, err := api.DeleteWorkspace(uuid)
if err != nil {
return err
}
fmt.Printf(messages.HOUSTON_WORKSPACE_DELETE_SUCCESS, ws.Label, ws.Uuid)
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 WORKSPACID")
}
return c.Workspace, nil
}
// Switch switches workspaces
func Switch(uuid string) error {
// validate workspace
_, err := api.GetWorkspace(uuid)
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(workspaceId string, args map[string]string) error {
s := jsonstr.MapToJsonObjStr(args)
ws, err := api.UpdateWorkspace(workspaceId, s)
if err != nil {
return err
}
fmt.Printf(messages.HOUSTON_WORKSPACE_UPDATE_SUCCESS, ws.Uuid)
return nil
}