Skip to content

Commit

Permalink
added set env cmd and use default env when running (#249)
Browse files Browse the repository at this point in the history
* added set env cmd and use default env when running

* changing environment to env
  • Loading branch information
yashjaind11 authored Aug 28, 2024
1 parent 6b705b2 commit d4c6410
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 29 deletions.
2 changes: 1 addition & 1 deletion cmd/create/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/delete/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 3 additions & 6 deletions cmd/deploy/service-set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.")
Expand Down
8 changes: 2 additions & 6 deletions cmd/deploy/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/list/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions cmd/operate/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -43,16 +44,15 @@ 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)
}
operateCmd.AddCommand(operateComponentCmd)
}

func execute(cmd *cobra.Command) {
env = config.EnsureEnvPresent(env)

ctx := cmd.Context()
//validate the variables
var optionsData map[string]interface{}
Expand Down
6 changes: 3 additions & 3 deletions cmd/operate/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -33,16 +34,15 @@ 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)
}
operateCmd.AddCommand(operateServiceCmd)
}

func executeOperateService(cmd *cobra.Command) {
env = config.EnsureEnvPresent(env)

ctx := cmd.Context()
//validate the variables
var optionsData map[string]interface{}
Expand Down
28 changes: 28 additions & 0 deletions cmd/set/env.go
Original file line number Diff line number Diff line change
@@ -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)
}
7 changes: 3 additions & 4 deletions cmd/undeploy/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down
8 changes: 4 additions & 4 deletions internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
45 changes: 45 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <env-name>`")
}
return env
}

0 comments on commit d4c6410

Please sign in to comment.