Split the client up into resource-specific files

This commit is contained in:
Ian Gulliver
2021-09-23 04:30:50 +00:00
parent 31985ee5c6
commit e85fbd5df9
10 changed files with 459 additions and 432 deletions

52
client/workspace.go Normal file
View File

@@ -0,0 +1,52 @@
package client
import "fmt"
type Workspace struct {
GID string `json:"gid"`
Name string `json:"name"`
}
type workspacesResponse struct {
Data []*Workspace `json:"data"`
}
func (c *Client) InWorkspace(name string) (*WorkspaceClient, error) {
wrk, err := c.GetWorkspaceByName(name)
if err != nil {
return nil, err
}
return &WorkspaceClient{
client: c,
workspace: wrk,
}, nil
}
func (c *Client) GetWorkspaces() ([]*Workspace, error) {
resp := &workspacesResponse{}
err := c.get("workspaces", nil, resp)
if err != nil {
return nil, err
}
return resp.Data, nil
}
func (c *Client) GetWorkspaceByName(name string) (*Workspace, error) {
wrks, err := c.GetWorkspaces()
if err != nil {
return nil, err
}
for _, wrk := range wrks {
if wrk.Name == name {
return wrk, nil
}
}
return nil, fmt.Errorf("Workspace `%s` not found", name)
}
func (wrk *Workspace) String() string {
return fmt.Sprintf("%s (%s)", wrk.GID, wrk.Name)
}