From fde73f7a0f6c3e574d5cb8424ff7ba852c33e85c Mon Sep 17 00:00:00 2001 From: Tolga Ozen Date: Tue, 13 Jun 2023 14:47:16 +0300 Subject: [PATCH 1/3] refactor: unnecessary coverage struct removed --- internal/config/config.go | 11 ----------- pkg/cmd/flags/coverage.go | 6 ++---- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 030ec0025..bacdd168b 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -22,7 +22,6 @@ type ( Service `mapstructure:"service"` // Service configuration Database `mapstructure:"database"` // Database configuration Distributed `mapstructure:"distributed"` // Distributed configuration - Coverage `mapstructure:"coverage"` // Coverage configuration } // Server contains the configurations for both HTTP and gRPC servers. @@ -157,12 +156,6 @@ type ( Enabled bool `mapstructure:"enabled"` Nodes []string `mapstructure:"nodes"` } - - // Coverage contains configuration for coverage. - Coverage struct { - Relationships int `mapstructure:"relationships"` // Relationships trashhold - Assertions int `mapstructure:"assertions"` // Assertions trashhold - } ) // NewConfig initializes and returns a new Config object by reading and unmarshalling @@ -313,10 +306,6 @@ func DefaultConfig() *Config { Enabled: false, Nodes: []string{}, }, - Coverage: Coverage{ - Relationships: 0, - Assertions: 0, - }, } } diff --git a/pkg/cmd/flags/coverage.go b/pkg/cmd/flags/coverage.go index 35b688472..e654f6acf 100644 --- a/pkg/cmd/flags/coverage.go +++ b/pkg/cmd/flags/coverage.go @@ -1,22 +1,20 @@ package flags import ( - "github.com/Permify/permify/internal/config" "github.com/spf13/cobra" "github.com/spf13/viper" ) // RegisterCoverageFlags registers coverage flags. func RegisterCoverageFlags(cmd *cobra.Command) { - conf := config.DefaultConfig() flags := cmd.Flags() - flags.Int("coverage-relationships", conf.Coverage.Relationships, "the min coverage for relationships") + flags.Int("coverage-relationships", 0, "the min coverage for relationships") if err := viper.BindPFlag("coverage-relationships", flags.Lookup("coverage-relationships")); err != nil { panic(err) } - flags.Int("coverage-assertions", conf.Coverage.Assertions, "the min coverage for assertions") + flags.Int("coverage-assertions", 0, "the min coverage for assertions") if err := viper.BindPFlag("coverage-assertions", flags.Lookup("coverage-assertions")); err != nil { panic(err) } From 66e0774d572066c247b4492ff059b80a593f2938 Mon Sep 17 00:00:00 2001 From: Tolga Ozen Date: Tue, 13 Jun 2023 18:35:20 +0300 Subject: [PATCH 2/3] fix: wrong flag registerer --- cmd/permify/permify.go | 1 - pkg/cmd/coverage.go | 2 +- pkg/cmd/flags/validation.go | 1 + pkg/cmd/validate.go | 16 ++++++++++++---- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/cmd/permify/permify.go b/cmd/permify/permify.go index fc0360871..85604f1c4 100644 --- a/cmd/permify/permify.go +++ b/cmd/permify/permify.go @@ -18,7 +18,6 @@ func main() { root.AddCommand(validate) coverage := cmd.NewCoverageCommand() - flags.RegisterCoverageFlags(coverage) root.AddCommand(coverage) migrate := cmd.NewMigrateCommand() diff --git a/pkg/cmd/coverage.go b/pkg/cmd/coverage.go index 710b4632d..bf6bff8a2 100644 --- a/pkg/cmd/coverage.go +++ b/pkg/cmd/coverage.go @@ -24,7 +24,7 @@ func NewCoverageCommand() *cobra.Command { } // register flags for validation - flags.RegisterValidationFlags(command) + flags.RegisterCoverageFlags(command) return command } diff --git a/pkg/cmd/flags/validation.go b/pkg/cmd/flags/validation.go index 2bf014da6..9a42f9ec2 100644 --- a/pkg/cmd/flags/validation.go +++ b/pkg/cmd/flags/validation.go @@ -8,6 +8,7 @@ import ( // RegisterValidationFlags registers validation flags. func RegisterValidationFlags(cmd *cobra.Command) { flags := cmd.Flags() + flags.String("output-format", "", "output format. one of: verbose, json") if err := viper.BindPFlag("output-format", flags.Lookup("output-format")); err != nil { panic(err) diff --git a/pkg/cmd/validate.go b/pkg/cmd/validate.go index df1c8dd44..f1d8bfb54 100644 --- a/pkg/cmd/validate.go +++ b/pkg/cmd/validate.go @@ -212,8 +212,10 @@ func validate() func(cmd *cobra.Command, args []string) error { // Check Assertions for sn, scenario := range s.Scenarios { - color.Notice.Printf("%v.scenario: %s - %s\n", sn+1, scenario.Name, scenario.Description) - color.Notice.Println(" checks:") + if debug { + color.Notice.Printf("%v.scenario: %s - %s\n", sn+1, scenario.Name, scenario.Description) + color.Notice.Println(" checks:") + } for _, check := range scenario.Checks { entity, err := tuple.E(check.Entity) @@ -299,7 +301,9 @@ func validate() func(cmd *cobra.Command, args []string) error { } } - color.Notice.Println(" entity_filters:") + if debug { + color.Notice.Println(" entity_filters:") + } for _, filter := range scenario.EntityFilters { @@ -365,7 +369,9 @@ func validate() func(cmd *cobra.Command, args []string) error { } } - color.Notice.Println(" subject_filters:") + if debug { + color.Notice.Println(" subject_filters:") + } for _, filter := range scenario.SubjectFilters { @@ -440,6 +446,8 @@ func validate() func(cmd *cobra.Command, args []string) error { } fmt.Println(string(b)) os.Exit(1) + } else { + fmt.Println("{}") } if debug { From fc6cc01d0ebe3b28d67f912d55df6b4665312382 Mon Sep 17 00:00:00 2001 From: Tolga Ozen Date: Tue, 13 Jun 2023 22:02:28 +0300 Subject: [PATCH 3/3] chore: validation output format --- pkg/cmd/flags/validation.go | 16 ----- pkg/cmd/validate.go | 126 ++++++++---------------------------- 2 files changed, 28 insertions(+), 114 deletions(-) delete mode 100644 pkg/cmd/flags/validation.go diff --git a/pkg/cmd/flags/validation.go b/pkg/cmd/flags/validation.go deleted file mode 100644 index 9a42f9ec2..000000000 --- a/pkg/cmd/flags/validation.go +++ /dev/null @@ -1,16 +0,0 @@ -package flags - -import ( - "github.com/spf13/cobra" - "github.com/spf13/viper" -) - -// RegisterValidationFlags registers validation flags. -func RegisterValidationFlags(cmd *cobra.Command) { - flags := cmd.Flags() - - flags.String("output-format", "", "output format. one of: verbose, json") - if err := viper.BindPFlag("output-format", flags.Lookup("output-format")); err != nil { - panic(err) - } -} diff --git a/pkg/cmd/validate.go b/pkg/cmd/validate.go index f1d8bfb54..706a80a5a 100644 --- a/pkg/cmd/validate.go +++ b/pkg/cmd/validate.go @@ -2,22 +2,18 @@ package cmd import ( "context" - "encoding/json" "fmt" "net/url" "os" "sort" "strings" - "github.com/rs/xid" - "github.com/spf13/viper" - "github.com/gookit/color" + "github.com/rs/xid" "github.com/spf13/cobra" "github.com/Permify/permify/internal/storage" server_validation "github.com/Permify/permify/internal/validation" - "github.com/Permify/permify/pkg/cmd/flags" "github.com/Permify/permify/pkg/database" "github.com/Permify/permify/pkg/development" "github.com/Permify/permify/pkg/development/file" @@ -38,9 +34,6 @@ func NewValidateCommand() *cobra.Command { Args: cobra.ExactArgs(1), } - // register flags for validation - flags.RegisterValidationFlags(command) - return command } @@ -68,22 +61,6 @@ func (l *ErrList) Print() { // validate returns a function that validates authorization model with assertions func validate() func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) error { - // set debug to false initially - debug := false - - // get output format from viper - format := viper.GetString("output-format") - - // if output format is not verbose or json, set it to verbose - if format != "verbose" && format != "json" { - format = "verbose" - } - - // if output format is verbose, set debug to true - if format == "verbose" { - debug = true - } - // create an empty error list list := &ErrList{ Errors: []string{}, @@ -117,9 +94,7 @@ func validate() func(cmd *cobra.Command, args []string) error { } // if debug is true, print schema is creating with color blue - if debug { - color.Notice.Println("schema is creating... 🚀") - } + color.Notice.Println("schema is creating... 🚀") sch, err := parser.NewParser(s.Schema).Parse() if err != nil { @@ -147,9 +122,7 @@ func validate() func(cmd *cobra.Command, args []string) error { err = dev.Container.SW.WriteSchema(ctx, cnf) if err != nil { list.Add(err.Error()) - if debug { - color.Danger.Printf("fail: %s\n", validationError(err.Error())) - } + color.Danger.Printf("fail: %s\n", validationError(err.Error())) if len(list.Errors) != 0 { list.Print() os.Exit(1) @@ -157,14 +130,12 @@ func validate() func(cmd *cobra.Command, args []string) error { } // if there are no errors and debug is true, print success with color success - if len(list.Errors) == 0 && debug { + if len(list.Errors) == 0 { color.Success.Println(" success") } // if debug is true, print relationships are creating with color blue - if debug { - color.Notice.Println("relationships are creating... 🚀") - } + color.Notice.Println("relationships are creating... 🚀") // write relationships for _, t := range s.Relationships { @@ -194,28 +165,20 @@ func validate() func(cmd *cobra.Command, args []string) error { })) if err != nil { list.Add(fmt.Sprintf("%s failed %s", t, err.Error())) - if debug { - color.Danger.Println(fmt.Sprintf("fail: %s failed %s", t, validationError(err.Error()))) - } + color.Danger.Println(fmt.Sprintf("fail: %s failed %s", t, validationError(err.Error()))) continue } - if debug { - color.Success.Println(fmt.Sprintf(" success: %s ", t)) - } + color.Success.Println(fmt.Sprintf(" success: %s ", t)) } // if debug is true, print checking assertions with color blue - if debug { - color.Notice.Println("checking scenarios... 🚀") - } + color.Notice.Println("checking scenarios... 🚀") // Check Assertions for sn, scenario := range s.Scenarios { - if debug { - color.Notice.Printf("%v.scenario: %s - %s\n", sn+1, scenario.Name, scenario.Description) - color.Notice.Println(" checks:") - } + color.Notice.Printf("%v.scenario: %s - %s\n", sn+1, scenario.Name, scenario.Description) + color.Notice.Println(" checks:") for _, check := range scenario.Checks { entity, err := tuple.E(check.Entity) @@ -278,32 +241,22 @@ func validate() func(cmd *cobra.Command, args []string) error { query := tuple.SubjectToString(subject) + " " + permission + " " + tuple.EntityToString(entity) if res.Can == exp { - if debug { - color.Success.Print(" success:") - fmt.Printf(" %s \n", query) - } + color.Success.Print(" success:") + fmt.Printf(" %s \n", query) } else { - if debug { - color.Danger.Printf(" fail: %s ->", query) - } + color.Danger.Printf(" fail: %s ->", query) if res.Can == base.PermissionCheckResponse_RESULT_ALLOWED { - if debug { - color.Danger.Println(" expected: DENIED actual: ALLOWED ") - } + color.Danger.Println(" expected: DENIED actual: ALLOWED ") list.Add(fmt.Sprintf("%s -> expected: DENIED actual: ALLOWED ", query)) } else { - if debug { - color.Danger.Println(" expected: ALLOWED actual: DENIED ") - } + color.Danger.Println(" expected: ALLOWED actual: DENIED ") list.Add(fmt.Sprintf("%s -> expected: ALLOWED actual: DENIED ", query)) } } } } - if debug { - color.Notice.Println(" entity_filters:") - } + color.Notice.Println(" entity_filters:") for _, filter := range scenario.EntityFilters { @@ -356,22 +309,16 @@ func validate() func(cmd *cobra.Command, args []string) error { query := tuple.SubjectToString(subject) + " " + permission + " " + filter.EntityType if isSameArray(res.GetEntityIds(), expected) { - if debug { - color.Success.Print(" success:") - fmt.Printf(" %v\n", query) - } + color.Success.Print(" success:") + fmt.Printf(" %v\n", query) } else { - if debug { - color.Danger.Printf(" fail: %s -> expected: %+v actual: %+v\n", query, expected, res.GetEntityIds()) - } + color.Danger.Printf(" fail: %s -> expected: %+v actual: %+v\n", query, expected, res.GetEntityIds()) list.Add(fmt.Sprintf("%s -> expected: %+v actual: %+v", query, expected, res.GetEntityIds())) } } } - if debug { - color.Notice.Println(" subject_filters:") - } + color.Notice.Println(" subject_filters:") for _, filter := range scenario.SubjectFilters { @@ -420,14 +367,10 @@ func validate() func(cmd *cobra.Command, args []string) error { query := tuple.EntityToString(entity) + " " + permission + " " + filter.SubjectReference if isSameArray(res.GetSubjectIds(), expected) { - if debug { - color.Success.Print(" success:") - fmt.Printf(" %v\n", query) - } + color.Success.Print(" success:") + fmt.Printf(" %v\n", query) } else { - if debug { - color.Danger.Printf(" fail: %s -> expected: %+v actual: %+v\n", query, expected, res.GetSubjectIds()) - } + color.Danger.Printf(" fail: %s -> expected: %+v actual: %+v\n", query, expected, res.GetSubjectIds()) list.Add(fmt.Sprintf("%s -> expected: %+v actual: %+v", query, expected, res.GetSubjectIds())) } } @@ -435,27 +378,14 @@ func validate() func(cmd *cobra.Command, args []string) error { } if len(list.Errors) != 0 { - if debug { - list.Print() - os.Exit(1) - } - var b []byte - b, err = json.Marshal(list.Errors) - if err != nil { - return err - } - fmt.Println(string(b)) + list.Print() os.Exit(1) - } else { - fmt.Println("{}") } - if debug { - color.Notice.Println("schema successfully created") - color.Notice.Println("relationships successfully created") - color.Notice.Println("assertions successfully passed") - color.Success.Println("SUCCESS") - } + color.Notice.Println("schema successfully created") + color.Notice.Println("relationships successfully created") + color.Notice.Println("assertions successfully passed") + color.Success.Println("SUCCESS") return nil }