Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions pkg/cli/commands/organizations/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package organizations

import (
"fmt"
"io"
"strings"
"time"

"github.com/superplanehq/superplane/pkg/cli/core"
"github.com/superplanehq/superplane/pkg/openapi_client"
)

func resolveOrganizationID(ctx core.CommandContext) (string, error) {
me, _, err := ctx.API.MeAPI.MeMe(ctx.Context).Execute()
if err != nil {
return "", err
}

if !me.HasOrganizationId() || strings.TrimSpace(me.GetOrganizationId()) == "" {
return "", fmt.Errorf("organization id not found for authenticated user")
}

return me.GetOrganizationId(), nil
}

func renderOrganization(stdout io.Writer, org openapi_client.OrganizationsOrganization) error {
metadata := org.GetMetadata()

_, _ = fmt.Fprintf(stdout, "ID: %s\n", metadata.GetId())
_, _ = fmt.Fprintf(stdout, "Name: %s\n", metadata.GetName())
_, _ = fmt.Fprintf(stdout, "Description: %s\n", metadata.GetDescription())
_, _ = fmt.Fprintf(stdout, "Versioning Enabled: %t\n", metadata.GetVersioningEnabled())
if metadata.HasCreatedAt() {
_, _ = fmt.Fprintf(stdout, "Created At: %s\n", metadata.GetCreatedAt().Format(time.RFC3339))
}
if metadata.HasUpdatedAt() {
_, _ = fmt.Fprintf(stdout, "Updated At: %s\n", metadata.GetUpdatedAt().Format(time.RFC3339))
}

return nil
}
32 changes: 32 additions & 0 deletions pkg/cli/commands/organizations/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package organizations

import (
"io"

"github.com/superplanehq/superplane/pkg/cli/core"
)

type getCommand struct{}

func (c *getCommand) Execute(ctx core.CommandContext) error {
organizationID, err := resolveOrganizationID(ctx)
if err != nil {
return err
}

response, _, err := ctx.API.OrganizationAPI.
OrganizationsDescribeOrganization(ctx.Context, organizationID).
Execute()
if err != nil {
return err
}

if !ctx.Renderer.IsText() {
return ctx.Renderer.Render(response)
}

org := response.GetOrganization()
return ctx.Renderer.RenderText(func(stdout io.Writer) error {
return renderOrganization(stdout, org)
})
}
43 changes: 43 additions & 0 deletions pkg/cli/commands/organizations/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package organizations

import (
"github.com/spf13/cobra"
"github.com/superplanehq/superplane/pkg/cli/core"
)

func NewCommand(options core.BindOptions) *cobra.Command {
root := &cobra.Command{
Use: "organizations",
Short: "Manage organizations",
Aliases: []string{"org", "orgs"},
}

getCmd := &cobra.Command{
Use: "get",
Short: "Get details for the current organization",
Args: cobra.NoArgs,
}
core.Bind(getCmd, &getCommand{}, options)

var updateName string
var updateDescription string
var updateVersioningEnabled bool
updateCmd := &cobra.Command{
Use: "update",
Short: "Update the current organization",
Args: cobra.NoArgs,
}
updateCmd.Flags().StringVar(&updateName, "name", "", "organization name")
updateCmd.Flags().StringVar(&updateDescription, "description", "", "organization description")
updateCmd.Flags().BoolVar(&updateVersioningEnabled, "versioning-enabled", false, "enable or disable global versioning")
core.Bind(updateCmd, &updateCommand{
name: &updateName,
description: &updateDescription,
versioningEnabled: &updateVersioningEnabled,
}, options)

root.AddCommand(getCmd)
root.AddCommand(updateCmd)

return root
}
62 changes: 62 additions & 0 deletions pkg/cli/commands/organizations/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package organizations

import (
"fmt"
"io"

"github.com/superplanehq/superplane/pkg/cli/core"
"github.com/superplanehq/superplane/pkg/openapi_client"
)

type updateCommand struct {
name *string
description *string
versioningEnabled *bool
}

func (c *updateCommand) Execute(ctx core.CommandContext) error {
if !ctx.Cmd.Flags().Changed("name") &&
!ctx.Cmd.Flags().Changed("description") &&
!ctx.Cmd.Flags().Changed("versioning-enabled") {
return fmt.Errorf("at least one flag must be provided: --name, --description, or --versioning-enabled")
}

organizationID, err := resolveOrganizationID(ctx)
if err != nil {
return err
}

metadata := openapi_client.OrganizationsOrganizationMetadata{}
if ctx.Cmd.Flags().Changed("name") {
metadata.SetName(*c.name)
}
if ctx.Cmd.Flags().Changed("description") {
metadata.SetDescription(*c.description)
}
if ctx.Cmd.Flags().Changed("versioning-enabled") {
metadata.SetVersioningEnabled(*c.versioningEnabled)
}

org := openapi_client.OrganizationsOrganization{}
org.SetMetadata(metadata)

body := openapi_client.OrganizationsUpdateOrganizationBody{}
body.SetOrganization(org)

response, _, err := ctx.API.OrganizationAPI.
OrganizationsUpdateOrganization(ctx.Context, organizationID).
Body(body).
Execute()
if err != nil {
return err
}

if !ctx.Renderer.IsText() {
return ctx.Renderer.Render(response)
}

updated := response.GetOrganization()
return ctx.Renderer.RenderText(func(stdout io.Writer) error {
return renderOrganization(stdout, updated)
})
}
2 changes: 2 additions & 0 deletions pkg/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
executions "github.com/superplanehq/superplane/pkg/cli/commands/executions"
index "github.com/superplanehq/superplane/pkg/cli/commands/index"
integrations "github.com/superplanehq/superplane/pkg/cli/commands/integrations"
organizations "github.com/superplanehq/superplane/pkg/cli/commands/organizations"
queue "github.com/superplanehq/superplane/pkg/cli/commands/queue"
secrets "github.com/superplanehq/superplane/pkg/cli/commands/secrets"
usage "github.com/superplanehq/superplane/pkg/cli/commands/usage"
Expand Down Expand Up @@ -57,6 +58,7 @@ func init() {
RootCmd.AddCommand(events.NewCommand(options))
RootCmd.AddCommand(index.NewCommand(options))
RootCmd.AddCommand(integrations.NewCommand(options))
RootCmd.AddCommand(organizations.NewCommand(options))
RootCmd.AddCommand(queue.NewCommand(options))
RootCmd.AddCommand(secrets.NewCommand(options))
RootCmd.AddCommand(usage.NewCommand(options))
Expand Down
Loading