Skip to content

Commit

Permalink
feat: prompt for missing vcluster name in create command/cluster name…
Browse files Browse the repository at this point in the history
… in add command (interactive only)
  • Loading branch information
zerbitx committed Oct 10, 2024
1 parent 32c9c7f commit b2feba9
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
10 changes: 9 additions & 1 deletion cmd/vclusterctl/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"cmp"
"context"
"errors"
"fmt"

"github.com/loft-sh/log"
Expand Down Expand Up @@ -44,8 +45,15 @@ Example:
vcluster create test --namespace test
#######################################################
`,
Args: util.VClusterNameOnlyValidator,
RunE: func(cobraCmd *cobra.Command, args []string) error {
args, err := util.PromptForMissingArgs(cmd.log, args, "vcluster name")
if err != nil {
if errors.Is(err, util.ErrNonInteractive) {
return util.VClusterNameOnlyValidator(cobraCmd, args)
}
return err
}

// Check for newer version
upgrade.PrintNewerVersionWarning()

Expand Down
20 changes: 14 additions & 6 deletions cmd/vclusterctl/cmd/platform/add/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@ package add
import (
"cmp"
"context"
"errors"
"fmt"
"os"
"os/exec"
"time"

"github.com/loft-sh/log"
"github.com/sirupsen/logrus"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/wait"

managementv1 "github.com/loft-sh/api/v4/pkg/apis/management/v1"
storagev1 "github.com/loft-sh/api/v4/pkg/apis/storage/v1"
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/cli/util"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/loft-sh/vcluster/pkg/platform/clihelper"
"github.com/loft-sh/vcluster/pkg/platform/kube"
"github.com/loft-sh/vcluster/pkg/upgrade"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
Expand Down Expand Up @@ -60,8 +61,15 @@ Example:
vcluster platform add cluster my-cluster
########################################################
`,
Args: cobra.ExactArgs(1),
RunE: func(cobraCmd *cobra.Command, args []string) error {
args, err := util.PromptForMissingArgs(cmd.Log, args, "cluster name")
if err != nil {
if errors.Is(err, util.ErrNonInteractive) {
return cobra.ExactArgs(len(args))(cobraCmd, args)
}
return err
}

// Check for newer version
upgrade.PrintNewerVersionWarning()

Expand Down
10 changes: 9 additions & 1 deletion cmd/vclusterctl/cmd/platform/create/vcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package create

import (
"context"
"errors"

"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/pkg/cli"
Expand Down Expand Up @@ -39,8 +40,15 @@ Example:
vcluster platform create vcluster test --namespace test
#########################################################################
`,
Args: util.VClusterNameOnlyValidator,
RunE: func(cobraCmd *cobra.Command, args []string) error {
args, err := util.PromptForMissingArgs(cmd.log, args, "vcluster name")
if err != nil {
if errors.Is(err, util.ErrNonInteractive) {
return util.VClusterNameOnlyValidator(cobraCmd, args)
}
return err
}

// Check for newer version
upgrade.PrintNewerVersionWarning()

Expand Down
32 changes: 32 additions & 0 deletions pkg/cli/util/args.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package util

import (
"errors"
"fmt"
"strings"

"github.com/loft-sh/log"
"github.com/loft-sh/log/survey"
"github.com/loft-sh/log/terminal"
"github.com/spf13/cobra"
)

Expand All @@ -14,6 +18,8 @@ var (
VClusterNameOnlyUseLine string

VClusterNameOnlyValidator cobra.PositionalArgs

ErrNonInteractive = errors.New("terminal is not interactive")
)

func init() {
Expand Down Expand Up @@ -67,3 +73,29 @@ func NamedPositionalArgsValidator(failMissing, failExtra bool, expectedArgs ...s
return nil
}
}

// PromptForMissingArgs expects that the number of args, matched the number of argNames, in the order they should appear
// and will prompt one by one for the missing args adding them to the args slice and returning a new set for a command to use
// always returns the args, rather than a nil slice so they're unaltered in error cases.
func PromptForMissingArgs(l log.Logger, args []string, argNames ...string) ([]string, error) {
if !terminal.IsTerminalIn {
return args, ErrNonInteractive
}

if len(args) == len(argNames) {
return args, nil
}

for i := range argNames[len(args):] {
answer, err := l.Question(&survey.QuestionOptions{
Question: fmt.Sprintf("Please specify %s", argNames[i]),
})

if err != nil {
return args, err
}
args = append(args, answer)
}

return args, nil
}

0 comments on commit b2feba9

Please sign in to comment.