-
Notifications
You must be signed in to change notification settings - Fork 4
/
project.go
82 lines (61 loc) · 1.71 KB
/
project.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
package wizard
import (
"fmt"
"bunnyshell.com/cli/pkg/api/project"
"bunnyshell.com/sdk"
)
func (w *Wizard) HasProject() bool {
return w.profile.Context.Project != ""
}
func (w *Wizard) GetProject() (*sdk.ProjectItem, error) {
if !w.HasProject() {
itemCol, err := w.SelectProject()
if err != nil {
return nil, err
}
w.profile.Context.Project = itemCol.GetId()
}
item, err := project.Get(project.NewItemOptions(w.profile.Context.Project))
if err != nil {
return nil, err
}
w.profile.Context.Organization = item.GetOrganization()
return item, nil
}
func (w *Wizard) SelectProject() (*sdk.ProjectCollection, error) {
return w.selectProject(1)
}
func (w *Wizard) selectProject(page int32) (*sdk.ProjectCollection, error) {
model, err := w.getProjects(page)
if err != nil {
return nil, err
}
embedded, ok := model.GetEmbeddedOk()
if !ok {
return nil, fmt.Errorf("%s %w", "projects", 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 Project", items, currentPage, totalPages)
if err != nil {
return nil, err
}
if newPage != nil {
return w.selectProject(*newPage)
}
if index != nil {
return &collectionItems[*index], nil
}
panic("Something went wrong...")
}
func (w *Wizard) getProjects(page int32) (*sdk.PaginatedProjectCollection, error) {
listOptions := project.NewListOptions()
listOptions.Page = page
listOptions.Profile = w.profile
listOptions.Organization = w.profile.Context.Organization
return project.List(listOptions)
}