Skip to content

Commit

Permalink
fix: fixed output and help
Browse files Browse the repository at this point in the history
  • Loading branch information
aaguiarz committed Jul 28, 2023
1 parent 7e692e7 commit 5d079c8
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions cmd/model/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ import (
var transformCmd = &cobra.Command{
Use: "transform",
Short: "Transforms an authorization model",
Example: `fga model transform --file=model.json --from-json
Example: `fga model transform --file=model.json --input-format json
fga model transform --file=model.fga
fga model transform '{"schema_version":"1.1,"type_definitions":[{"type":"user"}]} --from-json'`,
fga model transform '{ "schema_version": "1.1", "type_definitions":[{"type":"user"}] }' --input-format json`,

Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var inputModel string
Expand All @@ -46,18 +47,13 @@ fga model transform '{"schema_version":"1.1,"type_definitions":[{"type":"user"}]
return err //nolint:wrapcheck
}

fields, err := cmd.Flags().GetStringArray("field")
if err != nil {
return fmt.Errorf("failed to parse field array flag due to %w", err)
}

authModel := authorizationmodel.AuthzModel{}
if transformInputFormat == authorizationmodel.ModelFormatJSON {
if err := authModel.ReadFromJSONString(inputModel); err != nil {
return err //nolint:wrapcheck
}

dslModel, err := authModel.DisplayAsDSL(fields)
dslModel, err := authModel.DisplayAsDSL([]string{"model"})
if err != nil {
return fmt.Errorf("failed to transform model due to %w", err)
}
Expand All @@ -70,14 +66,13 @@ fga model transform '{"schema_version":"1.1,"type_definitions":[{"type":"user"}]
return err //nolint:wrapcheck
}

return output.Display(authModel.DisplayAsJSON(fields)) //nolint:wrapcheck
return output.Display(authModel.DisplayAsJSON([]string{"model"})) //nolint:wrapcheck
},
}

var transformInputFormat = authorizationmodel.ModelFormatFGA

func init() {
transformCmd.Flags().String("file", "", "File Name. The file should have the model in the JSON or DSL format")
transformCmd.Flags().Var(&transformInputFormat, "input-format", `Authorization model input format. Can be "fga" or "json"`) //nolint:lll
transformCmd.Flags().StringArray("field", []string{"id", "created_at", "model"}, "Fields to display, choices are: id, created_at and model") //nolint:lll
transformCmd.Flags().Var(&transformInputFormat, "input-format", `Authorization model input format. Can be "fga" or "json"`) //nolint:lll
}

0 comments on commit 5d079c8

Please sign in to comment.