diff --git a/.runny.yaml b/.runny.yaml index 5133248..b8f8af6 100644 --- a/.runny.yaml +++ b/.runny.yaml @@ -17,6 +17,8 @@ commands: run: | export GITHUB_TOKEN=$(gh auth token) goreleaser + generate-schema: + run: go generate ./... test: run: go test ./... test-env: diff --git a/.vscode/settings.json b/.vscode/settings.json index 8a5ccee..7586e22 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,5 @@ { "yaml.schemas": { - "./schema.json": ".runny.yaml" + "./schema/runny.schema.json": ".runny.yaml" } } diff --git a/go.mod b/go.mod index 383011c..1961b4f 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 6a60813..c1c9711 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/runny/model.go b/runny/model.go index 14a2838..5d8735a 100644 --- a/runny/model.go +++ b/runny/model.go @@ -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 } diff --git a/schema.json b/schema.json deleted file mode 100644 index 083b245..0000000 --- a/schema.json +++ /dev/null @@ -1,41 +0,0 @@ -{ - "type": "object", - "properties": { - "shell": { - "type": "string" - }, - "env": { - "type": "array", - "items": { - "type": "string" - } - }, - "commands": { - "type": "object", - "additionalProperties": { - "type": "object", - "properties": { - "if": { - "type": "string" - }, - "run": { - "type": "string" - }, - "needs": { - "type": "array", - "items": { - "type": "string" - } - }, - "env": { - "type": "array", - "items": { - "type": "string" - } - } - } - } - } - }, - "required": ["commands"] -} diff --git a/schema/main.go b/schema/main.go new file mode 100644 index 0000000..6bda3a0 --- /dev/null +++ b/schema/main.go @@ -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) + } +} diff --git a/schema/runny.schema.json b/schema/runny.schema.json new file mode 100644 index 0000000..e50c030 --- /dev/null +++ b/schema/runny.schema.json @@ -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" +} \ No newline at end of file