Mohammed Nafees 3 роки тому
батько
коміт
32e387d069

+ 2 - 2
cli/cmd/delete.go

@@ -32,7 +32,7 @@ deleting a configuration:
 		color.New(color.FgGreen, color.Bold).Sprintf("porter delete"),
 	),
 	Run: func(cmd *cobra.Command, args []string) {
-		err := checkLoginAndRun(args, delete)
+		err := checkLoginAndRun(args, deleteDeployment)
 
 		if err != nil {
 			os.Exit(1)
@@ -100,7 +100,7 @@ func init() {
 	rootCmd.AddCommand(deleteCmd)
 }
 
-func delete(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+func deleteDeployment(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
 	projectID := cliConf.Project
 
 	if projectID == 0 {

+ 110 - 8
cli/cmd/deploy.go

@@ -6,7 +6,9 @@ import (
 	"os"
 	"path/filepath"
 	"strings"
+	"time"
 
+	"github.com/briandowns/spinner"
 	"github.com/fatih/color"
 	api "github.com/porter-dev/porter/api/client"
 	"github.com/porter-dev/porter/api/types"
@@ -249,10 +251,10 @@ var forcePush bool
 var useCache bool
 var value string
 var version uint
+var varType string
 
 func init() {
 	buildFlagsEnv = []string{}
-
 	rootCmd.AddCommand(updateCmd)
 
 	updateCmd.PersistentFlags().StringVar(
@@ -262,8 +264,6 @@ func init() {
 		"Application in the Porter dashboard",
 	)
 
-	updateCmd.MarkPersistentFlagRequired("app")
-
 	updateCmd.PersistentFlags().BoolVar(
 		&useCache,
 		"use-cache",
@@ -365,6 +365,14 @@ func init() {
 		"file destination for .env files",
 	)
 
+	updateGetEnvCmd.MarkPersistentFlagRequired("app")
+
+	updateBuildCmd.MarkPersistentFlagRequired("app")
+
+	updatePushCmd.MarkPersistentFlagRequired("app")
+
+	updateConfigCmd.MarkPersistentFlagRequired("app")
+
 	updateEnvGroupCmd.PersistentFlags().StringVar(
 		&name,
 		"name",
@@ -379,6 +387,15 @@ func init() {
 		"the version of the environment group",
 	)
 
+	updateEnvGroupCmd.MarkPersistentFlagRequired("name")
+
+	updateSetEnvGroupCmd.PersistentFlags().StringVar(
+		&varType,
+		"type",
+		"normal",
+		"the type of environment variable (either \"normal\" or \"secret\")",
+	)
+
 	updateEnvGroupCmd.AddCommand(updateSetEnvGroupCmd)
 	updateEnvGroupCmd.AddCommand(updateUnsetEnvGroupCmd)
 
@@ -494,27 +511,112 @@ func updateUpgrade(_ *types.GetAuthenticatedUserResponse, client *api.Client, ar
 
 func updateSetEnvGroup(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
 	if len(args) == 0 {
-		return fmt.Errorf("need variable in the form VAR=VALUE")
+		return fmt.Errorf("required variable in the form of VAR=VALUE")
 	}
 
 	key, value, found := strings.Cut(args[0], "=")
 
 	if !found {
-		return fmt.Errorf("need variable in the form VAR=VALUE")
+		return fmt.Errorf("variable should be in the form of VAR=VALUE")
 	}
 
-	envGroup, err := client.GetEnvGroup(context.Background(), cliConf.Project, cliConf.Cluster, namespace, &types.GetEnvGroupRequest{
-		Name: name, Version: version,
-	})
+	s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
+	s.Color("cyan")
+
+	s.Suffix = fmt.Sprintf(" Fetching env group '%s' in namespace '%s'", name, namespace)
+	s.Start()
+
+	envGroupResp, err := client.GetEnvGroup(context.Background(), cliConf.Project, cliConf.Cluster, namespace,
+		&types.GetEnvGroupRequest{
+			Name: name, Version: version,
+		},
+	)
+
+	s.Stop()
+
+	if err != nil {
+		return err
+	}
+
+	newEnvGroup := &types.CreateEnvGroupRequest{
+		Name:      envGroupResp.Name,
+		Variables: envGroupResp.Variables,
+	}
+
+	delete(newEnvGroup.Variables, key)
+
+	if varType == "secret" {
+		newEnvGroup.SecretVariables = make(map[string]string)
+		newEnvGroup.SecretVariables[key] = value
+
+		s.Suffix = fmt.Sprintf(" Adding new secret variable '%s' to env group '%s' in namespace '%s'", key, name, namespace)
+	} else {
+		newEnvGroup.Variables[key] = value
+
+		s.Suffix = fmt.Sprintf(" Adding new variable '%s' to env group '%s' in namespace '%s'", key, name, namespace)
+	}
+
+	s.Start()
+
+	_, err = client.CreateEnvGroup(
+		context.Background(), cliConf.Project, cliConf.Cluster, namespace, newEnvGroup,
+	)
+
+	s.Stop()
 
 	if err != nil {
 		return err
 	}
 
+	color.New(color.FgGreen).Println("env group successfully updated")
+
 	return nil
 }
 
 func updateUnsetEnvGroup(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+	if len(args) == 0 {
+		return fmt.Errorf("required variable name")
+	}
+
+	s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
+	s.Color("cyan")
+
+	s.Suffix = fmt.Sprintf(" Fetching env group '%s' in namespace '%s'", name, namespace)
+	s.Start()
+
+	envGroupResp, err := client.GetEnvGroup(context.Background(), cliConf.Project, cliConf.Cluster, namespace,
+		&types.GetEnvGroupRequest{
+			Name: name, Version: version,
+		},
+	)
+
+	s.Stop()
+
+	if err != nil {
+		return err
+	}
+
+	newEnvGroup := &types.CreateEnvGroupRequest{
+		Name:      envGroupResp.Name,
+		Variables: envGroupResp.Variables,
+	}
+
+	delete(newEnvGroup.Variables, args[0])
+
+	s.Suffix = fmt.Sprintf(" Removing variable '%s' from env group '%s' in namespace '%s'", args[0], name, namespace)
+
+	s.Start()
+
+	_, err = client.CreateEnvGroup(
+		context.Background(), cliConf.Project, cliConf.Cluster, namespace, newEnvGroup,
+	)
+
+	s.Stop()
+
+	if err != nil {
+		return err
+	}
+
 	return nil
 }
 

+ 10 - 4
cli/cmd/preview/env_group_driver.go

@@ -67,7 +67,7 @@ func (d *EnvGroupDriver) Apply(resource *models.Resource) (*models.Resource, err
 			group.Namespace = d.target.Namespace
 		}
 
-		envGroup, err := client.GetEnvGroup(
+		envGroupResp, err := client.GetEnvGroup(
 			context.Background(),
 			d.target.Project,
 			d.target.Cluster,
@@ -78,7 +78,7 @@ func (d *EnvGroupDriver) Apply(resource *models.Resource) (*models.Resource, err
 		)
 
 		if err != nil && err.Error() == "env group not found" {
-			envGroup, err = client.CreateEnvGroup(
+			newEnvGroup, err := client.CreateEnvGroup(
 				context.Background(), d.target.Project, d.target.Cluster, group.Namespace,
 				&types.CreateEnvGroupRequest{
 					Name:      group.Name,
@@ -89,12 +89,18 @@ func (d *EnvGroupDriver) Apply(resource *models.Resource) (*models.Resource, err
 			if err != nil {
 				return nil, err
 			}
+
+			envGroupResp = &types.GetEnvGroupResponse{
+				EnvGroup: &types.EnvGroup{
+					Variables: newEnvGroup.Variables,
+				},
+			}
 		} else if err != nil {
 			return nil, err
 		}
 
-		d.output[envGroup.Name] = map[string]interface{}{
-			"variables": envGroup.Variables,
+		d.output[envGroupResp.Name] = map[string]interface{}{
+			"variables": envGroupResp.Variables,
 		}
 	}
 

+ 4 - 0
internal/kubernetes/envgroup/create.go

@@ -85,6 +85,10 @@ func CreateEnvGroup(agent *kubernetes.Agent, input types.ConfigMapInput) (*v1.Co
 
 	oldSecret, _, err := agent.GetLatestVersionedSecret(input.Name, input.Namespace)
 
+	if input.SecretVariables == nil {
+		input.SecretVariables = make(map[string]string)
+	}
+
 	if err != nil && !errors.Is(err, kubernetes.IsNotFoundError) {
 		return nil, err
 	} else if err == nil && oldSecret != nil {