Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better linting and error handling #81

Merged
merged 8 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: more linting and code gen for JSON schema
  • Loading branch information
jippi committed Sep 11, 2024
commit 85b94ec38f95b5f0b5903a8d16c1d55bdcb75570
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
/schema/ignore
/scm-engine
/scm-engine.exe
/scm-engine.schema.json
scm-engine.schema.json
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ tasks:
- go run . gitlab -h > docs/gitlab/_partials/cmd-gitlab.md
- go run . gitlab evaluate -h > docs/gitlab/_partials/cmd-gitlab-evaluate.md
- go run . gitlab server -h > docs/gitlab/_partials/cmd-gitlab-server.md
- go run . jsonschema > docs/scm-engine.schema.json
- cp pkg/generated/resources/scm-engine.schema.json docs/scm-engine.schema.json

docs:server:
desc: Run Docs dev server with live preview
Expand Down
4 changes: 2 additions & 2 deletions cmd/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ var GitLab = &cli.Command{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "schema",
Usage: "The HTTP/HTTPS URL or file path to the scm-engine schema file",
Value: "https://jippi.github.io/scm-engine/scm-engine.schema.json",
Usage: "Where to find the JSON Schema file. Can load the file from either the embedded version (default), http://, https://, or a file:// URI",
Value: "embed://",
},
},
},
Expand Down
28 changes: 19 additions & 9 deletions cmd/gitlab_lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"fmt"
"net/http"
"os"
"strings"
"time"

"github.com/jippi/scm-engine/pkg/config"
"github.com/jippi/scm-engine/pkg/generated/resources"
"github.com/jippi/scm-engine/pkg/scm/gitlab"
"github.com/jippi/scm-engine/pkg/state"
"github.com/santhosh-tekuri/jsonschema/v6"
Expand All @@ -19,15 +21,6 @@ func Lint(cCtx *cli.Context) error {
ctx := cCtx.Context
ctx = state.WithConfigFilePath(ctx, cCtx.String(FlagConfigFile))

cfg, err := config.LoadFile(state.ConfigFilePath(ctx))
if err != nil {
return err
}

if len(cfg.Includes) != 0 {
slogctx.Warn(ctx, "Configuration file contains 'include' settings, those are currently unsupported by 'lint' command and will be ignored")
}

// Read raw YAML file
raw, err := os.ReadFile(state.ConfigFilePath(ctx))
if err != nil {
Expand All @@ -45,6 +38,7 @@ func Lint(cCtx *cli.Context) error {
"file": jsonschema.FileLoader{},
"http": newHTTPURLLoader(),
"https": newHTTPURLLoader(),
"embed": &EmbedLoader{},
}

// Create json schema compiler
Expand All @@ -62,10 +56,26 @@ func Lint(cCtx *cli.Context) error {
return err
}

// Load the configuration file via our Go struct
cfg, err := config.LoadFile(state.ConfigFilePath(ctx))
if err != nil {
return err
}

if len(cfg.Includes) != 0 {
slogctx.Warn(ctx, "Configuration file contains 'include' settings, those are currently unsupported by 'lint' command and will be ignored")
}

// To scm-engine specific linting last
return cfg.Lint(ctx, &gitlab.Context{})
}

type EmbedLoader struct{}

func (l *EmbedLoader) Load(url string) (any, error) {
return jsonschema.UnmarshalJSON(strings.NewReader(resources.JSONSchema))
}

type HTTPURLLoader http.Client

func (l *HTTPURLLoader) Load(url string) (any, error) {
Expand Down
3 changes: 3 additions & 0 deletions generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package main

//go:generate go run ./pkg/generated/main.go
25 changes: 3 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
//go:build !generate
// +build !generate

package main

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

"github.com/davecgh/go-spew/spew"
"github.com/invopop/jsonschema"
"github.com/jippi/scm-engine/cmd"
"github.com/jippi/scm-engine/pkg/config"
"github.com/jippi/scm-engine/pkg/state"
"github.com/jippi/scm-engine/pkg/tui"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -72,26 +72,7 @@ func main() {
Commands: []*cli.Command{
cmd.GitLab,
cmd.GitHub,
{
Name: "jsonschema",
Action: func(ctx *cli.Context) error {
r := new(jsonschema.Reflector)
if err := r.AddGoComments("github.com/jippi/scm-engine", "./"); err != nil {
return err
}

schema := r.Reflect(&config.Config{})

data, err := json.MarshalIndent(schema, "", " ")
if err != nil {
panic(err.Error())
}

fmt.Println(string(data))

return os.WriteFile("scm-engine.schema.json", data, 0o600)
},
},
// DEPRECATED COMMANDS
{
Name: "evaluate",
Expand Down
35 changes: 35 additions & 0 deletions pkg/generated/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

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

"github.com/invopop/jsonschema"
"github.com/jippi/scm-engine/pkg/config"
)

func main() {
r := new(jsonschema.Reflector)
if err := r.AddGoComments("github.com/jippi/scm-engine", "./pkg"); err != nil {
panic(err)
}

schema := r.Reflect(&config.Config{})

data, err := json.MarshalIndent(schema, "", " ")
if err != nil {
panic(err)
}

fmt.Println(string(data))

dir, err := os.Getwd()
if err != nil {
panic(err)
}

if err := os.WriteFile(dir+"/pkg/generated/resources/scm-engine.schema.json", data, 0o600); err != nil {
panic(err)
}
}
6 changes: 6 additions & 0 deletions pkg/generated/resources/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package resources

import _ "embed"

//go:embed scm-engine.schema.json
var JSONSchema string
Loading