Skip to content

Commit

Permalink
feat(fctl): allow to select versions on stack creation (#1053)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag authored Dec 21, 2023
1 parent 4ba6f67 commit d46aa0e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
8 changes: 7 additions & 1 deletion components/fctl/cmd/cloud/regions/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,15 @@ func (c *ListController) Render(cmd *cobra.Command, args []string) error {
}
return "Formance Cloud"
}(),
func() string {
if i.Version == nil {
return ""
}
return *i.Version
}(),
}
})
tableData = fctl.Prepend(tableData, []string{"ID", "Name", "Base url", "Public", "Active", "Last ping", "Owner"})
tableData = fctl.Prepend(tableData, []string{"ID", "Name", "Base url", "Public", "Active", "Last ping", "Owner", "Version"})
return pterm.DefaultTable.
WithHasHeader().
WithWriter(cmd.OutOrStdout()).
Expand Down
50 changes: 48 additions & 2 deletions components/fctl/cmd/stack/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"net/http"

"github.com/formancehq/stack/libs/go-libs/pointer"

"github.com/formancehq/fctl/cmd/stack/internal"
"github.com/formancehq/fctl/membershipclient"
fctl "github.com/formancehq/fctl/pkg"
Expand All @@ -17,6 +19,7 @@ const (
unprotectFlag = "unprotect"
regionFlag = "region"
nowaitFlag = "no-wait"
versionFlag = "version"
)

type StackCreateStore struct {
Expand Down Expand Up @@ -50,6 +53,7 @@ func NewCreateCommand() *cobra.Command {
fctl.WithArgs(cobra.RangeArgs(0, 1)),
fctl.WithBoolFlag(unprotectFlag, false, "Unprotect stacks (no confirmation on write commands)"),
fctl.WithStringFlag(regionFlag, "", "Region on which deploy the stack"),
fctl.WithStringFlag(versionFlag, "", "Version of the stack"),
fctl.WithBoolFlag(nowaitFlag, false, "Not wait stack availability"),
fctl.WithController[*StackCreateStore](NewStackCreateController()),
)
Expand Down Expand Up @@ -123,11 +127,53 @@ func (c *StackCreateController) Run(cmd *cobra.Command, args []string) (fctl.Ren
}
}

stackResponse, _, err := apiClient.DefaultApi.CreateStack(cmd.Context(), organization).CreateStackRequest(membershipclient.CreateStackRequest{
req := membershipclient.CreateStackRequest{
Name: name,
Metadata: metadata,
RegionID: region,
}).Execute()
}

availableVersions, httpResponse, err := apiClient.DefaultApi.GetRegionVersions(cmd.Context(), organization, region).Execute()
if httpResponse == nil {
return nil, err
}

specifiedVersion := fctl.GetString(cmd, versionFlag)

switch {
case httpResponse.StatusCode == http.StatusOK && specifiedVersion == "":
var options []string
for _, version := range availableVersions.Data {
options = append(options, version.Name)
}

printer := pterm.DefaultInteractiveSelect.WithOptions(options)
selectedOption, err := printer.Show("Please select a version")
if err != nil {
return nil, err
}
for i := 0; i < len(options); i++ {
if selectedOption == options[i] {
specifiedVersion = availableVersions.Data[i].Name
break
}
}
case httpResponse.StatusCode != http.StatusOK && specifiedVersion == "":
// nothing to do, we cannot set a specific version for this membership version
case httpResponse.StatusCode == http.StatusOK && specifiedVersion != "":
// nothing to do, let membership handle the case
case httpResponse.StatusCode != http.StatusOK && specifiedVersion != "":
return nil, errors.New("--version flag can not be used with the actual membership api")
}

if specifiedVersion != "" {
req.Version = pointer.For(specifiedVersion)
}

stackResponse, _, err := apiClient.DefaultApi.
CreateStack(cmd.Context(), organization).
CreateStackRequest(req).
Execute()
if err != nil {
return nil, errors.Wrap(err, "creating stack")
}
Expand Down

0 comments on commit d46aa0e

Please sign in to comment.