Skip to content

Commit

Permalink
list and update command (#250)
Browse files Browse the repository at this point in the history
* list and update command

* list and update command

* list and update command

* list and update command

* list and update command

* removed blank implementation of operate
  • Loading branch information
nikhil-024 authored Sep 12, 2024
1 parent d4c6410 commit 199fe9b
Show file tree
Hide file tree
Showing 10 changed files with 364 additions and 127 deletions.
16 changes: 7 additions & 9 deletions cmd/list/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,13 @@ func writeOutput(response *environment.ListEnvironmentResponse, format string) {
}

func writeAsText(response *environment.ListEnvironmentResponse) {
tableHeaders := []string{"Name", "Created By", "State", "Account"}
tableHeaders := []string{"Name", "State", "Account"}
var tableData [][]interface{}
for _, env := range response.Environments {
tableData = append(tableData, []interface{}{
*env.Name,
*env.CreatedBy,
*env.Status,
*env.ProviderAccountName,
env.Name,
env.State,
env.Account,
})
}

Expand All @@ -91,10 +90,9 @@ func writeAsJSON(response *environment.ListEnvironmentResponse) {
var environments []map[string]interface{}
for _, env := range response.Environments {
environments = append(environments, map[string]interface{}{
"name": *env.Name,
"createdBy": *env.CreatedBy,
"status": *env.Status,
"account": *env.ProviderAccountName,
"name": env.Name,
"status": env.State,
"account": env.Account,
})
}
output, _ := json.MarshalIndent(environments, "", " ")
Expand Down
114 changes: 114 additions & 0 deletions cmd/update/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package update

import (
"encoding/json"
"fmt"

"github.com/dream11/odin/internal/service"
"github.com/dream11/odin/pkg/constant"
fileUtil "github.com/dream11/odin/pkg/util"
environment "github.com/dream11/odin/proto/gen/go/dream11/od/environment/v1"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"google.golang.org/protobuf/types/known/structpb"
)

var envName string
var data string
var file string
var updateEnvClient = service.Environment{}

var updateEnvCmd = &cobra.Command{
Use: "env",
Short: "update env",
Args: func(cmd *cobra.Command, args []string) error {
return cobra.NoArgs(cmd, args)
},
Long: `odin update env [Options]`,
Run: func(cmd *cobra.Command, args []string) {
executeupdate(cmd)
},
}

func init() {
updateEnvCmd.Flags().StringVar(&envName, "name", "", "name of the component")
updateEnvCmd.Flags().StringVar(&data, "data", "{}", "options of the operation in JSON format")
updateEnvCmd.Flags().StringVar(&file, "file", "", "path of the file which contains the options for the operation in JSON format")
if err := updateEnvCmd.MarkFlagRequired("name"); err != nil {
log.Fatal("Error marking 'name' flag as required:", err)
}

updateCmd.AddCommand(updateEnvCmd)
}

func executeupdate(cmd *cobra.Command) {

ctx := cmd.Context()
var optionsData map[string]interface{}

isOptionsPresent := data != "{}"
isFilePresent := len(file) > 0

if isOptionsPresent && isFilePresent {
log.Fatal("You can provide either --data or --file but not both")
}
if !isOptionsPresent && !isFilePresent {
log.Fatal("Either --data or --file is mandatory")
}

if isFilePresent {
parsedConfig, err := fileUtil.ParseFile(file)
if err != nil {
log.Fatal("Error while parsing file " + file + " : " + err.Error())
}
optionsData = parsedConfig.(map[string]interface{})
} else {
err := json.Unmarshal([]byte(data), &optionsData)
if err != nil {
log.Fatal("Unable to parse JSON data " + err.Error())
}
}

dataStruct, err := structpb.NewStruct(optionsData)
if err != nil {
log.Fatal("error converting JSON to structpb.Struct: ", err)
}
//call update env client
response, err := updateEnvClient.UpdateEnvironment(&ctx, &environment.UpdateEnvironmentRequest{
EnvName: envName,
Data: dataStruct,
})

if err != nil {
log.Fatal("Failed to update environment ", err.Error())
}

outputFormat, err := cmd.Flags().GetString("output")
if err != nil {
log.Fatal(err)
}
writeOutput(response, outputFormat)

}

func writeOutput(response *environment.UpdateEnvironmentResponse, format string) {

switch format {
case constant.TEXT:
writeAsText(response)
case constant.JSON:
writeAsJSON(response)
default:
log.Fatal("Unknown output format: ", format)
}
}

func writeAsText(response *environment.UpdateEnvironmentResponse) {
fmt.Print(string("Environment updated successfully"))
}

func writeAsJSON(response *environment.UpdateEnvironmentResponse) {
var environments []map[string]interface{}
output, _ := json.MarshalIndent(environments, "", " ")
fmt.Print(string(output))
}
20 changes: 20 additions & 0 deletions cmd/update/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package update

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

// updateCmd represents the update command
var updateCmd = &cobra.Command{
Use: "update",
Short: "This command is accessed by using one of the subcommands env.",
Long: `This command is accessed by using one of the subcommands below.
Subcommands:
env update an environment`,
}

func init() {
cmd.RootCmd.AddCommand(updateCmd)
}
16 changes: 16 additions & 0 deletions internal/service/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,19 @@ func (e *Environment) DeleteEnvironment(ctx *context.Context, request *environme
log.Info(message)
return err
}

func (e *Environment) UpdateEnvironment(ctx *context.Context, request *environment.UpdateEnvironmentRequest) (*environment.UpdateEnvironmentResponse, error) {
conn, requestCtx, err := grpcClient(ctx)
if err != nil {
return nil, err
}

client := environment.NewEnvironmentServiceClient(conn)
response, err := client.UpdateEnvironment(*requestCtx, request)

if err != nil {
return nil, err
}

return response, nil
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
_ "github.com/dream11/odin/cmd/operate"
_ "github.com/dream11/odin/cmd/set"
_ "github.com/dream11/odin/cmd/undeploy"
_ "github.com/dream11/odin/cmd/update"
_ "github.com/dream11/odin/internal/ui"
)

Expand Down
Binary file added odin
Binary file not shown.
6 changes: 6 additions & 0 deletions proto/dream11/od/dto/v1/environment.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ message AccountInformation {
google.protobuf.Struct service_accounts_snapshot = 2;
string status = 3;
}

message EnvironmentSummary {
string name = 1;
string account = 2;
string state = 3;
}
2 changes: 1 addition & 1 deletion proto/dream11/od/environment/v1/environment.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ message ListEnvironmentRequest {
}

message ListEnvironmentResponse {
repeated dream11.od.dto.v1.Environment environments = 1;
repeated dream11.od.dto.v1.EnvironmentSummary environments = 1;
}

message DescribeEnvironmentRequest {
Expand Down
113 changes: 97 additions & 16 deletions 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.

Loading

0 comments on commit 199fe9b

Please sign in to comment.