Skip to content

Commit

Permalink
add a global styles and add user sysAdmin elevate cmd
Browse files Browse the repository at this point in the history
Signed-off-by: amands98 <amandeepsm.in@gmail.com>
  • Loading branch information
amands98 committed May 13, 2024
1 parent 4f9bab5 commit eebd1b2
Show file tree
Hide file tree
Showing 11 changed files with 289 additions and 46 deletions.
2 changes: 2 additions & 0 deletions cmd/harbor/root/user/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ func User() *cobra.Command {
cmd.AddCommand(
UserListCmd(),
UserCreateCmd(),
UserDeleteCmd(),
ElevateUserCmd(),
)

return cmd
Expand Down
51 changes: 51 additions & 0 deletions cmd/harbor/root/user/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package user

import (
"context"
"strconv"

"github.com/goharbor/go-client/pkg/sdk/v2.0/client/user"
"github.com/goharbor/harbor-cli/pkg/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func UserDeleteCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "delete user",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var err error
if len(args) > 0 {
userId, _ := strconv.ParseInt(args[0], 10, 64)
err = runDeleteUser(userId)

} else {
userId := utils.GetUserIdFromUser()
err = runDeleteUser(userId)
}

if err != nil {
log.Errorf("failed to delete user: %v", err)
}

},
}

return cmd

}

func runDeleteUser(userId int64) error {
credentialName := viper.GetString("current-credential-name")
client := utils.GetClientByCredentialName(credentialName)
ctx := context.Background()
_, err := client.User.DeleteUser(ctx, &user.DeleteUserParams{UserID: userId})
if err != nil {
return err
}
log.Info("user deleted successfully")
return nil
}
58 changes: 58 additions & 0 deletions cmd/harbor/root/user/elevate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package user

import (
"context"
"strconv"

"github.com/goharbor/go-client/pkg/sdk/v2.0/client/user"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func ElevateUserCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "elevate",
Short: "elevate user",
Long: "elevate user to admin role",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var err error
var userId int64
if len(args) > 0 {
userId, _ = strconv.ParseInt(args[0], 10, 64)

} else {
userId = utils.GetUserIdFromUser()
}

// Todo : Ask for the confirmation before elevating the user to admin role

err = runElevateUser(userId)

if err != nil {
log.Errorf("failed to elevate user: %v", err)
}

},
}

return cmd
}

func runElevateUser(userId int64) error {
credentialName := viper.GetString("current-credential-name")
client := utils.GetClientByCredentialName(credentialName)
ctx := context.Background()
UserSysAdminFlag := &models.UserSysAdminFlag{
SysadminFlag: true,
}
_, err := client.User.SetUserSysAdmin(ctx, &user.SetUserSysAdminParams{UserID: userId, SysadminFlag: UserSysAdminFlag})
if err != nil {
return err
}
log.Info("user elevated role to admin successfully")
return nil
}
20 changes: 20 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
v2client "github.com/goharbor/go-client/pkg/sdk/v2.0/client"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/project"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/registry"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/user"
pview "github.com/goharbor/harbor-cli/pkg/views/project/select"
rview "github.com/goharbor/harbor-cli/pkg/views/registry/select"
uview "github.com/goharbor/harbor-cli/pkg/views/user/select"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -88,3 +90,21 @@ func GetProjectNameFromUser() string {

return <-projectName
}

func GetUserIdFromUser() int64 {
userId := make(chan int64)

go func() {
credentialName := viper.GetString("current-credential-name")
client := GetClientByCredentialName(credentialName)
ctx := context.Background()
response, err := client.User.ListUsers(ctx, &user.ListUsersParams{})
if err != nil {
log.Fatal(err)
}
uview.UserList(response.Payload, userId)
}()

return <-userId

}
6 changes: 2 additions & 4 deletions pkg/views/project/list/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import (
"github.com/charmbracelet/lipgloss"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views"
)

var baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).Padding(0, 1)

type model struct {
table table.Model
}
Expand All @@ -40,7 +38,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m model) View() string {
return baseStyle.Render(m.table.View()) + "\n"
return views.BaseStyle.Render(m.table.View()) + "\n"
}

func ListProjects(projects []*models.Project) {
Expand Down
20 changes: 6 additions & 14 deletions pkg/views/project/select/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,12 @@ import (

"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/views"
)

const listHeight = 14

var (
titleStyle = lipgloss.NewStyle().MarginLeft(2)
itemStyle = lipgloss.NewStyle().PaddingLeft(4)
selectedItemStyle = lipgloss.NewStyle().PaddingLeft(2).Foreground(lipgloss.Color("170"))
paginationStyle = list.DefaultStyles().PaginationStyle.PaddingLeft(4)
helpStyle = list.DefaultStyles().HelpStyle.PaddingLeft(4).PaddingBottom(1)
)

type item string

func (i item) FilterValue() string { return "" }
Expand All @@ -39,10 +31,10 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list

str := fmt.Sprintf("%d. %s", index+1, i)

fn := itemStyle.Render
fn := views.ItemStyle.Render
if index == m.Index() {
fn = func(s ...string) string {
return selectedItemStyle.Render("> " + strings.Join(s, " "))
return views.SelectedItemStyle.Render("> " + strings.Join(s, " "))
}
}

Expand Down Expand Up @@ -99,9 +91,9 @@ func ProjectList(project []*models.Project, choice chan<- string) {
l.Title = "Select a project"
l.SetShowStatusBar(false)
l.SetFilteringEnabled(false)
l.Styles.Title = titleStyle
l.Styles.PaginationStyle = paginationStyle
l.Styles.HelpStyle = helpStyle
l.Styles.Title = views.TitleStyle
l.Styles.PaginationStyle = views.PaginationStyle
l.Styles.HelpStyle = views.HelpStyle

m := model{list: l}

Expand Down
6 changes: 2 additions & 4 deletions pkg/views/registry/list/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import (
"github.com/charmbracelet/lipgloss"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views"
)

var baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).Padding(0, 1)

type model struct {
table table.Model
}
Expand All @@ -40,7 +38,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m model) View() string {
return baseStyle.Render(m.table.View()) + "\n"
return views.BaseStyle.Render(m.table.View()) + "\n"
}

func ListRegistry(registry []*models.Registry) {
Expand Down
22 changes: 7 additions & 15 deletions pkg/views/registry/select/view.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package project
package registry

import (
"fmt"
Expand All @@ -8,20 +8,12 @@ import (

"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/views"
)

const listHeight = 14

var (
titleStyle = lipgloss.NewStyle().MarginLeft(2)
itemStyle = lipgloss.NewStyle().PaddingLeft(4)
selectedItemStyle = lipgloss.NewStyle().PaddingLeft(2).Foreground(lipgloss.Color("170"))
paginationStyle = list.DefaultStyles().PaginationStyle.PaddingLeft(4)
helpStyle = list.DefaultStyles().HelpStyle.PaddingLeft(4).PaddingBottom(1)
)

type item string

func (i item) FilterValue() string { return "" }
Expand All @@ -39,10 +31,10 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list

str := fmt.Sprintf("%d. %s", index+1, i)

fn := itemStyle.Render
fn := views.ItemStyle.Render
if index == m.Index() {
fn = func(s ...string) string {
return selectedItemStyle.Render("> " + strings.Join(s, " "))
return views.SelectedItemStyle.Render("> " + strings.Join(s, " "))
}
}

Expand Down Expand Up @@ -103,9 +95,9 @@ func RegistryList(registry []*models.Registry, choice chan<- int64) {
l.Title = "Select a Registry"
l.SetShowStatusBar(false)
l.SetFilteringEnabled(false)
l.Styles.Title = titleStyle
l.Styles.PaginationStyle = paginationStyle
l.Styles.HelpStyle = helpStyle
l.Styles.Title = views.TitleStyle
l.Styles.PaginationStyle = views.PaginationStyle
l.Styles.HelpStyle = views.HelpStyle

m := model{list: l}

Expand Down
17 changes: 17 additions & 0 deletions pkg/views/styles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package views

import (
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/lipgloss"
)

var (
TitleStyle = lipgloss.NewStyle().MarginLeft(2)
ItemStyle = lipgloss.NewStyle().PaddingLeft(4)
SelectedItemStyle = lipgloss.NewStyle().PaddingLeft(2).Foreground(lipgloss.Color("170"))
PaginationStyle = list.DefaultStyles().PaginationStyle.PaddingLeft(4)
HelpStyle = list.DefaultStyles().HelpStyle.PaddingLeft(4).PaddingBottom(1)
)

var BaseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).Padding(0, 1)
18 changes: 9 additions & 9 deletions pkg/views/user/list/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views"
)

var baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).Padding(0, 1)

type model struct {
table table.Model
}

var columns = []table.Column{
{Title: "Name", Width: 12},
{Title: "Name", Width: 16},
{Title: "Administrator", Width: 16},
{Title: "Email", Width: 12},
{Title: "Registration Time", Width: 30},
{Title: "Email", Width: 20},
{Title: "Registration Time", Width: 24},
}

func (m model) Init() tea.Cmd {
Expand All @@ -36,21 +35,22 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m model) View() string {
return baseStyle.Render(m.table.View()) + "\n"
return views.BaseStyle.Render(m.table.View()) + "\n"
}

func ListUsers(users []*models.UserResp) {
var rows []table.Row
for _, user := range users {
isAdmin := "No"
if user.AdminRoleInAuth {
if user.SysadminFlag {
isAdmin = "Yes"
}
createdTime, _ := utils.FormatCreatedTime(user.CreationTime.String())
rows = append(rows, table.Row{
user.Username,
isAdmin,
user.Email,
fmt.Sprintf("%s", user.CreationTime),
createdTime,
})
}

Expand Down
Loading

0 comments on commit eebd1b2

Please sign in to comment.