Skip to content

Commit

Permalink
Add schema generation
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwhitaker committed Jul 22, 2024
1 parent c6557a7 commit 5ecf9a6
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 49 deletions.
2 changes: 2 additions & 0 deletions .runny.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ commands:
run: |
export GITHUB_TOKEN=$(gh auth token)
goreleaser
generate-schema:
run: go generate ./...
test:
run: go test ./...
test-env:
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"yaml.schemas": {
"./schema.json": ".runny.yaml"
"./schema/runny.schema.json": ".runny.yaml"
}
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ go 1.22.5

require (
github.com/fatih/color v1.17.0
github.com/invopop/jsonschema v0.12.0
golang.org/x/term v0.22.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
golang.org/x/sys v0.22.0 // indirect
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/invopop/jsonschema v0.12.0 h1:6ovsNSuvn9wEQVOyc72aycBMVQFKz7cPdMJn10CvzRI=
github.com/invopop/jsonschema v0.12.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
Expand Down
14 changes: 7 additions & 7 deletions runny/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import (
type CommandName string

type CommandDef struct {
Run string
Needs []CommandName
If string
Env []string
Run string `json:"run,omitempty"`
Needs []CommandName `json:"needs,omitempty"`
If string `json:"if,omitempty"`
Env []string `json:"env,omitempty"`
}

type Config struct {
Commands map[CommandName]CommandDef
Shell string
Env []string
Commands map[CommandName]CommandDef `json:"commands"`
Shell string `json:"shell,omitempty"`
Env []string `json:"env,omitempty"`
verbose bool
}

Expand Down
41 changes: 0 additions & 41 deletions schema.json

This file was deleted.

45 changes: 45 additions & 0 deletions schema/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

//go:generate go run . ./runny.schema.json

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

"github.com/invopop/jsonschema"
"github.com/simonwhitaker/runny/runny"
)

func main() {
schema := jsonschema.Reflect(&runny.Config{})
bytes, err := schema.MarshalJSON()
if err != nil {
panic(err)
}

// jsonschema doesn't support indenting, so we need to unmarshal/marshal with the json package
var tempJsonObj map[string]interface{}
err = json.Unmarshal(bytes, &tempJsonObj)
if err != nil {
panic(err)
}

indentedSchema, err := json.MarshalIndent(tempJsonObj, "", " ")
schemaString := string(indentedSchema[:])

if err != nil {
panic(err)
}
if len(os.Args) > 1 {
filename := os.Args[1]
f, err := os.Create(filename)
if err != nil {
panic(err)
}
defer f.Close()
f.WriteString(schemaString)
} else {
fmt.Println(schemaString)
}
}
55 changes: 55 additions & 0 deletions schema/runny.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"$defs": {
"CommandDef": {
"additionalProperties": false,
"properties": {
"env": {
"items": {
"type": "string"
},
"type": "array"
},
"if": {
"type": "string"
},
"needs": {
"items": {
"type": "string"
},
"type": "array"
},
"run": {
"type": "string"
}
},
"type": "object"
},
"Config": {
"additionalProperties": false,
"properties": {
"commands": {
"additionalProperties": {
"$ref": "#/$defs/CommandDef"
},
"type": "object"
},
"env": {
"items": {
"type": "string"
},
"type": "array"
},
"shell": {
"type": "string"
}
},
"required": [
"commands"
],
"type": "object"
}
},
"$id": "https://github.com/simonwhitaker/runny/runny/config",
"$ref": "#/$defs/Config",
"$schema": "https://json-schema.org/draft/2020-12/schema"
}

0 comments on commit 5ecf9a6

Please sign in to comment.