Skip to content

Commit

Permalink
new: add init subcommand back (#68)
Browse files Browse the repository at this point in the history
The rego init command was removed in a prior patch when we moved to
go:embed. This patch brings back the feature and modernizes the initial
specset.
  • Loading branch information
primalmotion authored Apr 13, 2023
1 parent 4e113e6 commit 3c01345
Show file tree
Hide file tree
Showing 14 changed files with 383 additions and 0 deletions.
22 changes: 22 additions & 0 deletions cmd/rego/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/spf13/viper"
"go.aporeto.io/regolithe/cmd/rego/doc"
"go.aporeto.io/regolithe/cmd/rego/jsonschema"
"go.aporeto.io/regolithe/cmd/rego/specset"
"go.aporeto.io/regolithe/spec"
)

Expand Down Expand Up @@ -159,9 +160,30 @@ func main() {
jsonSchemaCmd.Flags().StringP("out", "o", "./codegen", "Path where to write the json files.")
jsonSchemaCmd.Flags().BoolP("public", "p", false, "If set to true, only exposed attributes and public objects will be generated.")

var initCmd = &cobra.Command{
Use: "init <dest>",
Short: "Generate a new set of specification",
SilenceErrors: true,
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
return viper.BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {

if len(args) != 1 {
return fmt.Errorf("usage: init <dest>")
}

dir := args[0]

return specset.Dump(dir)
},
}

rootCmd.AddCommand(
formatCmd,
docCmd,
initCmd,
jsonSchemaCmd,
)

Expand Down
46 changes: 46 additions & 0 deletions cmd/rego/specset/data/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
MAKEFLAGS += --warn-undefined-variables
SHELL := /bin/bash -o pipefail

default: format codegen

.PHONY:codegen
codegen:
@ elegen folder -d specs -o codegen -g openapi3 || exit 1
@ rm -rf openapi3
@ elegen folder -d specs -o codegen || exit 1
@ mv custom_validations.go custom_validations.go.keep
@ mv custom_validations_test.go custom_validations_test.go.keep
@ rm -rf ./*.go
@ mv custom_validations.go.keep custom_validations.go
@ mv custom_validations_test.go.keep custom_validations_test.go
@ mv codegen/elemental/*.go ./
@ mv codegen/openapi3 openapi3
@ rm -rf codegen doc
@ mkdir -p doc
@ data=$$(rego doc -d specs || exit 1) && echo -e "$${data}" > doc/documentation.md

format: format-specs format-type format-validation format-parameter
format-specs:
@ for f in specs/*.abs; do \
rego format < $$f > $$f.formatted && \
mv $$f.formatted $$f; \
done
@ for f in specs/*.spec; do \
rego format < $$f > $$f.formatted && \
mv $$f.formatted $$f; \
done

format-type: target = "specs/_type.mapping"
format-type:
@ rego format -m typemapping < $(target) > $(target).formatted
@ mv $(target).formatted $(target)

format-validation: target = "specs/_validation.mapping"
format-validation:
@ rego format -m validationmapping < $(target) > $(target).formatted
@ mv $(target).formatted $(target)

format-parameter: target = "specs/_parameter.mapping"
format-parameter:
@ rego format -m parametermapping < $(target) > $(target).formatted
@ mv $(target).formatted $(target)
43 changes: 43 additions & 0 deletions cmd/rego/specset/data/custom_validations.nogo
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package api

import (
"fmt"
"net/http"
"net/url"

)

// ValidateURL validates the given value is a correct url.
func ValidateURL(attribute string, u string) error {

uu, err := url.Parse(u)
if err != nil {
return makeErr(attribute, fmt.Sprintf("invalid url: %s", err))
}

switch uu.Scheme {
case "http", "https":
case "":
return makeErr(attribute, "invalid url: missing scheme")
default:
return makeErr(attribute, "invalid url: invalid scheme")
}

return nil
}

func makeErr(attribute string, message string) elemental.Error {

err := elemental.NewError(
"Validation Error",
message,
"a3s",
http.StatusUnprocessableEntity,
)

if attribute != "" {
err.Data = map[string]any{"attribute": attribute}
}

return err
}
91 changes: 91 additions & 0 deletions cmd/rego/specset/data/custom_validations_test.nogo
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package api

import (
"testing"
)

func TestValidateURL(t *testing.T) {
type args struct {
attribute string
u string
}
tests := []struct {
name string
args func(t *testing.T) args

wantErr bool
inspectErr func(err error, t *testing.T) //use for more precise error evaluation after test
}{
{
"valid url",
func(t *testing.T) args {
return args{
"attr",
"https://toto.com",
}
},
false,
nil,
},
{
"invalid url",
func(t *testing.T) args {
return args{
"attr",
"wesh",
}
},
true,
nil,
},
{
"invalid url 2",
func(t *testing.T) args {
return args{
"attr",
"",
}
},
true,
nil,
},
{
"invalid url 3",
func(t *testing.T) args {
return args{
"attr",
"http##dd%",
}
},
true,
nil,
},
{
"invalid scheme",
func(t *testing.T) args {
return args{
"attr",
"ftp://what.com",
}
},
true,
nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tArgs := tt.args(t)

err := ValidateURL(tArgs.attribute, tArgs.u)

if (err != nil) != tt.wantErr {
t.Fatalf("ValidateURL error = %v, wantErr: %t", err, tt.wantErr)
}

if tt.inspectErr != nil {
tt.inspectErr(err, t)
}
})
}
}
3 changes: 3 additions & 0 deletions cmd/rego/specset/data/specs/.regolithe-gen-cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
cd .. || exit 0
make codegen
13 changes: 13 additions & 0 deletions cmd/rego/specset/data/specs/@identifiable.abs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Attributes
attributes:
v1:
- name: ID
description: ID is the identifier of the object.
type: string
exposed: true
stored: true
read_only: true
autogenerated: true
filterable: true
identifier: true
orderable: true
3 changes: 3 additions & 0 deletions cmd/rego/specset/data/specs/_api.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
prefix: api
root: root
version: 1
6 changes: 6 additions & 0 deletions cmd/rego/specset/data/specs/_parameter.mapping
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
$queryable:
entries:
- name: q
description: This is an example.
type: string
example_value: hello == world
15 changes: 15 additions & 0 deletions cmd/rego/specset/data/specs/_type.mapping
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'[][]string':
elemental:
type: '[][]string'
init: '[][]string{}'
jsonschema:
type: |-
{
"type": "array",
"items": {
"type": "array",
"items": {
"type": "string"
}
}
}
3 changes: 3 additions & 0 deletions cmd/rego/specset/data/specs/_validation.mapping
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$url:
elemental:
name: ValidateURL
25 changes: 25 additions & 0 deletions cmd/rego/specset/data/specs/object.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Model
model:
rest_name: object
resource_name: objects
entity_name: Objects
package: default
group: core
description: This is random object.
get:
description: Gets the object.
update:
description: Updates the object.
delete:
description: Deletes the object.
extends:
- '@identifiable'

# Attributes
attributes:
v1:
- name: name
description: The name of the object.
type: string
exposed: true
stored: true
10 changes: 10 additions & 0 deletions cmd/rego/specset/data/specs/regolithe.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[regolithe]
product_name = api
copyright = yourcompany

[transformer]
name = api
url = www.mycompany.net/myapi
author = you
email = [email protected]
version = 1.0
19 changes: 19 additions & 0 deletions cmd/rego/specset/data/specs/root.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Model
model:
rest_name: root
resource_name: root
entity_name: Root
package: root
group: core
description: root object.
get:
description: gets the object.
root: true

# Relations
relations:
- rest_name: object
get:
description: Retrieves the list of objects.
create:
description: Creates a new object.
Loading

0 comments on commit 3c01345

Please sign in to comment.