-
Notifications
You must be signed in to change notification settings - Fork 3
/
user.go
87 lines (77 loc) · 2.01 KB
/
user.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
85
86
87
package iam
import (
"log"
tea "github.com/charmbracelet/bubbletea"
"github.com/danielcmessias/sawsy/data"
"github.com/danielcmessias/sawsy/ui/components/page"
"github.com/danielcmessias/sawsy/ui/components/table"
"github.com/danielcmessias/sawsy/ui/context"
)
type UserPageModel struct {
page.Model
}
type UserPageContext struct {
UserName string
}
func NewUserPage(ctx *context.ProgramContext) *UserPageModel {
return &UserPageModel{
Model: page.New(ctx, userPageSpec),
}
}
func (m *UserPageModel) FetchData(client *data.Client) tea.Cmd {
return tea.Batch(
m.fetchUserPolicies(client, nil),
m.fetchUserTags(client, nil),
)
}
func (m *UserPageModel) fetchUserPolicies(client *data.Client, nextToken *string) tea.Cmd {
return func() tea.Msg {
rows, nextToken, _ := client.IAM.GetUserPolicies(m.Context.(UserPageContext).UserName, nextToken)
msg := page.NewRowsMsg{
Page: m.Spec.Name,
PaneId: m.GetPaneId("Policies"),
Rows: rows,
}
if nextToken != nil {
msg.NextCmd = m.fetchUserPolicies(client, nextToken)
}
return msg
}
}
func (m *UserPageModel) fetchUserTags(client *data.Client, nextToken *string) tea.Cmd {
return func() tea.Msg {
rows, nextToken, _ := client.IAM.GetUserTags(m.Context.(UserPageContext).UserName, nextToken)
msg := page.NewRowsMsg{
Page: m.Spec.Name,
PaneId: m.GetPaneId("Tags"),
Rows: rows,
}
if nextToken != nil {
msg.NextCmd = m.fetchUserTags(client, nextToken)
}
return msg
}
}
func (m *UserPageModel) Inspect(client *data.Client) tea.Cmd {
table, ok := m.CurrentPane().(*table.Model)
if !ok {
log.Fatal("This pane is not a table")
}
switch m.GetCurrentPaneId() {
case m.GetPaneId("Policies"):
row := table.GetCurrentRowMarshalled()
return func() tea.Msg {
return page.ChangePageMsg{
NewPage: "iam/policy",
FetchData: true,
PageContext: PolicyPageContext{
UserName: m.Context.(UserPageContext).UserName,
PolicyName: row["Name"],
PolicyArn: row["ARN"],
},
}
}
default:
return nil
}
}