Skip to content

Commit

Permalink
feat: add command update users (#968)
Browse files Browse the repository at this point in the history
Co-authored-by: David Ragot <[email protected]>
  • Loading branch information
Dav-14 and David Ragot authored Dec 11, 2023
1 parent fcbb796 commit ff4c77c
Show file tree
Hide file tree
Showing 26 changed files with 1,863 additions and 1,725 deletions.
2 changes: 2 additions & 0 deletions components/fctl/cmd/cloud/organizations/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package organizations

import (
"github.com/formancehq/fctl/cmd/cloud/organizations/invitations"
"github.com/formancehq/fctl/cmd/cloud/organizations/users"
fctl "github.com/formancehq/fctl/pkg"
"github.com/spf13/cobra"
)
Expand All @@ -14,6 +15,7 @@ func NewCommand() *cobra.Command {
NewListCommand(),
NewCreateCommand(),
NewDeleteCommand(),
users.NewCommand(),
invitations.NewCommand(),
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,40 @@ import (
"github.com/spf13/cobra"
)

type UnlinkStore struct {
type DeleteStore struct {
OrganizationID string `json:"organizationId"`
UserID string `json:"userId"`
}
type UnlinkController struct {
store *UnlinkStore
type DeleteController struct {
store *DeleteStore
}

var _ fctl.Controller[*UnlinkStore] = (*UnlinkController)(nil)
var _ fctl.Controller[*DeleteStore] = (*DeleteController)(nil)

func NewDefaultUnlinkStore() *UnlinkStore {
return &UnlinkStore{}
func NewDefaultDeleteStore() *DeleteStore {
return &DeleteStore{}
}

func NewUnlinkController() *UnlinkController {
return &UnlinkController{
store: NewDefaultUnlinkStore(),
func NewDeleteController() *DeleteController {
return &DeleteController{
store: NewDefaultDeleteStore(),
}
}

func NewUnlinkCommand() *cobra.Command {
return fctl.NewCommand("unlink <user-id>",
func NewDeleteCommand() *cobra.Command {
return fctl.NewCommand("delete <user-id>",
fctl.WithAliases("u", "un"),
fctl.WithShortDescription("unlink user from organization"),
fctl.WithShortDescription("Delete user from organization"),
fctl.WithArgs(cobra.ExactArgs(1)),
fctl.WithController[*UnlinkStore](NewUnlinkController()),
fctl.WithController[*DeleteStore](NewDeleteController()),
)
}

func (c *UnlinkController) GetStore() *UnlinkStore {
func (c *DeleteController) GetStore() *DeleteStore {
return c.store
}

func (c *UnlinkController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
func (c *DeleteController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {

cfg, err := fctl.GetConfig(cmd)
if err != nil {
Expand All @@ -56,7 +56,7 @@ func (c *UnlinkController) Run(cmd *cobra.Command, args []string) (fctl.Renderab
return nil, err
}

_, err = apiClient.DefaultApi.UnlinkUserFromOrganization(cmd.Context(), organizationID, args[0]).Execute()
_, err = apiClient.DefaultApi.DeleteUserFromOrganization(cmd.Context(), organizationID, args[0]).Execute()
if err != nil {
return nil, err
}
Expand All @@ -67,8 +67,8 @@ func (c *UnlinkController) Run(cmd *cobra.Command, args []string) (fctl.Renderab
return c, nil
}

func (c *UnlinkController) Render(cmd *cobra.Command, args []string) error {
pterm.Success.WithWriter(cmd.OutOrStdout()).Printfln("User '%s' unlinked from organization '%s'", c.store.UserID, c.store.OrganizationID)
func (c *DeleteController) Render(cmd *cobra.Command, args []string) error {
pterm.Success.WithWriter(cmd.OutOrStdout()).Printfln("User '%s' Deleted from organization '%s'", c.store.UserID, c.store.OrganizationID)

return nil

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
)

type User struct {
ID string `json:"id"`
Email string `json:"email"`
ID string `json:"id"`
Email string `json:"email"`
Roles []string `json:"roles"`
}

type ListStore struct {
Expand Down Expand Up @@ -59,7 +60,7 @@ func (c *ListController) Run(cmd *cobra.Command, args []string) (fctl.Renderable
return nil, err
}

usersResponse, _, err := apiClient.DefaultApi.ListUsers(cmd.Context(), organizationID).Execute()
usersResponse, _, err := apiClient.DefaultApi.ListUsersOfOrganization(cmd.Context(), organizationID).Execute()
if err != nil {
return nil, err
}
Expand All @@ -68,6 +69,7 @@ func (c *ListController) Run(cmd *cobra.Command, args []string) (fctl.Renderable
return User{
i.Id,
i.Email,
i.Roles,
}
})

Expand All @@ -80,10 +82,23 @@ func (c *ListController) Render(cmd *cobra.Command, args []string) error {
return []string{
i.ID,
i.Email,
func() string {
roles := ""

for _, role := range i.Roles {
if role == "ADMIN" {
roles += pterm.LightRed(role) + " | "
} else {
roles += pterm.LightGreen(role) + " | "
}
}

return roles
}(),
}
})

tableData := fctl.Prepend(usersRow, []string{"ID", "Email"})
tableData := fctl.Prepend(usersRow, []string{"ID", "Email", "Roles"})
return pterm.DefaultTable.
WithHasHeader().
WithWriter(cmd.OutOrStdout()).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ func NewCommand() *cobra.Command {
fctl.WithChildCommands(
NewListCommand(),
NewShowCommand(),
NewUnlinkCommand(),
NewDeleteCommand(),
NewUpdateCommand(),
),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
)

type ShowStore struct {
Id string `json:"id"`
Email string `json:"email"`
Id string `json:"id"`
Email string `json:"email"`
Roles []string `json:"roles"`
}
type ShowController struct {
store *ShowStore
Expand Down Expand Up @@ -55,13 +56,14 @@ func (c *ShowController) Run(cmd *cobra.Command, args []string) (fctl.Renderable
return nil, err
}

userResponse, _, err := apiClient.DefaultApi.ReadUser(cmd.Context(), organizationID, args[0]).Execute()
userResponse, _, err := apiClient.DefaultApi.ReadUserOfOrganization(cmd.Context(), organizationID, args[0]).Execute()
if err != nil {
return nil, err
}

c.store.Id = userResponse.Data.Id
c.store.Email = userResponse.Data.Email
c.store.Roles = userResponse.Data.Roles

return c, nil
}
Expand All @@ -70,6 +72,21 @@ func (c *ShowController) Render(cmd *cobra.Command, args []string) error {
tableData := pterm.TableData{}
tableData = append(tableData, []string{pterm.LightCyan("ID"), c.store.Id})
tableData = append(tableData, []string{pterm.LightCyan("Email"), c.store.Email})
tableData = append(tableData, []string{
pterm.LightCyan("Roles"),
func() string {
roles := ""
for _, role := range c.store.Roles {
if role == "ADMIN" {
roles += pterm.LightRed(role) + " | "
} else {
roles += pterm.LightGreen(role) + " | "
}
}

return roles
}(),
})

return pterm.DefaultTable.
WithWriter(cmd.OutOrStdout()).
Expand Down
82 changes: 82 additions & 0 deletions components/fctl/cmd/cloud/organizations/users/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package users

import (
"fmt"

fctl "github.com/formancehq/fctl/pkg"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

type UpdateStore struct {
Id string `json:"id"`
Email string `json:"email"`
IsAdmin bool `json:"is_admin"`
}
type UpdateController struct {
store *UpdateStore
}

var _ fctl.Controller[*UpdateStore] = (*UpdateController)(nil)

func NewDefaultUpdateStore() *UpdateStore {
return &UpdateStore{}
}

func NewUpdateController() *UpdateController {
return &UpdateController{
store: NewDefaultUpdateStore(),
}
}

func NewUpdateCommand() *cobra.Command {
return fctl.NewCommand("update <user-id> <roles...>",
fctl.WithAliases("s"),
fctl.WithShortDescription("Update user roles by by id within an organization"),
fctl.WithArgs(cobra.MinimumNArgs(1)),
fctl.WithController[*UpdateStore](NewUpdateController()),
)
}

func (c *UpdateController) GetStore() *UpdateStore {
return c.store
}

func (c *UpdateController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
cfg, err := fctl.GetConfig(cmd)
if err != nil {
return nil, err
}

apiClient, err := fctl.NewMembershipClient(cmd, cfg)
if err != nil {
return nil, err
}

organizationID, err := fctl.ResolveOrganizationID(cmd, cfg)
if err != nil {
return nil, err
}

roles := []string{}
roles = append(roles, args[1:]...)

response, err := apiClient.DefaultApi.UpdateOrganizationUser(
cmd.Context(),
organizationID,
args[0]).RequestBody(roles).Execute()
if err != nil {
return nil, err
}

if response.StatusCode > 300 {
return nil, fmt.Errorf("error updating user: %s", response.Status)
}

return c, nil
}

func (c *UpdateController) Render(cmd *cobra.Command, args []string) error {
pterm.Success.WithWriter(cmd.OutOrStdout()).Printfln("User updated.")
return nil
}
2 changes: 0 additions & 2 deletions components/fctl/cmd/cloud/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/formancehq/fctl/cmd/cloud/me"
"github.com/formancehq/fctl/cmd/cloud/organizations"
"github.com/formancehq/fctl/cmd/cloud/regions"
"github.com/formancehq/fctl/cmd/cloud/users"
fctl "github.com/formancehq/fctl/pkg"
"github.com/spf13/cobra"
)
Expand All @@ -16,7 +15,6 @@ func NewCommand() *cobra.Command {
fctl.WithChildCommands(
organizations.NewCommand(),
me.NewCommand(),
users.NewCommand(),
regions.NewCommand(),
NewGeneratePersonalTokenCommand(),
),
Expand Down
Loading

1 comment on commit ff4c77c

@vercel
Copy link

@vercel vercel bot commented on ff4c77c Dec 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.