2021-09-23 04:30:50 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
|
|
import "fmt"
|
2021-09-24 04:46:38 +00:00
|
|
|
import "net/url"
|
2021-09-23 04:30:50 +00:00
|
|
|
|
|
|
|
|
type Workspace struct {
|
|
|
|
|
GID string `json:"gid"`
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type workspacesResponse struct {
|
2021-09-24 04:46:38 +00:00
|
|
|
Data []*Workspace `json:"data"`
|
|
|
|
|
NextPage *nextPage `json:"next_page"`
|
2021-09-23 04:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-24 04:53:22 +00:00
|
|
|
func (c *Client) GetWorkspaces() (ret []*Workspace, err error) {
|
2021-09-24 04:46:38 +00:00
|
|
|
values := &url.Values{}
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
resp := &workspacesResponse{}
|
2021-09-24 04:53:22 +00:00
|
|
|
err = c.get("workspaces", values, resp)
|
2021-09-24 04:46:38 +00:00
|
|
|
if err != nil {
|
2021-09-24 04:53:22 +00:00
|
|
|
return
|
2021-09-24 04:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = append(ret, resp.Data...)
|
|
|
|
|
|
|
|
|
|
if resp.NextPage == nil {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
values.Set("offset", resp.NextPage.Offset)
|
2021-09-23 04:30:50 +00:00
|
|
|
}
|
2021-09-24 04:46:38 +00:00
|
|
|
|
2021-09-24 04:53:22 +00:00
|
|
|
return
|
2021-09-23 04:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|