Skip to content

Commit

Permalink
refactor: Update field names and styles in policy generation and view…
Browse files Browse the repository at this point in the history
… components.
  • Loading branch information
tolgaOzen committed Dec 26, 2024
1 parent 6414c6a commit ee009ac
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 43 deletions.
8 changes: 4 additions & 4 deletions internal/ai/policy_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ type IAMPrincipal struct {
}

type IAMActionResource struct {
IsStar bool
Resources []string
IsWildcard bool
Resources []string
}

var IAMPolicySchema = map[string]interface{}{
Expand Down Expand Up @@ -130,7 +130,7 @@ func (ar *IAMActionResource) UnmarshalJSON(data []byte) error {
var str string
if err := json.Unmarshal(data, &str); err == nil {
if str == "*" {
ar.IsStar = true
ar.IsWildcard = true
return nil
}
ar.Resources = []string{str}
Expand Down Expand Up @@ -185,7 +185,7 @@ func (p IAMPrincipal) MarshalJSON() ([]byte, error) {
}

func (ar IAMActionResource) MarshalJSON() ([]byte, error) {
if ar.IsStar {
if ar.IsWildcard {
return json.Marshal("*")
}
if len(ar.Resources) == 1 {
Expand Down
29 changes: 24 additions & 5 deletions pkg/aws/groups/create_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,33 @@ func (m CreatePolicy) View() string {
v := strings.TrimSuffix(m.form.View(), "\n\n")
form := m.lg.NewStyle().Margin(1, 0).Render(v)

var titles string
var titles []string
var title string

if m.controller.State.GetGroup() != nil {
titles = append(titles,
s.StateHeader.Render("Group Name: "+m.controller.State.GetGroup().Name),
s.StateHeader.Render("Group ARN: "+m.controller.State.GetGroup().Arn),
)
}

if m.controller.State.GetService() != nil && m.controller.State.GetResource() != nil {
titles = lipgloss.JoinVertical(lipgloss.Left,
s.ServiceNameHeader.Render("Service Name: "+m.controller.State.GetService().Name),
s.ResourceArnHeader.Render("Resource ARN: "+m.controller.State.GetResource().Arn),
titles = append(titles,
s.StateHeader.Render("Service Name: "+m.controller.State.GetService().Name),
s.StateHeader.Render("Resource ARN: "+m.controller.State.GetResource().Arn),
)
}

if len(titles) > 0 {
// Join the titles vertically
title = lipgloss.JoinVertical(lipgloss.Left, titles...)

// Apply margin-top to the entire title block
title = lipgloss.NewStyle().
MarginTop(1). // Set the margin top to 2 lines
Render(title)
}

// Status (right side)
var status string
{
Expand All @@ -159,7 +178,7 @@ func (m CreatePolicy) View() string {
errors := m.form.Errors()
header := lipgloss.JoinVertical(lipgloss.Top,
m.appBoundaryView("Custom Policy Generator"),
titles,
title,
)
if len(errors) > 0 {
header = m.appErrorBoundaryView(m.errorView())
Expand Down
2 changes: 1 addition & 1 deletion pkg/aws/groups/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (m Result) collectOverviewRows() [][]string {

func (m Result) formatPolicyRow(policy *models.Policy) []string {
if len(policy.Document) > 0 {
return []string{"Policy", policy.Name, string(policy.Document)}
return []string{"Policy", policy.Name, "new"}
}
return []string{"Policy", policy.Name, policy.Arn}
}
Expand Down
10 changes: 3 additions & 7 deletions pkg/aws/groups/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ type Styles struct {
HeaderText,
Status,
StatusHeader,
ServiceNameHeader,
ResourceArnHeader,
StateHeader,
Highlight,
ErrorHeaderText,
Help lipgloss.Style
Expand All @@ -44,12 +43,9 @@ func NewStyles(lg *lipgloss.Renderer) *Styles {
BorderForeground(purple).
PaddingLeft(1).
MarginTop(1)
s.ServiceNameHeader = lipgloss.NewStyle().
s.StateHeader = lipgloss.NewStyle().
Bold(true).
Foreground(green).MarginLeft(2).MarginTop(1)
s.ResourceArnHeader = lipgloss.NewStyle().
Bold(true).
Foreground(green).MarginLeft(2)
Foreground(green).MarginLeft(2).MarginTop(0).MarginLeft(2)
s.StatusHeader = lg.NewStyle().
Foreground(green).
Bold(true)
Expand Down
29 changes: 24 additions & 5 deletions pkg/aws/roles/create_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,33 @@ func (m CreatePolicy) View() string {
v := strings.TrimSuffix(m.form.View(), "\n\n")
form := m.lg.NewStyle().Margin(1, 0).Render(v)

var titles string
var titles []string
var title string

if m.controller.State.GetRole() != nil {
titles = append(titles,
s.StateHeader.Render("Role Name: "+m.controller.State.GetRole().Name),
s.StateHeader.Render("Role ARN: "+m.controller.State.GetRole().Arn),
)
}

if m.controller.State.GetService() != nil && m.controller.State.GetResource() != nil {
titles = lipgloss.JoinVertical(lipgloss.Left,
s.ServiceNameHeader.Render("Service Name: "+m.controller.State.GetService().Name),
s.ResourceArnHeader.Render("Resource ARN: "+m.controller.State.GetResource().Arn),
titles = append(titles,
s.StateHeader.Render("Service Name: "+m.controller.State.GetService().Name),
s.StateHeader.Render("Resource ARN: "+m.controller.State.GetResource().Arn),
)
}

if len(titles) > 0 {
// Join the titles vertically
title = lipgloss.JoinVertical(lipgloss.Left, titles...)

// Apply margin-top to the entire title block
title = lipgloss.NewStyle().
MarginTop(1). // Set the margin top to 2 lines
Render(title)
}

// Status (right side)
var status string
{
Expand All @@ -159,7 +178,7 @@ func (m CreatePolicy) View() string {
errors := m.form.Errors()
header := lipgloss.JoinVertical(lipgloss.Top,
m.appBoundaryView("Custom Policy Generator"),
titles,
title,
)
if len(errors) > 0 {
header = m.appErrorBoundaryView(m.errorView())
Expand Down
2 changes: 1 addition & 1 deletion pkg/aws/roles/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (m Result) collectOverviewRows() [][]string {

func (m Result) formatPolicyRow(policy *models.Policy) []string {
if len(policy.Document) > 0 {
return []string{"Policy", policy.Name, string(policy.Document)}
return []string{"Policy", policy.Name, "new"}
}
return []string{"Policy", policy.Name, policy.Arn}
}
Expand Down
10 changes: 3 additions & 7 deletions pkg/aws/roles/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ type Styles struct {
HeaderText,
Status,
StatusHeader,
ServiceNameHeader,
ResourceArnHeader,
StateHeader,
Highlight,
ErrorHeaderText,
Help lipgloss.Style
Expand All @@ -44,12 +43,9 @@ func NewStyles(lg *lipgloss.Renderer) *Styles {
BorderForeground(purple).
PaddingLeft(1).
MarginTop(1)
s.ServiceNameHeader = lipgloss.NewStyle().
s.StateHeader = lipgloss.NewStyle().
Bold(true).
Foreground(green).MarginLeft(2).MarginTop(1)
s.ResourceArnHeader = lipgloss.NewStyle().
Bold(true).
Foreground(green).MarginLeft(2)
Foreground(green).MarginLeft(2).MarginTop(0).MarginLeft(2)
s.StatusHeader = lg.NewStyle().
Foreground(green).
Bold(true)
Expand Down
29 changes: 24 additions & 5 deletions pkg/aws/users/create_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,33 @@ func (m CreatePolicy) View() string {
v := strings.TrimSuffix(m.form.View(), "\n\n")
form := m.lg.NewStyle().Margin(1, 0).Render(v)

var titles string
var titles []string
var title string

if m.controller.State.GetUser() != nil {
titles = append(titles,
s.StateHeader.Render("User Name: "+m.controller.State.GetUser().Name),
s.StateHeader.Render("User ARN: "+m.controller.State.GetUser().Arn),
)
}

if m.controller.State.GetService() != nil && m.controller.State.GetResource() != nil {
titles = lipgloss.JoinVertical(lipgloss.Left,
s.ServiceNameHeader.Render("Service Name: "+m.controller.State.GetService().Name),
s.ResourceArnHeader.Render("Resource ARN: "+m.controller.State.GetResource().Arn),
titles = append(titles,
s.StateHeader.Render("Service Name: "+m.controller.State.GetService().Name),
s.StateHeader.Render("Resource ARN: "+m.controller.State.GetResource().Arn),
)
}

if len(titles) > 0 {
// Join the titles vertically
title = lipgloss.JoinVertical(lipgloss.Left, titles...)

// Apply margin-top to the entire title block
title = lipgloss.NewStyle().
MarginTop(1). // Set the margin top to 2 lines
Render(title)
}

// Status (right side)
var status string
{
Expand All @@ -158,7 +177,7 @@ func (m CreatePolicy) View() string {
errors := m.form.Errors()
header := lipgloss.JoinVertical(lipgloss.Top,
m.appBoundaryView("Custom Policy Generator"),
titles,
title,
)
if len(errors) > 0 {
header = m.appErrorBoundaryView(m.errorView())
Expand Down
2 changes: 1 addition & 1 deletion pkg/aws/users/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (m Result) collectOverviewRows() [][]string {

func (m Result) formatPolicyRow(policy *models.Policy) []string {
if len(policy.Document) > 0 {
return []string{"Policy", policy.Name, string(policy.Document)}
return []string{"Policy", policy.Name, "new"}
}
return []string{"Policy", policy.Name, policy.Arn}
}
Expand Down
10 changes: 3 additions & 7 deletions pkg/aws/users/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ type Styles struct {
HeaderText,
Status,
StatusHeader,
ServiceNameHeader,
ResourceArnHeader,
StateHeader,
Highlight,
ErrorHeaderText,
Help lipgloss.Style
Expand All @@ -44,12 +43,9 @@ func NewStyles(lg *lipgloss.Renderer) *Styles {
BorderForeground(purple).
PaddingLeft(1).
MarginTop(1)
s.ServiceNameHeader = lipgloss.NewStyle().
s.StateHeader = lipgloss.NewStyle().
Bold(true).
Foreground(green).MarginLeft(2).MarginTop(1)
s.ResourceArnHeader = lipgloss.NewStyle().
Bold(true).
Foreground(green).MarginLeft(2)
Foreground(green).MarginLeft(2).MarginTop(0).MarginLeft(2)
s.StatusHeader = lg.NewStyle().
Foreground(green).
Bold(true)
Expand Down

0 comments on commit ee009ac

Please sign in to comment.