Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show diff on operate #259

Open
wants to merge 12 commits into
base: 2.x.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .staticcheck.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"checks": {
"ignore": [
"SA1019"
]
}
}
121 changes: 121 additions & 0 deletions cmd/operate/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ package operate

import (
"encoding/json"
"fmt"
"sort"
"strings"

"github.com/dream11/odin/internal/service"
"github.com/dream11/odin/internal/ui"
"github.com/dream11/odin/pkg/config"
"github.com/dream11/odin/pkg/table"
fileUtil "github.com/dream11/odin/pkg/util"
serviceProto "github.com/dream11/odin/proto/gen/go/dream11/od/service/v1"
"github.com/fatih/color"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"google.golang.org/protobuf/types/known/structpb"
Expand Down Expand Up @@ -82,6 +88,99 @@ func execute(cmd *cobra.Command) {
log.Fatal("error converting JSON to structpb.Struct: ", err)
}
//call operate component client

diffValues, err := componentClient.CompareOperationChanges(&ctx, &serviceProto.CompareOperationChangesRequest{
EnvName: env,
ServiceName: serviceName,
ComponentName: name,
Operation: operation,
Config: config,
})
if err != nil {
log.Fatal("Failed to compare operation changes\n", err)
}
oldComponentValues := diffValues.OldValues
newComponentValues := diffValues.NewValues

if oldComponentValues != nil && len(oldComponentValues.Fields) > 0 {
log.Info("\nBelow changes will happen after this operation:")
tableHeaders := []string{"Component Name", "Key", "Old Value", "New Value"}
var tableData [][]interface{}

componentName := name
flatendOldComponentValues := flattenMap(oldComponentValues.AsMap(), "")
flatendNewComponentValues := flattenMap(newComponentValues.AsMap(), "")

keys := make([]string, 0, len(flatendNewComponentValues))
for k := range flatendOldComponentValues {
keys = append(keys, k)
}
sort.Strings(keys)

for _, key := range keys {
oldValue := flatendOldComponentValues[key]
newValue := flatendNewComponentValues[key]
if fmt.Sprintf("%v", oldValue) != fmt.Sprintf("%v", newValue) {
var oldValueString string
var newValueString string

switch oldValue := oldValue.(type) {
case []interface{}:
strSlice := make([]string, len(oldValue))
for i, v := range oldValue {
strSlice[i] = fmt.Sprintf("%v", v)
}
oldValueString = color.RedString("[" + strings.Join(strSlice, ", ") + "]")
default:
oldValueString = color.RedString(fmt.Sprintf("%v", oldValue))
}

switch newValue := newValue.(type) {
case []interface{}:
strSlice := make([]string, len(newValue))
for i, v := range newValue {
strSlice[i] = fmt.Sprintf("%v", v)
}
newValueString = color.GreenString("[" + strings.Join(strSlice, ", ") + "]")
default:
newValueString = color.GreenString(fmt.Sprintf("%v", flatendNewComponentValues[key]))
}

tableData = append(tableData, []interface{}{
componentName,
key,
oldValueString,
newValueString,
})
}
}
table.Write(tableHeaders, tableData)
}

var message string
if oldComponentValues == nil || len(oldComponentValues.Fields) == 0 {
message = "\nNo changes from previous deployment. Do you want to continue? [Y/n]:"
} else {
message = "\nDo you want to proceed with the above command? [Y/n]:"
}
allowedInputsSlice := []string{"Y", "n"}
allowedInputs := make(map[string]struct{}, len(allowedInputsSlice))
for _, input := range allowedInputsSlice {
allowedInputs[input] = struct{}{}
}

inputHandler := ui.Input{}
val, err := inputHandler.AskWithConstraints(message, allowedInputs)

if err != nil {
log.Fatal(err.Error())
}

if val != "Y" {
log.Info("Aborting the operation")
return
}

err = componentClient.OperateComponent(&ctx, &serviceProto.OperateServiceRequest{
EnvName: env,
ServiceName: serviceName,
Expand All @@ -96,3 +195,25 @@ func execute(cmd *cobra.Command) {
}

}

func flattenMap(m map[string]interface{}, prefix string) map[string]interface{} {
flattened := make(map[string]interface{})
for k, v := range m {
key := prefix + k
if prefix != "" {
key = prefix + "." + k
}
if vm, ok := v.(map[string]interface{}); ok {
flattenedMap := flattenMap(vm, key)
if len(flattenedMap) == 0 {
flattened[key] = make(map[string]interface{})
}
for fk, fv := range flattenedMap {
flattened[fk] = fv
}
} else {
flattened[key] = v
}
}
return flattened
}
70 changes: 47 additions & 23 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,45 +1,69 @@
module github.com/dream11/odin

go 1.17
go 1.22.0

toolchain go1.22.3

require (
github.com/briandowns/spinner v1.23.0
github.com/briandowns/spinner v1.23.1
github.com/mitchellh/cli v1.1.5
github.com/olekukonko/tablewriter v0.0.5
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.1 // indirect
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/posener/complete v1.1.1 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sagikazarmark/locafero v0.6.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect
)

require (
github.com/fatih/color v1.17.0
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/iancoleman/orderedmap v0.3.0
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/term v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/term v0.25.0 // indirect
golang.org/x/text v0.19.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)

require (
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
golang.org/x/net v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230815205213-6bfd019c3878 // indirect
google.golang.org/grpc v1.57.0
google.golang.org/protobuf v1.33.0
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
golang.org/x/net v0.30.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241015192408-796eee8c2d53 // indirect
google.golang.org/grpc v1.67.1
google.golang.org/protobuf v1.35.1
)
Loading
Loading