Skip to content

Commit

Permalink
feat: check membership version (#1095)
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 Jan 4, 2024
1 parent cc25934 commit 25d2950
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
25 changes: 25 additions & 0 deletions components/fctl/cmd/cloud/organizations/users/root.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
package users

import (
"fmt"

fctl "github.com/formancehq/fctl/pkg"
"github.com/spf13/cobra"
"golang.org/x/mod/semver"
)

func NewCommand() *cobra.Command {
return fctl.NewMembershipCommand("users",
fctl.WithAliases("u"),
fctl.WithShortDescription("Users management"),
fctl.WithRunE(func(cmd *cobra.Command, args []string) error {
cfg, err := fctl.GetConfig(cmd)
if err != nil {
return err
}

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

version := fctl.MembershipServerInfo(cmd.Context(), apiClient)
if !semver.IsValid(version) {
return nil
}

if semver.Compare(version, "v0.26.1") >= 0 {
return nil
}

return fmt.Errorf("unsupported membership server version: %s", version)
}),
fctl.WithChildCommands(
NewListCommand(),
NewShowCommand(),
Expand Down
25 changes: 25 additions & 0 deletions components/fctl/cmd/stack/roles/root.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
package roles

import (
"fmt"

fctl "github.com/formancehq/fctl/pkg"
"github.com/spf13/cobra"
"golang.org/x/mod/semver"
)

func NewCommand() *cobra.Command {
return fctl.NewMembershipCommand("roles",
fctl.WithAliases("s"),
fctl.WithShortDescription("Stack users management within an organization"),
fctl.WithRunE(func(cmd *cobra.Command, args []string) error {
cfg, err := fctl.GetConfig(cmd)
if err != nil {
return err
}

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

version := fctl.MembershipServerInfo(cmd.Context(), apiClient)
if !semver.IsValid(version) {
return nil
}

if semver.Compare(version, "v0.26.1") >= 0 {
return nil
}

return fmt.Errorf("unsupported membership server version: %s", version)
}),
fctl.WithChildCommands(
NewUpsertCommand(),
NewListCommand(),
Expand Down
13 changes: 13 additions & 0 deletions components/fctl/pkg/clients.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fctl

import (
"context"
"fmt"

"github.com/formancehq/fctl/membershipclient"
Expand Down Expand Up @@ -33,6 +34,18 @@ func NewMembershipClient(cmd *cobra.Command, cfg *Config) (*membershipclient.API
return membershipclient.NewAPIClient(configuration), nil
}

func MembershipServerInfo(ctx context.Context, client *membershipclient.APIClient) string {
serverInfo, response, err := client.DefaultApi.GetServerInfo(ctx).Execute()
if err != nil {
return fmt.Sprintf("Error: %s", err)
}
if response.StatusCode != 200 {
return fmt.Sprintf("Error: %s", response.Status)
}
return serverInfo.Version
}


func NewStackClient(cmd *cobra.Command, cfg *Config, stack *membershipclient.Stack) (*formance.Formance, error) {
profile := GetCurrentProfile(cmd, cfg)
httpClient := GetHttpClient(cmd, map[string][]string{})
Expand Down
14 changes: 12 additions & 2 deletions components/fctl/pkg/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,6 @@ func WithStringArrayFlag(name string, defaultValue []string, help string) Comman
}
}



func WithHiddenFlag(name string) CommandOptionFn {
return func(cmd *cobra.Command) {
_ = cmd.Flags().MarkHidden(name)
Expand Down Expand Up @@ -248,6 +246,18 @@ func WithDeprecated(message string) CommandOptionFn {
func WithController[T any](c Controller[T]) CommandOptionFn {
return func(cmd *cobra.Command) {
cmd.RunE = func(cmd *cobra.Command, args []string) error {
// Run all parent commands
parent := cmd.Parent()
for parent != nil {
if parent.RunE != nil {
err := parent.RunE(parent, args)
if err != nil {
return err
}
}
parent = parent.Parent()
}

renderable, err := c.Run(cmd, args)

// If the controller return an argument error, we want to print the usage
Expand Down

0 comments on commit 25d2950

Please sign in to comment.