|
|
@@ -2,6 +2,7 @@ package commands
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "errors"
|
|
|
"fmt"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
@@ -18,7 +19,8 @@ import (
|
|
|
"github.com/spf13/cobra"
|
|
|
)
|
|
|
|
|
|
-func registerCommand_Config(cliConf config.CLIConfig, currentProfile string) *cobra.Command {
|
|
|
+// func registerCommand_Config() *cobra.Command {
|
|
|
+func registerCommand_Config() *cobra.Command {
|
|
|
configCmd := &cobra.Command{
|
|
|
Use: "config",
|
|
|
Short: "Commands that control local configuration settings",
|
|
|
@@ -34,35 +36,26 @@ func registerCommand_Config(cliConf config.CLIConfig, currentProfile string) *co
|
|
|
Use: "set-project [id]",
|
|
|
Args: cobra.MaximumNArgs(1),
|
|
|
Short: "Saves the project id in the default configuration",
|
|
|
- Run: func(cmd *cobra.Command, args []string) {
|
|
|
- client, err := api.NewClientWithConfig(cmd.Context(), api.NewClientInput{
|
|
|
- BaseURL: fmt.Sprintf("%s/api", cliConf.Host),
|
|
|
- BearerToken: cliConf.Token,
|
|
|
- CookieFileName: "cookie.json",
|
|
|
- })
|
|
|
- if err != nil {
|
|
|
- _, _ = color.New(color.FgRed).Fprintf(os.Stderr, "error creating porter API client: %s\n", err.Error())
|
|
|
- os.Exit(1)
|
|
|
- }
|
|
|
-
|
|
|
+ RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
if len(args) == 0 {
|
|
|
- err := checkLoginAndRunWithConfig(cmd, cliConf, currentProfile, args, listAndSetProject)
|
|
|
+ err := checkLoginAndRunWithConfig(cmd, args, listAndSetProject)
|
|
|
if err != nil {
|
|
|
- os.Exit(1)
|
|
|
- }
|
|
|
- } else {
|
|
|
- projID, err := strconv.ParseUint(args[0], 10, 64)
|
|
|
- if err != nil {
|
|
|
- _, _ = color.New(color.FgRed).Fprintf(os.Stderr, "An error occurred: %s\n", err.Error())
|
|
|
- os.Exit(1)
|
|
|
+ if errors.Is(err, errCreatingPorterAPIClient) {
|
|
|
+ return errors.New("error creating porter API client. Please ensure you are logged in")
|
|
|
+ }
|
|
|
+ return fmt.Errorf("error checking login and setting project: %w", err)
|
|
|
}
|
|
|
-
|
|
|
- err = cliConf.SetProject(cmd.Context(), client, uint(projID), currentProfile)
|
|
|
- if err != nil {
|
|
|
- _, _ = color.New(color.FgRed).Fprintf(os.Stderr, "An error occurred: %s\n", err.Error())
|
|
|
- os.Exit(1)
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ err := checkLoginAndRunWithConfig(cmd, args, setProjectAndCluster)
|
|
|
+ if err != nil {
|
|
|
+ if errors.Is(err, errCreatingPorterAPIClient) {
|
|
|
+ return errors.New("error creating porter API client. Please ensure you are logged in")
|
|
|
}
|
|
|
+ return fmt.Errorf("error checking login and setting project with cluster: %w", err)
|
|
|
}
|
|
|
+
|
|
|
+ return nil
|
|
|
},
|
|
|
}
|
|
|
|
|
|
@@ -70,26 +63,36 @@ func registerCommand_Config(cliConf config.CLIConfig, currentProfile string) *co
|
|
|
Use: "set-cluster [id]",
|
|
|
Args: cobra.MaximumNArgs(1),
|
|
|
Short: "Saves the cluster id in the default configuration",
|
|
|
- Run: func(cmd *cobra.Command, args []string) {
|
|
|
+ RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
if len(args) == 0 {
|
|
|
- err := checkLoginAndRunWithConfig(cmd, cliConf, currentProfile, args, listAndSetCluster)
|
|
|
- if err != nil {
|
|
|
- os.Exit(1)
|
|
|
- }
|
|
|
- } else {
|
|
|
- clusterID, err := strconv.ParseUint(args[0], 10, 64)
|
|
|
+ err := checkLoginAndRunWithConfig(cmd, args, listAndSetCluster)
|
|
|
if err != nil {
|
|
|
- _, _ = color.New(color.FgRed).Fprintf(os.Stderr, "An error occurred: %v\n", err)
|
|
|
- os.Exit(1)
|
|
|
+ if errors.Is(err, errCreatingPorterAPIClient) {
|
|
|
+ return errors.New("error creating porter API client. Please ensure you are logged in")
|
|
|
+ }
|
|
|
+ return fmt.Errorf("error checking login and setting project: %w", err)
|
|
|
}
|
|
|
+ return nil
|
|
|
+ }
|
|
|
|
|
|
- err = cliConf.SetCluster(uint(clusterID))
|
|
|
+ _, currentProfile, err := currentProfileIncludingFlags(cmd)
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("error whilst initialising config: %w", err)
|
|
|
+ }
|
|
|
|
|
|
- if err != nil {
|
|
|
- _, _ = color.New(color.FgRed).Fprintf(os.Stderr, "An error occurred: %v\n", err)
|
|
|
- os.Exit(1)
|
|
|
- }
|
|
|
+ clusterID, err := strconv.ParseUint(args[0], 10, 64)
|
|
|
+ if err != nil {
|
|
|
+ _, _ = color.New(color.FgRed).Fprintf(os.Stderr, "An error occurred: %v\n", err)
|
|
|
+ os.Exit(1)
|
|
|
}
|
|
|
+
|
|
|
+ err = config.SetCluster(uint(clusterID), currentProfile)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ _, _ = color.New(color.FgRed).Fprintf(os.Stderr, "An error occurred: %v\n", err)
|
|
|
+ os.Exit(1)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
},
|
|
|
}
|
|
|
|
|
|
@@ -99,7 +102,7 @@ func registerCommand_Config(cliConf config.CLIConfig, currentProfile string) *co
|
|
|
Short: "Saves the registry id in the default configuration",
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
if len(args) == 0 {
|
|
|
- err := checkLoginAndRunWithConfig(cmd, cliConf, currentProfile, args, listAndSetRegistry)
|
|
|
+ err := checkLoginAndRunWithConfig(cmd, args, listAndSetRegistry)
|
|
|
if err != nil {
|
|
|
os.Exit(1)
|
|
|
}
|
|
|
@@ -110,7 +113,7 @@ func registerCommand_Config(cliConf config.CLIConfig, currentProfile string) *co
|
|
|
os.Exit(1)
|
|
|
}
|
|
|
|
|
|
- err = cliConf.SetRegistry(uint(registryID))
|
|
|
+ err = config.SetRegistry(uint(registryID))
|
|
|
|
|
|
if err != nil {
|
|
|
_, _ = color.New(color.FgRed).Fprintf(os.Stderr, "An error occurred: %v\n", err)
|
|
|
@@ -131,7 +134,7 @@ func registerCommand_Config(cliConf config.CLIConfig, currentProfile string) *co
|
|
|
os.Exit(1)
|
|
|
}
|
|
|
|
|
|
- err = cliConf.SetHelmRepo(uint(hrID))
|
|
|
+ err = config.SetHelmRepo(uint(hrID))
|
|
|
|
|
|
if err != nil {
|
|
|
_, _ = color.New(color.FgRed).Fprintf(os.Stderr, "An error occurred: %v\n", err)
|
|
|
@@ -144,12 +147,18 @@ func registerCommand_Config(cliConf config.CLIConfig, currentProfile string) *co
|
|
|
Use: "set-host [host]",
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
Short: "Saves the host in the default configuration",
|
|
|
- Run: func(cmd *cobra.Command, args []string) {
|
|
|
- err := cliConf.SetHost(args[0], currentProfile)
|
|
|
+ RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
+ _, currentProfile, err := currentProfileIncludingFlags(cmd)
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("error whilst initialising config: %w", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ err = config.SetHost(args[0], currentProfile)
|
|
|
if err != nil {
|
|
|
_, _ = color.New(color.FgRed).Fprintf(os.Stderr, "An error occurred: %s\n", err.Error())
|
|
|
- os.Exit(1)
|
|
|
+ return err
|
|
|
}
|
|
|
+ return nil
|
|
|
},
|
|
|
}
|
|
|
|
|
|
@@ -158,7 +167,7 @@ func registerCommand_Config(cliConf config.CLIConfig, currentProfile string) *co
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
Short: "Saves the path to kubeconfig in the default configuration",
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
- err := cliConf.SetKubeconfig(args[0])
|
|
|
+ err := config.SetKubeconfig(args[0])
|
|
|
if err != nil {
|
|
|
_, _ = color.New(color.FgRed).Fprintf(os.Stderr, "An error occurred: %s\n", err.Error())
|
|
|
os.Exit(1)
|
|
|
@@ -166,12 +175,22 @@ func registerCommand_Config(cliConf config.CLIConfig, currentProfile string) *co
|
|
|
},
|
|
|
}
|
|
|
|
|
|
+ configSetProfileCmd := &cobra.Command{
|
|
|
+ Use: "set-profile [profile-name]",
|
|
|
+ Args: cobra.ExactArgs(1),
|
|
|
+ Short: "Saves the path to kubeconfig in the default configuration",
|
|
|
+ RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
+ return config.SetProfile(args[0])
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
configCmd.AddCommand(configSetProjectCmd)
|
|
|
configCmd.AddCommand(configSetClusterCmd)
|
|
|
configCmd.AddCommand(configSetHostCmd)
|
|
|
configCmd.AddCommand(configSetRegistryCmd)
|
|
|
configCmd.AddCommand(configSetHelmRepoCmd)
|
|
|
configCmd.AddCommand(configSetKubeconfigCmd)
|
|
|
+ configCmd.AddCommand(configSetProfileCmd)
|
|
|
return configCmd
|
|
|
}
|
|
|
|
|
|
@@ -193,13 +212,12 @@ func listAndSetProject(ctx context.Context, _ *types.GetAuthenticatedUserRespons
|
|
|
s.Start()
|
|
|
|
|
|
resp, err := client.ListUserProjects(ctx)
|
|
|
-
|
|
|
- s.Stop()
|
|
|
-
|
|
|
if err != nil {
|
|
|
- return err
|
|
|
+ return fmt.Errorf("error listing projects to set config: %w", err)
|
|
|
}
|
|
|
|
|
|
+ s.Stop()
|
|
|
+
|
|
|
var projID uint64
|
|
|
|
|
|
if len(*resp) > 1 {
|
|
|
@@ -222,7 +240,7 @@ func listAndSetProject(ctx context.Context, _ *types.GetAuthenticatedUserRespons
|
|
|
projID = uint64((*resp)[0].ID)
|
|
|
}
|
|
|
|
|
|
- err = cliConf.SetProject(ctx, client, uint(projID), currentProfile)
|
|
|
+ err = config.SetProject(uint(projID), currentProfile)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
@@ -230,6 +248,28 @@ func listAndSetProject(ctx context.Context, _ *types.GetAuthenticatedUserRespons
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+func setProjectAndCluster(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, currentProfile string, featureFlags config.FeatureFlags, cmd *cobra.Command, args []string) error {
|
|
|
+ projID, err := strconv.ParseUint(args[0], 10, 64)
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("error parsing project id: %w", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ err = config.SetProject(uint(projID), currentProfile)
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("error setting project: %w", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ err = listAndSetProject(ctx, nil, client, cliConf, currentProfile, featureFlags, cmd, args)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ err = listAndSetCluster(ctx, nil, client, cliConf, currentProfile, featureFlags, cmd, args)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
func listAndSetCluster(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, currentProfile string, featureFlags config.FeatureFlags, cmd *cobra.Command, args []string) error {
|
|
|
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
|
|
|
_ = s.Color("cyan")
|
|
|
@@ -237,13 +277,12 @@ func listAndSetCluster(ctx context.Context, _ *types.GetAuthenticatedUserRespons
|
|
|
s.Start()
|
|
|
|
|
|
resp, err := client.ListProjectClusters(ctx, cliConf.Project)
|
|
|
-
|
|
|
- s.Stop()
|
|
|
-
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
+ s.Stop()
|
|
|
+
|
|
|
var clusterID uint64
|
|
|
|
|
|
if len(*resp) > 1 {
|
|
|
@@ -265,7 +304,7 @@ func listAndSetCluster(ctx context.Context, _ *types.GetAuthenticatedUserRespons
|
|
|
clusterID = uint64((*resp)[0].ID)
|
|
|
}
|
|
|
|
|
|
- err = cliConf.SetCluster(uint(clusterID))
|
|
|
+ err = config.SetCluster(uint(clusterID), currentProfile)
|
|
|
if err != nil {
|
|
|
return fmt.Errorf("unable to set cluster: %w", err)
|
|
|
}
|
|
|
@@ -308,7 +347,7 @@ func listAndSetRegistry(ctx context.Context, _ *types.GetAuthenticatedUserRespon
|
|
|
regID = uint64((*resp)[0].ID)
|
|
|
}
|
|
|
|
|
|
- err = cliConf.SetRegistry(uint(regID))
|
|
|
+ err = config.SetRegistry(uint(regID))
|
|
|
if err != nil {
|
|
|
return fmt.Errorf("error setting registry: %w", err)
|
|
|
}
|