From d4c6410652012b473a2e4d766f1a6e99804d1807 Mon Sep 17 00:00:00 2001 From: J Yash Sakariya Jain <113010737+yashjaind11@users.noreply.github.com> Date: Wed, 28 Aug 2024 16:41:38 +0530 Subject: [PATCH] added set env cmd and use default env when running (#249) * added set env cmd and use default env when running * changing environment to env --- cmd/create/environment.go | 2 +- cmd/delete/environment.go | 2 +- cmd/deploy/service-set.go | 9 +++----- cmd/deploy/service.go | 8 ++----- cmd/list/environment.go | 2 +- cmd/operate/component.go | 6 ++--- cmd/operate/service.go | 6 ++--- cmd/set/env.go | 28 +++++++++++++++++++++++ cmd/undeploy/service.go | 7 +++--- internal/service/service.go | 8 +++---- pkg/config/config.go | 45 +++++++++++++++++++++++++++++++++++++ 11 files changed, 94 insertions(+), 29 deletions(-) create mode 100644 cmd/set/env.go diff --git a/cmd/create/environment.go b/cmd/create/environment.go index af87c935..775c361a 100644 --- a/cmd/create/environment.go +++ b/cmd/create/environment.go @@ -16,7 +16,7 @@ var environmentClient service.Environment // environmentCmd represents the environment command var environmentCmd = &cobra.Command{ - Use: "environment", + Use: "env", Short: "Create environment", Args: func(cmd *cobra.Command, args []string) error { return cobra.NoArgs(cmd, args) diff --git a/cmd/delete/environment.go b/cmd/delete/environment.go index 41d666bd..c7ec94cf 100644 --- a/cmd/delete/environment.go +++ b/cmd/delete/environment.go @@ -12,7 +12,7 @@ var name string var environmentClient = service.Environment{} var environmentCmd = &cobra.Command{ - Use: "environment", + Use: "env", Short: "Delete environment", Long: `Delete environment`, Args: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/deploy/service-set.go b/cmd/deploy/service-set.go index 5db7c108..59a106cf 100644 --- a/cmd/deploy/service-set.go +++ b/cmd/deploy/service-set.go @@ -4,6 +4,7 @@ import ( "encoding/json" "os" + "github.com/dream11/odin/pkg/config" serviceDto "github.com/dream11/odin/proto/gen/go/dream11/od/dto/v1" serviceProto "github.com/dream11/odin/proto/gen/go/dream11/od/service/v1" log "github.com/sirupsen/logrus" @@ -28,16 +29,12 @@ func init() { serviceSetDeployCmd.Flags().StringVar(&env, "env", "", "environment for deploying the service-set") serviceSetDeployCmd.Flags().StringVar(&provisioningFile, "file", "", "path to the service set provisioning file") serviceSetDeployCmd.Flags().StringVar(&serviceSetName, "name", "", "released service set name") - - err := serviceSetDeployCmd.MarkFlagRequired("env") - if err != nil { - log.Fatal("Error marking 'env' flag as required:", err) - } - deployCmd.AddCommand(serviceSetDeployCmd) } func executeDeployServiceSet(cmd *cobra.Command) { + env = config.EnsureEnvPresent(env) + ctx := cmd.Context() if serviceSetName == "" && provisioningFile == "" { log.Fatal("Please provide either --name or --file.") diff --git a/cmd/deploy/service.go b/cmd/deploy/service.go index 217836e1..3a201218 100644 --- a/cmd/deploy/service.go +++ b/cmd/deploy/service.go @@ -5,6 +5,7 @@ import ( "os" "github.com/dream11/odin/internal/service" + "github.com/dream11/odin/pkg/config" serviceDto "github.com/dream11/odin/proto/gen/go/dream11/od/dto/v1" serviceProto "github.com/dream11/odin/proto/gen/go/dream11/od/service/v1" log "github.com/sirupsen/logrus" @@ -36,16 +37,11 @@ func init() { serviceCmd.Flags().StringVar(&provisioningFile, "provisioning", "", "path to the provisioning file") serviceCmd.Flags().StringVar(&serviceName, "name", "", "released service name") serviceCmd.Flags().StringVar(&serviceVersion, "version", "", "released service version") - - err := serviceCmd.MarkFlagRequired("env") - if err != nil { - log.Fatal("Error marking 'env' flag as required:", err) - } - deployCmd.AddCommand(serviceCmd) } func execute(cmd *cobra.Command) { + env = config.EnsureEnvPresent(env) ctx := cmd.Context() if (serviceName == "" && serviceVersion == "") && (definitionFile != "" && provisioningFile != "") { definitionData, err := os.ReadFile(definitionFile) diff --git a/cmd/list/environment.go b/cmd/list/environment.go index 2ae02453..648c2aae 100644 --- a/cmd/list/environment.go +++ b/cmd/list/environment.go @@ -20,7 +20,7 @@ var displayAll bool var environmentClient = service.Environment{} var environmentCmd = &cobra.Command{ - Use: "environment", + Use: "env", Short: "List environments", Args: func(cmd *cobra.Command, args []string) error { return cobra.NoArgs(cmd, args) diff --git a/cmd/operate/component.go b/cmd/operate/component.go index df28c2bd..87c36bde 100644 --- a/cmd/operate/component.go +++ b/cmd/operate/component.go @@ -4,6 +4,7 @@ import ( "encoding/json" "github.com/dream11/odin/internal/service" + "github.com/dream11/odin/pkg/config" fileUtil "github.com/dream11/odin/pkg/util" serviceProto "github.com/dream11/odin/proto/gen/go/dream11/od/service/v1" log "github.com/sirupsen/logrus" @@ -43,9 +44,6 @@ func init() { if err := operateComponentCmd.MarkFlagRequired("service"); err != nil { log.Fatal("Error marking 'service' flag as required:", err) } - if err := operateComponentCmd.MarkFlagRequired("env"); err != nil { - log.Fatal("Error marking 'env' flag as required:", err) - } if err := operateComponentCmd.MarkFlagRequired("operation"); err != nil { log.Fatal("Error marking 'operation' flag as required:", err) } @@ -53,6 +51,8 @@ func init() { } func execute(cmd *cobra.Command) { + env = config.EnsureEnvPresent(env) + ctx := cmd.Context() //validate the variables var optionsData map[string]interface{} diff --git a/cmd/operate/service.go b/cmd/operate/service.go index 23d6d777..a1fe1ea9 100644 --- a/cmd/operate/service.go +++ b/cmd/operate/service.go @@ -4,6 +4,7 @@ import ( "encoding/json" "github.com/dream11/odin/internal/service" + "github.com/dream11/odin/pkg/config" fileUtil "github.com/dream11/odin/pkg/util" serviceProto "github.com/dream11/odin/proto/gen/go/dream11/od/service/v1" log "github.com/sirupsen/logrus" @@ -33,9 +34,6 @@ func init() { if err := operateServiceCmd.MarkFlagRequired("name"); err != nil { log.Fatal("Error marking 'name' flag as required:", err) } - if err := operateServiceCmd.MarkFlagRequired("env"); err != nil { - log.Fatal("Error marking 'env' flag as required:", err) - } if err := operateServiceCmd.MarkFlagRequired("operation"); err != nil { log.Fatal("Error marking 'operation' flag as required:", err) } @@ -43,6 +41,8 @@ func init() { } func executeOperateService(cmd *cobra.Command) { + env = config.EnsureEnvPresent(env) + ctx := cmd.Context() //validate the variables var optionsData map[string]interface{} diff --git a/cmd/set/env.go b/cmd/set/env.go new file mode 100644 index 00000000..cc980869 --- /dev/null +++ b/cmd/set/env.go @@ -0,0 +1,28 @@ +package set + +import ( + "github.com/dream11/odin/pkg/config" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +// profileCmd represents the profile command +var setEnvCmd = &cobra.Command{ + Use: "env", + Short: "odin set default environment", + Long: `modify environment in config file`, + Run: func(cmd *cobra.Command, args []string) { + if len(args) < 1 { + log.Fatal("Error: need one more parameter for environment name") + } + setEnvironment(args[0]) + }, +} + +func init() { + setCmd.AddCommand(setEnvCmd) +} + +func setEnvironment(envName string) { + config.UpdateEnvName(envName) +} diff --git a/cmd/undeploy/service.go b/cmd/undeploy/service.go index 779c618f..f909977c 100644 --- a/cmd/undeploy/service.go +++ b/cmd/undeploy/service.go @@ -2,6 +2,7 @@ package undeploy import ( "github.com/dream11/odin/internal/service" + "github.com/dream11/odin/pkg/config" serviceProto "github.com/dream11/odin/proto/gen/go/dream11/od/service/v1" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -31,14 +32,12 @@ func init() { if err != nil { log.Fatal("Error marking 'name' flag as required:", err) } - err = serviceCmd.MarkFlagRequired("env") - if err != nil { - log.Fatal("Error marking 'name' flag as required:", err) - } undeployCmd.AddCommand(serviceCmd) } func execute(cmd *cobra.Command) { + envName = config.EnsureEnvPresent(envName) + ctx := cmd.Context() err := serviceClient.UndeployService(&ctx, &serviceProto.UndeployServiceRequest{ diff --git a/internal/service/service.go b/internal/service/service.go index bcc7260f..e4965ca4 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -50,7 +50,7 @@ func (e *Service) DeployService(ctx *context.Context, request *serviceProto.Depl message = response.ServiceResponse.Message message += fmt.Sprintf("\n Service %s %s", response.ServiceResponse.ServiceStatus.ServiceAction, response.ServiceResponse.ServiceStatus) for _, compMessage := range response.ServiceResponse.ComponentsStatus { - message += fmt.Sprintf("\n Component %s %s %s %s", compMessage.ComponentName, compMessage.ComponentAction, compMessage.ComponentStatus,compMessage.Error) + message += fmt.Sprintf("\n Component %s %s %s %s", compMessage.ComponentName, compMessage.ComponentAction, compMessage.ComponentStatus, compMessage.Error) } spinnerInstance.Prefix = fmt.Sprintf(" %s ", message) spinnerInstance.Start() @@ -92,11 +92,11 @@ func (e *Service) DeployServiceSet(ctx *context.Context, request *serviceProto.D } if response != nil { - message="" + message = "" for index, serviceRespose := range response.GetServices() { - message += fmt.Sprintf("\n Service:%d %s %s %s",index+1 , serviceRespose.ServiceIdentifier, serviceRespose.ServiceResponse.ServiceStatus, serviceRespose.ServiceResponse.Message) + message += fmt.Sprintf("\n Service:%d %s %s %s", index+1, serviceRespose.ServiceIdentifier, serviceRespose.ServiceResponse.ServiceStatus, serviceRespose.ServiceResponse.Message) for cindex, compMessage := range serviceRespose.ServiceResponse.ComponentsStatus { - message += fmt.Sprintf("\n Component:%d %s %s %s \n",cindex+1, compMessage.ComponentName, compMessage.ComponentAction, compMessage.ComponentStatus) + message += fmt.Sprintf("\n Component:%d %s %s %s \n", cindex+1, compMessage.ComponentName, compMessage.ComponentAction, compMessage.ComponentStatus) } } spinnerInstance.Prefix = fmt.Sprintf(" %s ", message) diff --git a/pkg/config/config.go b/pkg/config/config.go index de4a1ee8..1b8a7541 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -83,3 +83,48 @@ func SetProfile(profileName string) { log.Fatal("Unable to write configuration: ", err) } } + +// UpdateEnvName updates the EnvName in the configuration for the given profile +func UpdateEnvName(envName string) { + readConfigFile() + profile := viper.GetString("profile") + + // Retrieve the configuration for the specified profile + config, err := getConfigForProfile(profile) + if err != nil { + log.Fatal("Error while reading config: ", err) + } + + // Update the EnvName field + config.EnvName = envName + + // Write the updated configuration back to the file + viper.Set(profile, config) + if err := viper.WriteConfig(); err != nil { + log.Fatal("Unable to write configuration: ", err) + } + log.Infof("EnvName updated to [%s] successfully in profile [%s]", envName, profile) +} + +// GetActiveProfileEnvName returns the EnvName for the active profile +func GetActiveProfileEnvName() string { + readConfigFile() + profile := viper.GetString("profile") + config, err := getConfigForProfile(profile) + if err != nil { + log.Fatal("Error while reading config: ", err) + } + return config.EnvName +} + +// EnsureEnvPresent checks if default env is present in config else asks user for env via --env param +func EnsureEnvPresent(inputEnv string) string { + if inputEnv != "" { + return inputEnv + } + env := GetActiveProfileEnvName() + if env == "" { + log.Fatal("Please provide the environment name using --env, or set the default environment using `odin set environment `") + } + return env +}