Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions cmd/common/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ func ValidateCronExpressionOpt(inp *textinput.TextInput) {
inp.Validate = ValidateCronExpression
}

func ValidateUniqueOpt(values []string) GetInputOption {
func ValidateUniqueOpt(values []string, msgAndArgs ...string) GetInputOption {
return func(inp *textinput.TextInput) {
inp.Validate = ValidateUnique(values)
inp.Validate = ValidateUnique(values, msgAndArgs...)
}
}

Expand Down
17 changes: 14 additions & 3 deletions cmd/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,23 @@ func ValidatePort(s string) error {
return nil
}

func ValidateUnique(values []string) func(string) error {
func ValidateUnique(values []string, msgAndArgs ...string) func(string) error {
return func(s string) error {
for _, v := range values {
if v == s {
return errors.New("must be unique")
if v != s {
continue
}
var format string
var args []any
if len(msgAndArgs) == 0 {
format = "must be unique"
} else {
format = msgAndArgs[0]
for _, m := range msgAndArgs[1:] {
args = append(args, m)
}
}
return fmt.Errorf(format, args...)
}
return nil
}
Expand Down
33 changes: 27 additions & 6 deletions cmd/rig/cmd/environment/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"connectrpc.com/connect"
"github.com/rigdev/rig-go-api/api/v1/cluster"
"github.com/rigdev/rig-go-api/api/v1/environment"
"github.com/rigdev/rig/cmd/common"
"github.com/rigdev/rig/pkg/errors"
Expand All @@ -25,24 +26,44 @@ func (c *Cmd) create(ctx context.Context, _ *cobra.Command, args []string) error

func (c *Cmd) createEnvironment(ctx context.Context,
name string,
cluster string,
clusterName string,
useNewEnvironment *bool) error {
var err error
if name == "" {
if !c.Scope.IsInteractive() {
return fmt.Errorf("missing environment argument")
}
name, err = c.Prompter.Input("Environment:", common.ValidateSystemNameOpt)
resp, err := c.Rig.Environment().List(ctx, connect.NewRequest(&environment.ListRequest{}))
if err != nil {
return err
}
var names []string
for _, e := range resp.Msg.GetEnvironments() {
names = append(names, e.GetEnvironmentId())
}
name, err = c.Prompter.Input(
"Environment:", common.ValidateSystemNameOpt, common.ValidateUniqueOpt(names, "the environment name already exists"),
)
if err != nil {
return err
}
}

if cluster == "" {
if clusterName == "" {
if !c.Scope.IsInteractive() {
return fmt.Errorf("missing cluster argument")
}
cluster, err = c.Prompter.Input("Cluster:", common.ValidateSystemNameOpt)

var names []string
clusters, err := c.Rig.Cluster().List(ctx, connect.NewRequest(&cluster.ListRequest{}))
if err != nil {
return err
}
for _, c := range clusters.Msg.GetClusters() {
names = append(names, c.GetClusterId())
}

_, clusterName, err = c.Prompter.Select("Cluster:", names, common.SelectEnableFilterOpt)
if err != nil {
return err
}
Expand All @@ -67,7 +88,7 @@ func (c *Cmd) createEnvironment(ctx context.Context,
_, err = c.Rig.Environment().Create(ctx, &connect.Request[environment.CreateRequest]{
Msg: &environment.CreateRequest{
EnvironmentId: name,
ClusterId: cluster,
ClusterId: clusterName,
Initializers: initializers,
NamespaceTemplate: namespaceTemplate,
Ephemeral: ephemeral,
Expand All @@ -82,7 +103,7 @@ func (c *Cmd) createEnvironment(ctx context.Context,
} else if err != nil {
return err
} else {
fmt.Printf("Successfully created environment %s in cluster %s\n", name, cluster)
fmt.Printf("Successfully created environment %s in cluster %s\n", name, clusterName)
}

if useNewEnvironment == nil {
Expand Down