-
Notifications
You must be signed in to change notification settings - Fork 4
/
component.go
84 lines (63 loc) · 1.88 KB
/
component.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
83
84
package wizard
import (
"fmt"
"bunnyshell.com/cli/pkg/api/component"
"bunnyshell.com/sdk"
)
func (w *Wizard) HasComponent() bool {
return w.profile.Context.ServiceComponent != ""
}
func (w *Wizard) GetComponent() (*sdk.ComponentItem, error) {
if !w.HasComponent() {
colItem, err := w.SelectComponent()
if err != nil {
return nil, err
}
w.profile.Context.ServiceComponent = colItem.GetId()
}
item, err := component.Get(component.NewItemOptions(w.profile.Context.ServiceComponent))
if err != nil {
return nil, err
}
w.profile.Context.Environment = item.GetEnvironment()
return item, nil
}
func (w *Wizard) SelectComponent() (*sdk.ComponentCollection, error) {
return w.selectComponent(1)
}
func (w *Wizard) selectComponent(page int32) (*sdk.ComponentCollection, error) {
model, err := w.getComponents(page)
if err != nil {
return nil, err
}
embedded, ok := model.GetEmbeddedOk()
if !ok {
return nil, fmt.Errorf("%s %w", "components", 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 Component", items, currentPage, totalPages)
if err != nil {
return nil, err
}
if newPage != nil {
return w.selectComponent(*newPage)
}
if index != nil {
return &collectionItems[*index], nil
}
panic("Something went wrong...")
}
func (w *Wizard) getComponents(page int32) (*sdk.PaginatedComponentCollection, error) {
listOptions := component.NewListOptions()
listOptions.Page = page
listOptions.Profile = w.profile
listOptions.Organization = w.profile.Context.Organization
listOptions.Project = w.profile.Context.Project
listOptions.Environment = w.profile.Context.Environment
return component.List(listOptions)
}