Skip to content

Commit

Permalink
feat(fctl): add configurable scope on create, update, display on list
Browse files Browse the repository at this point in the history
  • Loading branch information
David Ragot committed Dec 22, 2023
1 parent 9846958 commit 94d6b5c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
7 changes: 7 additions & 0 deletions components/fctl/cmd/auth/clients/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type CreateClient struct {
IsPublic string `json:"isPublic"`
RedirectUri string `json:"redirectUri"`
PostLogoutRedirectUri string `json:"postLogoutRedirectUri"`
Scopes string `json:"scopes"`
}

type CreateStore struct {
Expand All @@ -29,6 +30,7 @@ type CreateController struct {
descriptionFlag string
redirectUriFlag string
postLogoutRedirectUriFlag string
scopes string
}

var _ fctl.Controller[*CreateStore] = (*CreateController)(nil)
Expand All @@ -45,6 +47,7 @@ func NewCreateController() *CreateController {
descriptionFlag: "description",
redirectUriFlag: "redirect-uri",
postLogoutRedirectUriFlag: "post-logout-redirect-uri",
scopes: "scopes",
}
}

Expand All @@ -59,6 +62,7 @@ func NewCreateCommand() *cobra.Command {
fctl.WithStringFlag(c.descriptionFlag, "", "Client description"),
fctl.WithStringSliceFlag(c.redirectUriFlag, []string{}, "Redirect URIS"),
fctl.WithStringSliceFlag(c.postLogoutRedirectUriFlag, []string{}, "Post logout redirect uris"),
fctl.WithStringSliceFlag(c.scopes, []string{""}, "Scopes"),
fctl.WithShortDescription("Create client"),
fctl.WithController[*CreateStore](c),
)
Expand Down Expand Up @@ -104,6 +108,7 @@ func (c *CreateController) Run(cmd *cobra.Command, args []string) (fctl.Renderab
Name: args[0],
Trusted: &trusted,
PostLogoutRedirectUris: fctl.GetStringSlice(cmd, c.postLogoutRedirectUriFlag),
Scopes: fctl.GetStringSlice(cmd, c.scopes),
}
response, err := authClient.Auth.CreateClient(cmd.Context(), &request)
if err != nil {
Expand All @@ -121,6 +126,7 @@ func (c *CreateController) Run(cmd *cobra.Command, args []string) (fctl.Renderab
IsPublic: fctl.BoolPointerToString(response.CreateClientResponse.Data.Public),
RedirectUri: strings.Join(response.CreateClientResponse.Data.RedirectUris, ","),
PostLogoutRedirectUri: strings.Join(response.CreateClientResponse.Data.PostLogoutRedirectUris, ","),
Scopes: strings.Join(response.CreateClientResponse.Data.Scopes, ","),
}

return c, nil
Expand All @@ -134,6 +140,7 @@ func (c *CreateController) Render(cmd *cobra.Command, args []string) error {
tableData = append(tableData, []string{pterm.LightCyan("Public"), c.store.Client.IsPublic})
tableData = append(tableData, []string{pterm.LightCyan("Redirect URIs"), c.store.Client.RedirectUri})
tableData = append(tableData, []string{pterm.LightCyan("Post logout redirect URIs"), c.store.Client.PostLogoutRedirectUri})
tableData = append(tableData, []string{pterm.LightCyan("Scopes"), c.store.Client.Scopes})
return pterm.DefaultTable.
WithWriter(cmd.OutOrStdout()).
WithData(tableData).
Expand Down
6 changes: 3 additions & 3 deletions components/fctl/cmd/auth/clients/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Client struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Scope string `json:"scope"`
Scopes string `json:"scopes"`
IsPublic string `json:"isPublic"`
}

Expand Down Expand Up @@ -90,7 +90,7 @@ func (c *ListController) Run(cmd *cobra.Command, args []string) (fctl.Renderable
}
return ""
}(),
Scope: strings.Join(o.Scopes, ","),
Scopes: strings.Join(o.Scopes, ","),
IsPublic: fctl.BoolPointerToString(o.Public),
}
})
Expand All @@ -104,7 +104,7 @@ func (c *ListController) Render(cmd *cobra.Command, args []string) error {
o.ID,
o.Name,
o.Description,
o.Scope,
o.Scopes,
o.IsPublic,
}
})
Expand Down
7 changes: 7 additions & 0 deletions components/fctl/cmd/auth/clients/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type UpdateClient struct {
IsPublic string `json:"isPublic"`
RedirectUri string `json:"redirectUri"`
PostLogoutRedirectUri string `json:"postLogoutRedirectUri"`
Scopes string `json:"scopes"`
}

type UpdateStore struct {
Expand All @@ -34,6 +35,7 @@ type UpdateController struct {
descriptionFlag string
redirectUriFlag string
postLogoutRedirectUriFlag string
scopes string
}

var _ fctl.Controller[*UpdateStore] = (*UpdateController)(nil)
Expand All @@ -52,6 +54,7 @@ func NewUpdateController() *UpdateController {
descriptionFlag: "description",
redirectUriFlag: "redirect-uri",
postLogoutRedirectUriFlag: "post-logout-redirect-uri",
scopes: "scopes",
}
}

Expand All @@ -67,6 +70,7 @@ func NewUpdateCommand() *cobra.Command {
fctl.WithStringFlag(c.descriptionFlag, "", "Client description"),
fctl.WithStringSliceFlag(c.redirectUriFlag, []string{}, "Redirect URIS"),
fctl.WithStringSliceFlag(c.postLogoutRedirectUriFlag, []string{}, "Post logout redirect uris"),
fctl.WithStringSliceFlag(c.scopes, []string{}, "Scopes"),
fctl.WithController[*UpdateStore](c),
)
}
Expand Down Expand Up @@ -114,6 +118,7 @@ func (c *UpdateController) Run(cmd *cobra.Command, args []string) (fctl.Renderab
Name: args[0],
Trusted: &trusted,
PostLogoutRedirectUris: fctl.GetStringSlice(cmd, c.postLogoutRedirectUriFlag),
Scopes: fctl.GetStringSlice(cmd, c.scopes),
},
}
response, err := authClient.Auth.UpdateClient(cmd.Context(), request)
Expand All @@ -131,6 +136,7 @@ func (c *UpdateController) Run(cmd *cobra.Command, args []string) (fctl.Renderab
c.store.Client.IsPublic = fctl.BoolPointerToString(response.UpdateClientResponse.Data.Public)
c.store.Client.RedirectUri = strings.Join(response.UpdateClientResponse.Data.RedirectUris, ",")
c.store.Client.PostLogoutRedirectUri = strings.Join(response.UpdateClientResponse.Data.PostLogoutRedirectUris, ",")
c.store.Client.Scopes = strings.Join(response.UpdateClientResponse.Data.Scopes, ",")

return c, nil
}
Expand All @@ -143,6 +149,7 @@ func (c *UpdateController) Render(cmd *cobra.Command, args []string) error {
tableData = append(tableData, []string{pterm.LightCyan("Public"), c.store.Client.IsPublic})
tableData = append(tableData, []string{pterm.LightCyan("Redirect URIs"), c.store.Client.RedirectUri})
tableData = append(tableData, []string{pterm.LightCyan("Post logout redirect URIs"), c.store.Client.PostLogoutRedirectUri})
tableData = append(tableData, []string{pterm.LightCyan("Scopes"), c.store.Client.Scopes})
return pterm.DefaultTable.
WithWriter(cmd.OutOrStdout()).
WithData(tableData).
Expand Down
4 changes: 4 additions & 0 deletions components/fctl/cmd/auth/clients/views/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package views
import (
"fmt"
"io"
"strings"

fctl "github.com/formancehq/fctl/pkg"
"github.com/formancehq/formance-sdk-go/pkg/models/shared"
Expand All @@ -15,6 +16,9 @@ func PrintClient(out io.Writer, client *shared.Client) error {
tableData = append(tableData, []string{pterm.LightCyan("Name"), client.Name})
tableData = append(tableData, []string{pterm.LightCyan("Description"), fctl.StringPointerToString(client.Description)})
tableData = append(tableData, []string{pterm.LightCyan("Public"), fctl.BoolPointerToString(client.Public)})
if len(client.Scopes) > 0 {
tableData = append(tableData, []string{pterm.LightCyan("Scopes"), strings.Join(client.Scopes, ", ")})
}

fctl.Section.WithWriter(out).Println("Information :")
if err := pterm.DefaultTable.
Expand Down

0 comments on commit 94d6b5c

Please sign in to comment.