Split the client up into resource-specific files
This commit is contained in:
52
client/workspace.go
Normal file
52
client/workspace.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user