Skip to content

Commit

Permalink
feat(models): add validate cmd that accepts a json model & validate…
Browse files Browse the repository at this point in the history
…s it
  • Loading branch information
rhamzeh committed Jun 14, 2023
1 parent 8fd6093 commit 0011c07
Show file tree
Hide file tree
Showing 5 changed files with 313 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ linters-settings:
- github.com/spf13/viper
- github.com/openfga/cli
- github.com/openfga/go-sdk
- github.com/openfga/openfga
- go.buf.build/openfga/go/openfga/api
test:
files:
- "$test"
Expand Down
1 change: 1 addition & 0 deletions cmd/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func init() {
ModelsCmd.AddCommand(writeCmd)
ModelsCmd.AddCommand(listCmd)
ModelsCmd.AddCommand(getCmd)
ModelsCmd.AddCommand(validateCmd)

ModelsCmd.PersistentFlags().String("store-id", "", "Store ID")
ModelsCmd.Flags().String("store-id", "", "Store ID")
Expand Down
52 changes: 52 additions & 0 deletions cmd/models/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright © 2023 OpenFGA
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package models

import (
"encoding/json"
"fmt"
"os"

"github.com/openfga/openfga/pkg/typesystem"
"github.com/spf13/cobra"
openfgapb "go.buf.build/openfga/go/openfga/api/openfga/v1"
)

// validateCmd represents the get command.
var validateCmd = &cobra.Command{
Use: "validate",
Short: "Validates an authorization model",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
model := &openfgapb.AuthorizationModel{}
err := json.Unmarshal([]byte(args[0]), &model)
if err != nil {
fmt.Printf("Failed to parse input due to %v", err)
os.Exit(1)
}

_, err = typesystem.NewAndValidate(model)

if err != nil {
fmt.Printf("Model is INVALID. Error = %v", err)
os.Exit(1)
}
fmt.Printf("Model is VALID.")
},
}

func init() {
}
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ go 1.20

require (
github.com/openfga/go-sdk v0.2.3-0.20230613190254-a6fb3f6bbb9f
github.com/openfga/openfga v1.1.0
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
go.buf.build/openfga/go/openfga/api v1.2.50
)

require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
Expand All @@ -20,9 +24,15 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
go.buf.build/openfga/go/envoyproxy/protoc-gen-validate v1.2.8 // indirect
go.buf.build/openfga/go/grpc-ecosystem/grpc-gateway v1.2.50 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/text v0.10.0 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 0011c07

Please sign in to comment.