-
Notifications
You must be signed in to change notification settings - Fork 4
/
organization.go
73 lines (55 loc) · 1.65 KB
/
organization.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
package wizard
import (
"fmt"
"bunnyshell.com/cli/pkg/api/organization"
"bunnyshell.com/sdk"
)
func (w *Wizard) HasOrganization() bool {
return w.profile.Context.Organization != ""
}
func (w *Wizard) GetOrganization() (*sdk.OrganizationItem, error) {
if !w.HasOrganization() {
itemCol, err := w.SelectOrganization()
if err != nil {
return nil, err
}
w.profile.Context.Organization = itemCol.GetId()
}
return organization.Get(organization.NewItemOptions(w.profile.Context.Organization))
}
func (w *Wizard) SelectOrganization() (*sdk.OrganizationCollection, error) {
return w.selectOrganization(1)
}
func (w *Wizard) selectOrganization(page int32) (*sdk.OrganizationCollection, error) {
model, err := w.getOrganizations(page)
if err != nil {
return nil, err
}
embedded, ok := model.GetEmbeddedOk()
if !ok {
return nil, fmt.Errorf("%s %w", "organizations", ErrEmptyListing)
}
collectionItems := embedded.GetItem()
items := []string{}
for _, item := range collectionItems {
items = append(items, fmt.Sprintf("%s (%s)", item.GetName(), item.GetId()))
}
currentPage, totalPages := getPaginationInfo(model)
index, newPage, err := chooseOrNavigate("Select Organization", items, currentPage, totalPages)
if err != nil {
return nil, err
}
if newPage != nil {
return w.selectOrganization(*newPage)
}
if index != nil {
return &collectionItems[*index], nil
}
panic("Something went wrong...")
}
func (w *Wizard) getOrganizations(page int32) (*sdk.PaginatedOrganizationCollection, error) {
listOptions := organization.NewListOptions()
listOptions.Page = page
listOptions.Profile = w.profile
return organization.List(listOptions)
}