Skip to content

Commit

Permalink
onboard delete env command
Browse files Browse the repository at this point in the history
  • Loading branch information
boppanasusanth committed Apr 23, 2024
1 parent fe05d48 commit 156ecb4
Show file tree
Hide file tree
Showing 18 changed files with 374 additions and 77 deletions.
10 changes: 5 additions & 5 deletions api/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ type SecretKeys struct {

// Configuration interface
type Configuration struct {
BackendAddress string `toml:"backend_address,omitempty" mapstructure:"backend_addr,omitempty"`
Keys SecretKeys
AccessToken string `toml:"access_token,omitempty" mapstructure:"access_token,omitempty"`
EnvName string `toml:"envName,omitempty" mapstructure:"envName,omitempty"`
Insecure bool `toml:"insecure,omitempty" mapstructure:"insecure,omitempty"`
BackendAddress string `toml:"backend_address,omitempty" mapstructure:"backend_addr,omitempty"`
Keys SecretKeys
AccessToken string `toml:"access_token,omitempty" mapstructure:"access_token,omitempty"`
EnvName string `toml:"envName,omitempty" mapstructure:"envName,omitempty"`
Insecure bool `toml:"insecure,omitempty" mapstructure:"insecure,omitempty"`
}
8 changes: 4 additions & 4 deletions cmd/configure/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func execute(cmd *cobra.Command) {
}

func createConfigFileIfNotExist() {
dirPath := path.Join(os.Getenv("HOME"), "." + app.App.Name)
dirPath := path.Join(os.Getenv("HOME"), "."+app.App.Name)
if err := dir.CreateDirIfNotExist(dirPath); err != nil {
log.Fatalf("Error creating the .%s folder: %v", app.App.Name, err)
}
Expand All @@ -78,7 +78,7 @@ func createConfigFileIfNotExist() {
}
}

func getConfigKey(flagKey string, flagValue string, envVariableName string, configValue string, defaultValue string) (string) {
func getConfigKey(flagKey string, flagValue string, envVariableName string, configValue string, defaultValue string) string {
if flagValue != "" {
return flagValue
} else if os.Getenv(envVariableName) != "" {
Expand All @@ -87,12 +87,12 @@ func getConfigKey(flagKey string, flagValue string, envVariableName string, conf
return configValue
} else if defaultValue != "" {
return defaultValue
}
}
log.Fatalf("Please pass %s flag nor set environment variable %s to configure", flagKey, envVariableName)
return ""
}

func hashKey(key string) (string) {
func hashKey(key string) string {
hash := sha256.New()
hash.Write([]byte(key))
hashedResult := hash.Sum(nil)
Expand Down
16 changes: 16 additions & 0 deletions cmd/delete/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package delete

import (
"github.com/dream11/odin/cmd"
"github.com/spf13/cobra"
)

var deleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete resources",
Long: `Delete resources`,
}

func init() {
cmd.RootCmd.AddCommand(deleteCmd)
}
41 changes: 41 additions & 0 deletions cmd/delete/environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package delete

import (
"github.com/dream11/odin/internal/service"
environment "github.com/dream11/odin/proto/gen/go/dream11/od/environment/v1"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var name string

var environmentClient = service.Environment{}

var environmentCmd = &cobra.Command{
Use: "environment",
Short: "Delete environment",
Long: `Delete environment`,
Args: func(cmd *cobra.Command, args []string) error {
return cobra.NoArgs(cmd, args)
},
Run: func(cmd *cobra.Command, args []string) {
execute(cmd)
},
}

func init() {
environmentCmd.Flags().StringVar(&name, "name", "", "name of the env")
deleteCmd.AddCommand(environmentCmd)
}

func execute(cmd *cobra.Command) {
ctx := cmd.Context()
log.Info("Deleting environment...")
err := environmentClient.DeleteEnvironment(&ctx, &environment.DeleteEnvironmentRequest{
EnvName: name,
})

if err != nil {
log.Fatal("Failed to delete environment ", err)
}
}
3 changes: 1 addition & 2 deletions cmd/list/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package list
import (
"encoding/json"
"fmt"
"github.com/dream11/odin/pkg/constant"
"strconv"

"github.com/dream11/odin/internal/service"
"github.com/dream11/odin/pkg/constant"
"github.com/dream11/odin/pkg/table"
environment "github.com/dream11/odin/proto/gen/go/dream11/od/environment/v1"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -100,4 +100,3 @@ func writeAsJSON(response *environment.ListEnvironmentResponse) {
output, _ := json.MarshalIndent(environments, "", " ")
fmt.Print(string(output))
}

30 changes: 30 additions & 0 deletions internal/service/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

environment "github.com/dream11/odin/proto/gen/go/dream11/od/environment/v1"
log "github.com/sirupsen/logrus"
)

// Environment performs operation on environment like create, list, describe, delete
Expand All @@ -23,3 +24,32 @@ func (e *Environment) ListEnvironments(ctx *context.Context, request *environmen

return response, nil
}

// DeleteEnvironment : Delete environment
func (e *Environment) DeleteEnvironment(ctx *context.Context, request *environment.DeleteEnvironmentRequest) error {
conn, requestCtx, err := grpcClient(ctx)
if err != nil {
return err
}

client := environment.NewEnvironmentServiceClient(conn)
stream, err := client.DeleteEnvironment(*requestCtx, request)

if err != nil {
return err
}

for {
response, err := stream.Recv()
if err != nil {
if err == context.Canceled {
break
}
log.Fatalf("Error receiving stream: %v", err)
}

log.Info(response)
}

return err
}
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ package main

import (
"github.com/dream11/odin/cmd"
_ "github.com/dream11/odin/cmd/list"
_ "github.com/dream11/odin/cmd/configure"
_ "github.com/dream11/odin/cmd/delete"
_ "github.com/dream11/odin/cmd/list"
_ "github.com/dream11/odin/internal/ui"
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/dir/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func Exists(path string) (bool, error) {
}

// CreateDirIfNotExist : create directory if it doesn't exist
func CreateDirIfNotExist(path string) (error) {
func CreateDirIfNotExist(path string) error {
dirExists, err := Exists(path)
if err != nil {
return err
Expand All @@ -77,7 +77,7 @@ func CreateDirIfNotExist(path string) (error) {
}

// CreateFileIfNotExist : create file if it doesn't exist
func CreateFileIfNotExist(path string) (error) {
func CreateFileIfNotExist(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
file, err := os.Create(path)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions proto/dream11/od/environment/v1/environment.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ service EnvironmentService {
rpc DescribeEnvironment(DescribeEnvironmentRequest) returns (DescribeEnvironmentResponse) {}
rpc UpdateEnvironment(UpdateEnvironmentRequest) returns (UpdateEnvironmentResponse) {}
rpc CreateEnvironment(CreateEnvironmentRequest) returns (stream CreateEnvironmentResponse) {}
rpc DeleteEnvironment(DeleteEnvironmentRequest) returns (stream DeleteEnvironmentResponse) {}
}

message ListEnvironmentRequest {
Expand Down Expand Up @@ -46,3 +47,11 @@ message CreateEnvironmentRequest {
message CreateEnvironmentResponse {
string message = 1;
}

message DeleteEnvironmentRequest {
string env_name = 1;
}

message DeleteEnvironmentResponse {
string message = 1;
}
2 changes: 1 addition & 1 deletion proto/gen/go/dream11/od/auth/v1/auth.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion proto/gen/go/dream11/od/auth/v1/auth_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion proto/gen/go/dream11/od/dto/v1/component.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion proto/gen/go/dream11/od/dto/v1/component_task.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion proto/gen/go/dream11/od/dto/v1/environment.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion proto/gen/go/dream11/od/dto/v1/provisioning_type.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion proto/gen/go/dream11/od/dto/v1/service_task.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 156ecb4

Please sign in to comment.