github.com/kubeshop/testkube@v1.17.23/cmd/kubectl-testkube/commands/config/namespace.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/spf13/cobra"
     7  	"k8s.io/apimachinery/pkg/util/validation"
     8  
     9  	"github.com/kubeshop/testkube/cmd/kubectl-testkube/config"
    10  	"github.com/kubeshop/testkube/pkg/ui"
    11  )
    12  
    13  func NewConfigureNamespaceCmd() *cobra.Command {
    14  	cmd := &cobra.Command{
    15  		Use:   "namespace <value>",
    16  		Short: "Set namespace for testkube client",
    17  		Args: func(cmd *cobra.Command, args []string) error {
    18  			if len(args) < 1 {
    19  				return fmt.Errorf("please pass valid namespace name")
    20  			}
    21  
    22  			errors := validation.IsDNS1123Subdomain(args[0])
    23  			if len(errors) > 0 {
    24  				return fmt.Errorf("invalid name, errors: %v", errors)
    25  			}
    26  
    27  			return nil
    28  		},
    29  		Run: func(cmd *cobra.Command, args []string) {
    30  
    31  			cfg, err := config.Load()
    32  			ui.ExitOnError("loading config file", err)
    33  
    34  			cfg.Namespace = args[0]
    35  			err = config.Save(cfg)
    36  			ui.ExitOnError("saving config file", err)
    37  			ui.Success("New namespace set to", cfg.Namespace)
    38  		},
    39  	}
    40  
    41  	return cmd
    42  }