Skip to content

Commit

Permalink
Cleanup RBAC commands
Browse files Browse the repository at this point in the history
Also add set-role command for user
  • Loading branch information
nitisht committed Nov 14, 2023
1 parent 4b87b3e commit 96cc1ae
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 79 deletions.
25 changes: 13 additions & 12 deletions cmd/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"net/url"
"os"

"pb/pkg/config"
"pb/pkg/model/credential"
"pb/pkg/model/defaultprofile"
Expand All @@ -39,19 +40,19 @@ func (item *ProfileListItem) Render(highlight bool) string {
if highlight {
render := fmt.Sprintf(
"%s\n%s\n%s",
selectedStyle.Render(item.title),
selectedStyleAlt.Render(fmt.Sprintf("url: %s", item.url)),
selectedStyleAlt.Render(fmt.Sprintf("user: %s", item.user)),
SelectedStyle.Render(item.title),
SelectedStyleAlt.Render(fmt.Sprintf("url: %s", item.url)),
SelectedStyleAlt.Render(fmt.Sprintf("user: %s", item.user)),
)
return selectedItemOuter.Render(render)
return SelectedItemOuter.Render(render)
}
render := fmt.Sprintf(
"%s\n%s\n%s",
standardStyle.Render(item.title),
standardStyleAlt.Render(fmt.Sprintf("url: %s", item.url)),
standardStyleAlt.Render(fmt.Sprintf("user: %s", item.user)),
StandardStyle.Render(item.title),
StandardStyleAlt.Render(fmt.Sprintf("url: %s", item.url)),
StandardStyleAlt.Render(fmt.Sprintf("user: %s", item.user)),
)
return itemOuter.Render(render)
return ItemOuter.Render(render)
}

var AddProfileCmd = &cobra.Command{
Expand Down Expand Up @@ -140,9 +141,9 @@ var RemoveProfileCmd = &cobra.Command{
fileConfig.DefaultProfile = ""
}
config.WriteConfigToFile(fileConfig)
fmt.Printf("Deleted profile %s\n", styleBold.Render(name))
fmt.Printf("Deleted profile %s\n", StyleBold.Render(name))
} else {
fmt.Printf("No profile found with the name: %s", styleBold.Render(name))
fmt.Printf("No profile found with the name: %s", StyleBold.Render(name))
}

return nil
Expand Down Expand Up @@ -185,12 +186,12 @@ var DefaultProfileCmd = &cobra.Command{
fileConfig.DefaultProfile = name
} else {
name = lipgloss.NewStyle().Bold(true).Render(name)
err := fmt.Sprintf("profile %s does not exist", styleBold.Render(name))
err := fmt.Sprintf("profile %s does not exist", StyleBold.Render(name))
return errors.New(err)
}

config.WriteConfigToFile(fileConfig)
fmt.Printf("%s is now set as default profile\n", styleBold.Render(name))
fmt.Printf("%s is now set as default profile\n", StyleBold.Render(name))
return nil
},
}
Expand Down
31 changes: 21 additions & 10 deletions cmd/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import (
"fmt"
"io"
"os"
"pb/pkg/model/role"
"strings"
"sync"

"pb/pkg/model/role"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/spf13/cobra"
Expand All @@ -42,18 +43,18 @@ type RoleData struct {

func (user *RoleData) Render() string {
var s strings.Builder
s.WriteString(standardStyle.Render("Privilege: "))
s.WriteString(standardStyleAlt.Render(user.Privilege))
s.WriteString(StandardStyle.Render("Privilege: "))
s.WriteString(StandardStyleAlt.Render(user.Privilege))
s.WriteString("\n")
if user.Resource != nil {
if user.Resource.Stream != "" {
s.WriteString(standardStyle.Render("Stream: "))
s.WriteString(standardStyleAlt.Render(user.Resource.Stream))
s.WriteString(StandardStyle.Render("Stream: "))
s.WriteString(StandardStyleAlt.Render(user.Resource.Stream))
s.WriteString("\n")
}
if user.Resource.Tag != "" {
s.WriteString(standardStyle.Render("Tag: "))
s.WriteString(standardStyleAlt.Render(user.Resource.Tag))
s.WriteString(StandardStyle.Render("Tag: "))
s.WriteString(StandardStyleAlt.Render(user.Resource.Tag))
s.WriteString("\n")
}
}
Expand All @@ -69,6 +70,17 @@ var AddRoleCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]

// check if the role already exists
var roles []string
client := DefaultClient()
if err := fetchRoles(&client, &roles); err != nil {
return err
}
if strings.Contains(strings.Join(roles, " "), name) {
fmt.Println("role already exists, please use a different name")
return nil
}

_m, err := tea.NewProgram(role.New()).Run()
if err != nil {
fmt.Printf("there's been an error: %v", err)
Expand Down Expand Up @@ -113,7 +125,6 @@ var AddRoleCmd = &cobra.Command{
putBody = bytes.NewBuffer(roleDataJSON)
}

client := DefaultClient()
req, err := client.NewRequest("PUT", "role/"+name, putBody)
if err != nil {
return err
Expand Down Expand Up @@ -161,7 +172,7 @@ var RemoveRoleCmd = &cobra.Command{
}

if resp.StatusCode == 200 {
fmt.Printf("Removed role %s\n", styleBold.Render(name))
fmt.Printf("Removed role %s\n", StyleBold.Render(name))
} else {
bytes, err := io.ReadAll(resp.Body)
if err != nil {
Expand Down Expand Up @@ -211,7 +222,7 @@ var ListRoleCmd = &cobra.Command{
for idx, roleName := range roles {
fetchRes := roleResponses[idx]
fmt.Print("• ")
fmt.Println(standardStyleBold.Bold(true).Render(roleName))
fmt.Println(StandardStyleBold.Bold(true).Render(roleName))
if fetchRes.err == nil {
for _, role := range fetchRes.data {
fmt.Println(lipgloss.NewStyle().PaddingLeft(3).Render(role.Render()))
Expand Down
24 changes: 12 additions & 12 deletions cmd/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ type StreamListItem struct {
}

func (item *StreamListItem) Render() string {
render := standardStyle.Render(item.name)
return itemOuter.Render(render)
render := StandardStyle.Render(item.name)
return ItemOuter.Render(render)
}

// StreamRetentionData is the data structure for stream retention
Expand Down Expand Up @@ -111,7 +111,7 @@ var AddStreamCmd = &cobra.Command{
}

if resp.StatusCode == 200 {
fmt.Printf("Created stream %s\n", styleBold.Render(name))
fmt.Printf("Created stream %s\n", StyleBold.Render(name))
} else {
bytes, err := io.ReadAll(resp.Body)
if err != nil {
Expand Down Expand Up @@ -152,7 +152,7 @@ var StatStreamCmd = &cobra.Command{

isRetentionSet := len(retention) > 0

fmt.Println(styleBold.Render("\nInfo:"))
fmt.Println(StyleBold.Render("\nInfo:"))
fmt.Printf(" Event Count: %d\n", ingestionCount)
fmt.Printf(" Ingestion Size: %s\n", humanize.Bytes(uint64(ingestionSize)))
fmt.Printf(" Storage Size: %s\n", humanize.Bytes(uint64(storageSize)))
Expand All @@ -162,14 +162,14 @@ var StatStreamCmd = &cobra.Command{
fmt.Println()

if isRetentionSet {
fmt.Println(styleBold.Render("Retention:"))
fmt.Println(StyleBold.Render("Retention:"))
for _, item := range retention {
fmt.Printf(" Action: %s\n", styleBold.Render(item.Action))
fmt.Printf(" Duration: %s\n", styleBold.Render(item.Duration))
fmt.Printf(" Action: %s\n", StyleBold.Render(item.Action))
fmt.Printf(" Duration: %s\n", StyleBold.Render(item.Duration))
fmt.Println()
}
} else {
fmt.Println(styleBold.Render("No retention period set on stream\n"))
fmt.Println(StyleBold.Render("No retention period set on stream\n"))
}

alertsData, err := fetchAlerts(&client, name)
Expand All @@ -181,9 +181,9 @@ var StatStreamCmd = &cobra.Command{
isAlertsSet := len(alerts) > 0

if isAlertsSet {
fmt.Println(styleBold.Render("Alerts:"))
fmt.Println(StyleBold.Render("Alerts:"))
for _, alert := range alerts {
fmt.Printf(" Alert: %s\n", styleBold.Render(alert.Name))
fmt.Printf(" Alert: %s\n", StyleBold.Render(alert.Name))
ruleFmt := fmt.Sprintf(
"%s %s %s repeated %d times",
alert.Rule.Config.Column,
Expand All @@ -199,7 +199,7 @@ var StatStreamCmd = &cobra.Command{
fmt.Print("\n\n")
}
} else {
fmt.Println(styleBold.Render("No alerts set on stream\n"))
fmt.Println(StyleBold.Render("No alerts set on stream\n"))
}

return nil
Expand All @@ -226,7 +226,7 @@ var RemoveStreamCmd = &cobra.Command{
}

if resp.StatusCode == 200 {
fmt.Printf("Removed stream %s\n", styleBold.Render(name))
fmt.Printf("Removed stream %s\n", StyleBold.Render(name))
} else {
bytes, err := io.ReadAll(resp.Body)
if err != nil {
Expand Down
31 changes: 14 additions & 17 deletions cmd/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,18 @@ import (

// styling for cli outputs
var (
FocusPrimary = lipgloss.AdaptiveColor{Light: "16", Dark: "226"}
FocusSecondry = lipgloss.AdaptiveColor{Light: "18", Dark: "220"}

standardPrimary = lipgloss.AdaptiveColor{Light: "235", Dark: "255"}
standardSecondry = lipgloss.AdaptiveColor{Light: "238", Dark: "254"}

standardStyle = lipgloss.NewStyle().Foreground(standardPrimary)
standardStyleBold = lipgloss.NewStyle().Foreground(standardPrimary).Bold(true)
standardStyleAlt = lipgloss.NewStyle().Foreground(standardSecondry)

selectedStyle = lipgloss.NewStyle().Foreground(FocusPrimary).Bold(true)
selectedStyleAlt = lipgloss.NewStyle().Foreground(FocusSecondry)

selectedItemOuter = lipgloss.NewStyle().BorderStyle(lipgloss.NormalBorder()).BorderLeft(true).PaddingLeft(1).BorderForeground(FocusPrimary)
itemOuter = lipgloss.NewStyle().PaddingLeft(1)

styleBold = lipgloss.NewStyle().Bold(true)
FocusPrimary = lipgloss.AdaptiveColor{Light: "16", Dark: "226"}
FocusSecondary = lipgloss.AdaptiveColor{Light: "18", Dark: "220"}

StandardPrimary = lipgloss.AdaptiveColor{Light: "235", Dark: "255"}
StandardSecondary = lipgloss.AdaptiveColor{Light: "238", Dark: "254"}
StandardStyle = lipgloss.NewStyle().Foreground(StandardPrimary)
StandardStyleBold = lipgloss.NewStyle().Foreground(StandardPrimary).Bold(true)
StandardStyleAlt = lipgloss.NewStyle().Foreground(StandardSecondary)
SelectedStyle = lipgloss.NewStyle().Foreground(FocusPrimary).Bold(true)
SelectedStyleAlt = lipgloss.NewStyle().Foreground(FocusSecondary)
SelectedItemOuter = lipgloss.NewStyle().BorderStyle(lipgloss.NormalBorder()).BorderLeft(true).PaddingLeft(1).BorderForeground(FocusPrimary)
ItemOuter = lipgloss.NewStyle().PaddingLeft(1)

StyleBold = lipgloss.NewStyle().Bold(true)
)
Loading

0 comments on commit 96cc1ae

Please sign in to comment.