diff --git a/cmd/idpscimcli/cmd/common.go b/cmd/idpscimcli/cmd/common.go index f0d0b9f..d1a14f4 100644 --- a/cmd/idpscimcli/cmd/common.go +++ b/cmd/idpscimcli/cmd/common.go @@ -1,19 +1,23 @@ package cmd import ( + "encoding/json" "fmt" - "github.com/slashdevops/idp-scim-sync/internal/convert" + "gopkg.in/yaml.v3" ) // show resource structure as outFormat func show(outFormat string, resource interface{}) { switch outFormat { case "json": - fmt.Print(convert.ToJSONString(resource, true)) + j, _ := json.MarshalIndent(resource, "", " ") + fmt.Print(string(j)) case "yaml": - fmt.Print(convert.ToYAML(resource)) + y, _ := yaml.Marshal(resource) + fmt.Print(string(y)) default: - fmt.Print(convert.ToJSONString(resource, true)) + j, _ := json.MarshalIndent(resource, "", " ") + fmt.Print(string(j)) } } diff --git a/go.mod b/go.mod index 17145df..8daf387 100644 --- a/go.mod +++ b/go.mod @@ -41,7 +41,7 @@ require ( github.com/aws/smithy-go v1.22.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/felixge/httpsnoop v1.0.4 // indirect - github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/fsnotify/fsnotify v1.8.0 // indirect github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect diff --git a/go.sum b/go.sum index 89f5778..3ddbb75 100644 --- a/go.sum +++ b/go.sum @@ -64,8 +64,8 @@ github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2 github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= -github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= -github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= +github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M= +github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= diff --git a/internal/config/config.go b/internal/config/config.go index 07d4375..f90a11b 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,7 +1,7 @@ package config const ( - // DefaultIsLambda is the progam execute as a lambda function? + // DefaultIsLambda is the program execute as a lambda function? DefaultIsLambda = false // DefaultLogLevel is the default logging level. diff --git a/internal/convert/convert.go b/internal/convert/convert.go deleted file mode 100644 index a10fcc9..0000000 --- a/internal/convert/convert.go +++ /dev/null @@ -1,58 +0,0 @@ -package convert - -import ( - "encoding/json" - "log/slog" - "os" - - "gopkg.in/yaml.v3" -) - -// ToJSON converts any type to JSON []byte -func ToJSON(stc interface{}, ident ...bool) []byte { - if stc == nil { - return []byte("") - } - if stc == "" { - return []byte("") - } - - var JSON []byte - var err error - if len(ident) > 0 && ident[0] { - JSON, err = json.MarshalIndent(stc, "", " ") - if err != nil { - slog.Error("error marshalling to JSON", "error", err) - os.Exit(1) - } - } else { - JSON, err = json.Marshal(stc) - if err != nil { - slog.Error("error marshalling to JSON", "error", err) - os.Exit(1) - } - } - return JSON -} - -// ToJSONString converts any type to JSON string -func ToJSONString(stc interface{}, ident ...bool) string { - return string(ToJSON(stc, ident...)) -} - -// ToYAML converts any type to YAML []byte -func ToYAML(stc interface{}) []byte { - if stc == nil { - return []byte("") - } - if stc == "" { - return []byte("") - } - - YAML, err := yaml.Marshal(stc) - if err != nil { - slog.Error("error marshalling to YAML", "error", err) - os.Exit(1) - } - return YAML -} diff --git a/internal/convert/convert_test.go b/internal/convert/convert_test.go deleted file mode 100644 index e5cf142..0000000 --- a/internal/convert/convert_test.go +++ /dev/null @@ -1,201 +0,0 @@ -package convert - -import ( - "reflect" - "testing" -) - -func TestToJSON(t *testing.T) { - type args struct { - stc interface{} - } - tests := []struct { - name string - args args - want []byte - }{ - { - name: "nil", - args: args{ - stc: nil, - }, - want: []byte(""), - }, - { - name: "empty", - args: args{ - stc: "", - }, - want: []byte(""), - }, - { - name: "string", - args: args{ - stc: "test", - }, - want: []byte("\"test\""), - }, - { - name: "int", - args: args{ - stc: 1, - }, - want: []byte("1"), - }, - { - name: "int64", - args: args{ - stc: int64(1), - }, - want: []byte("1"), - }, - { - name: "struct", - args: args{ - stc: struct { - Name string `json:"name"` - Age int `json:"age"` - }{ - Name: "test", - Age: 1, - }, - }, - want: []byte("{\"name\":\"test\",\"age\":1}"), - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := ToJSON(tt.args.stc); !reflect.DeepEqual(got, tt.want) { - t.Errorf("ToJSON() = %v, want %v", got, tt.want) - } - }) - } -} - -func TestToJSONString(t *testing.T) { - type args struct { - stc interface{} - ident []bool - } - tests := []struct { - name string - args args - want string - }{ - { - name: "nil", - args: args{ - stc: nil, - }, - want: "", - }, - { - name: "empty", - args: args{ - stc: "", - }, - want: "", - }, - { - name: "string", - args: args{ - stc: "test", - }, - want: "\"test\"", - }, - { - name: "int", - args: args{ - stc: 1, - }, - want: "1", - }, - { - name: "struct", - args: args{ - stc: struct { - Name string `json:"name"` - Age int `json:"age"` - }{ - Name: "test", - Age: 1, - }, - }, - want: `{"name":"test","age":1}`, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := ToJSONString(tt.args.stc, tt.args.ident...); got != tt.want { - t.Errorf("ToJSONString() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestToYAML(t *testing.T) { - type args struct { - stc interface{} - } - tests := []struct { - name string - args args - want []byte - }{ - { - name: "nil", - args: args{ - stc: nil, - }, - want: []byte(""), - }, - { - name: "empty", - args: args{ - stc: "", - }, - want: []byte(""), - }, - { - name: "string", - args: args{ - stc: "test", - }, - want: []byte("test\n"), - }, - { - name: "int", - args: args{ - stc: 1, - }, - want: []byte("1\n"), - }, - { - name: "int64", - args: args{ - stc: int64(1), - }, - want: []byte("1\n"), - }, - { - name: "struct", - args: args{ - stc: struct { - Name string `json:"name"` - Age int `json:"age"` - }{ - Name: "test", - Age: 1, - }, - }, - want: []byte("name: test\nage: 1\n"), - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := ToYAML(tt.args.stc); !reflect.DeepEqual(got, tt.want) { - t.Errorf("ToYAML() = %v, want %v", got, tt.want) - } - }) - } -} diff --git a/internal/core/options_test.go b/internal/core/options_test.go index 1a3fa9b..81e94a0 100644 --- a/internal/core/options_test.go +++ b/internal/core/options_test.go @@ -5,7 +5,6 @@ import ( "testing" gomock "github.com/golang/mock/gomock" - "github.com/slashdevops/idp-scim-sync/internal/convert" mocks "github.com/slashdevops/idp-scim-sync/mocks/core" ) @@ -53,7 +52,7 @@ func TestWithIdentityProviderGroupsFilter(t *testing.T) { } if !reflect.DeepEqual(got, want) { - t.Errorf("NewSyncService() got = %s, want %s", convert.ToJSONString(got, true), convert.ToJSONString(want, true)) + t.Errorf("NewSyncService() got = %s, want %s", got, want) } }) } @@ -102,7 +101,7 @@ func TestWithIdentityProviderUsersFilter(t *testing.T) { } if !reflect.DeepEqual(got, want) { - t.Errorf("NewSyncService() got = %s, want %s", convert.ToJSONString(got, true), convert.ToJSONString(want, true)) + t.Errorf("NewSyncService() got = %s, want %s", got, want) } }) } diff --git a/internal/idp/helpers.go b/internal/idp/helpers.go index 6396c24..ddbb283 100644 --- a/internal/idp/helpers.go +++ b/internal/idp/helpers.go @@ -5,7 +5,6 @@ import ( "log/slog" "strings" - "github.com/slashdevops/idp-scim-sync/internal/convert" "github.com/slashdevops/idp-scim-sync/internal/model" admin "google.golang.org/api/admin/directory/v1" ) @@ -232,7 +231,7 @@ func buildUser(usr *admin.User) *model.User { WithEnterpriseData(mainOrganization). Build() - slog.Debug("idp: buildUser() converted user", "from", convert.ToJSONString(usr), "to", convert.ToJSONString(userModel)) + slog.Debug("idp: buildUser() converted user", "from", usr, "to", userModel) return userModel } diff --git a/internal/model/group_test.go b/internal/model/group_test.go index 94515d0..135611d 100644 --- a/internal/model/group_test.go +++ b/internal/model/group_test.go @@ -8,7 +8,6 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" - "github.com/slashdevops/idp-scim-sync/internal/convert" ) func TestGroup_SetHashCode(t *testing.T) { @@ -238,12 +237,6 @@ func TestGroupsResult_SetHashCode(t *testing.T) { gr5 := MergeGroupsResult(&gr3, &gr2, &gr1) gr5.SetHashCode() - t.Logf("gr4: %s\n", convert.ToJSON(gr4, true)) - t.Logf("gr5: %s\n", convert.ToJSON(gr5, true)) - - t.Logf("gr4.HashCode: %s\n", gr4.HashCode) - t.Logf("gr5.HashCode: %s\n", gr5.HashCode) - if gr1.HashCode != gr2.HashCode { t.Errorf("GroupsResult.HashCode should be equal") } diff --git a/internal/model/member_test.go b/internal/model/member_test.go index 23531d7..b8ef673 100644 --- a/internal/model/member_test.go +++ b/internal/model/member_test.go @@ -7,7 +7,6 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" - "github.com/slashdevops/idp-scim-sync/internal/convert" ) func TestMember_GobEncode(t *testing.T) { @@ -201,14 +200,6 @@ func TestMembersResult_SetHashCode(t *testing.T) { } mr3.SetHashCode() - t.Logf("mr1: %s\n", convert.ToJSONString(mr1, true)) - t.Logf("mr2: %s\n", convert.ToJSONString(mr2, true)) - t.Logf("mr3: %s\n", convert.ToJSONString(mr3, true)) - - t.Logf("mr1.HashCode: %s\n", mr1.HashCode) - t.Logf("mr2.HashCode: %s\n", mr2.HashCode) - t.Logf("mr3.HashCode: %s\n", mr3.HashCode) - if mr1.HashCode != mr2.HashCode { t.Errorf("GroupsMembersResult.HashCode should be equal") } @@ -377,9 +368,6 @@ func TestGroupsMembersResult_SetHashCode(t *testing.T) { gmr5 := MergeGroupsMembersResult(&gmr3, &gmr2, &gmr1) gmr5.SetHashCode() - t.Logf("gmr4: %s\n", convert.ToJSONString(gmr4, true)) - t.Logf("gmr5: %s\n", convert.ToJSONString(gmr5, true)) - t.Logf("gmr4.HashCode: %s\n", gmr4.HashCode) t.Logf("gmr5.HashCode: %s\n", gmr5.HashCode) diff --git a/internal/model/operations.go b/internal/model/operations.go index 85f0121..2a4bf21 100644 --- a/internal/model/operations.go +++ b/internal/model/operations.go @@ -3,8 +3,6 @@ package model import ( "errors" "log/slog" - - "github.com/slashdevops/idp-scim-sync/internal/convert" ) var ( @@ -136,8 +134,8 @@ func UsersOperations(idp, scim *UsersResult) (create, update, equal, remove *Use return } - slog.Debug("idp UsersResult", "idp", convert.ToJSONString(idp)) - slog.Debug("scim UsersResult", "scim", convert.ToJSONString(scim)) + slog.Debug("idp UsersResult", "idp", idp) + slog.Debug("scim UsersResult", "scim", scim) idpUsers := make(map[string]struct{}) scimUsers := make(map[string]User) diff --git a/internal/model/user_test.go b/internal/model/user_test.go index b07c9bb..a8f1295 100644 --- a/internal/model/user_test.go +++ b/internal/model/user_test.go @@ -8,7 +8,6 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" - "github.com/slashdevops/idp-scim-sync/internal/convert" ) func TestName_GobEncode(t *testing.T) { @@ -874,9 +873,6 @@ func TestUsersResult_SetHashCode(t *testing.T) { ur5 := MergeUsersResult(&ur3, &ur2, &ur1) ur5.SetHashCode() - t.Logf("ur4: %s\n", convert.ToJSONString(ur4, true)) - t.Logf("ur5: %s\n", convert.ToJSONString(ur5, true)) - t.Logf("ur4.HashCode: %s\n", ur4.HashCode) t.Logf("ur5.HashCode: %s\n", ur5.HashCode) diff --git a/internal/scim/helpers.go b/internal/scim/helpers.go index 45edc48..8dad9ac 100644 --- a/internal/scim/helpers.go +++ b/internal/scim/helpers.go @@ -4,7 +4,6 @@ import ( "log/slog" "strings" - "github.com/slashdevops/idp-scim-sync/internal/convert" "github.com/slashdevops/idp-scim-sync/internal/model" "github.com/slashdevops/idp-scim-sync/pkg/aws" ) @@ -137,7 +136,7 @@ func buildUser(user *aws.User) *model.User { WithEnterpriseData(enterpriseData). Build() - slog.Debug("scim: buildUser() converted user", "from", convert.ToJSONString(user), "to", convert.ToJSONString(userModel)) + slog.Debug("scim: buildUser() converted user", "from", user, "to", userModel) return userModel } diff --git a/pkg/aws/scim.go b/pkg/aws/scim.go index e231bfd..309d9ec 100644 --- a/pkg/aws/scim.go +++ b/pkg/aws/scim.go @@ -195,7 +195,7 @@ func (s *SCIMService) CreateUser(ctx context.Context, cur *CreateUserRequest) (* return nil, fmt.Errorf("aws CreateUser: user: %s, error decoding response body: %w", cur.UserName, err) } - slog.Debug("aws CreateUser()", "response", toJSONString(response)) + slog.Debug("aws CreateUser()", "response", response) return &response, nil } diff --git a/pkg/aws/utils.go b/pkg/aws/utils.go deleted file mode 100644 index 1bb592d..0000000 --- a/pkg/aws/utils.go +++ /dev/null @@ -1,39 +0,0 @@ -package aws - -import ( - "encoding/json" - "log/slog" - "os" -) - -// toJSON converts any type to JSON []byte -func toJSON(stc interface{}, ident ...bool) []byte { - if stc == nil { - return []byte("") - } - if stc == "" { - return []byte("") - } - - var JSON []byte - var err error - if len(ident) > 0 && ident[0] { - JSON, err = json.MarshalIndent(stc, "", " ") - if err != nil { - slog.Error(err.Error()) - os.Exit(1) - } - } else { - JSON, err = json.Marshal(stc) - if err != nil { - slog.Error(err.Error()) - os.Exit(1) - } - } - return JSON -} - -// ToJSONString converts any type to JSON string -func toJSONString(stc interface{}, ident ...bool) string { - return string(toJSON(stc, ident...)) -} diff --git a/pkg/aws/utils_test.go b/pkg/aws/utils_test.go deleted file mode 100644 index 23dd0a1..0000000 --- a/pkg/aws/utils_test.go +++ /dev/null @@ -1,135 +0,0 @@ -package aws - -import ( - "reflect" - "testing" -) - -func Test_toJSON(t *testing.T) { - type args struct { - stc interface{} - ident []bool - } - tests := []struct { - name string - args args - want []byte - }{ - { - name: "nil", - args: args{ - stc: nil, - }, - want: []byte(""), - }, - { - name: "empty", - args: args{ - stc: "", - }, - want: []byte(""), - }, - { - name: "string", - args: args{ - stc: "test", - }, - want: []byte("\"test\""), - }, - { - name: "int", - args: args{ - stc: 1, - }, - want: []byte("1"), - }, - { - name: "int64", - args: args{ - stc: int64(1), - }, - want: []byte("1"), - }, - { - name: "struct", - args: args{ - stc: struct { - Name string `json:"name"` - Age int `json:"age"` - }{ - Name: "test", - Age: 1, - }, - }, - want: []byte("{\"name\":\"test\",\"age\":1}"), - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := toJSON(tt.args.stc, tt.args.ident...); !reflect.DeepEqual(got, tt.want) { - t.Errorf("toJSON() = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_toJSONString(t *testing.T) { - type args struct { - stc interface{} - ident []bool - } - tests := []struct { - name string - args args - want string - }{ - { - name: "nil", - args: args{ - stc: nil, - }, - want: "", - }, - { - name: "empty", - args: args{ - stc: "", - }, - want: "", - }, - { - name: "string", - args: args{ - stc: "test", - }, - want: "\"test\"", - }, - { - name: "int", - args: args{ - stc: 1, - }, - want: "1", - }, - { - name: "struct", - args: args{ - stc: struct { - Name string `json:"name"` - Age int `json:"age"` - }{ - Name: "test", - Age: 1, - }, - }, - want: `{"name":"test","age":1}`, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := toJSONString(tt.args.stc, tt.args.ident...); got != tt.want { - t.Errorf("toJSONString() got = %v, want %v", got, tt.want) - } - }) - } -}