-
Notifications
You must be signed in to change notification settings - Fork 2
validate_configs.py rewritten in GoLang #3
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
Open
andylockran
wants to merge
6
commits into
imposter-project:main
Choose a base branch
from
andylockran:validate_config_go
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
01f9fb4
Tests required, but python-equivalent script in golang to stretch gol…
andylockran 308e533
Updated script for ease of read
andylockran 635ca23
Working on adding some tests
andylockran 90ccbdb
Updated tests
andylockran 14feafd
Breaking up the logic
andylockran 2db32af
Update tools/validate_config/README.md
andylockran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,65 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/akamensky/argparse" | ||
| "github.com/xeipuuv/gojsonschema" | ||
| "sigs.k8s.io/yaml" | ||
| ) | ||
|
|
||
| func main() { | ||
| // Create a new parser | ||
| parser := argparse.NewParser("validate_configs", "Validates your imposter configs against the imposter config schema.") | ||
| // Create a ref to folder | ||
| c := parser.String("c", "configs", &argparse.Options{Required: true, Help: "Location of config files"}) | ||
|
|
||
| err := parser.Parse(os.Args) | ||
|
|
||
| if err != nil { | ||
| // In case of error print error and print usage | ||
| // This can also be done by passing -h or --help flags | ||
| fmt.Print(parser.Usage(err)) | ||
| } | ||
| if err != nil { | ||
| log.Println(err) | ||
| } | ||
| basePath, err := os.Getwd() | ||
| filename := "imposter-config-schema.json" | ||
| fullpath := filepath.Join(basePath, filename) | ||
| withfileprefix := "file://" + fullpath | ||
| schemaLoader := gojsonschema.NewReferenceLoader(withfileprefix) | ||
| fmt.Println("Validating config files") | ||
| filepath.Walk(*c, func(path string, info os.FileInfo, err error) error { | ||
| if err != nil { | ||
| fmt.Println(err) | ||
| return err | ||
| } | ||
| if info.IsDir() == false { | ||
| if strings.Contains(path, "-config.yaml") { | ||
| docYaml, err := os.ReadFile(path) | ||
| j2, err := yaml.YAMLToJSON(docYaml) | ||
|
|
||
| documentLoader := gojsonschema.NewStringLoader(string(j2)) | ||
| result, err := gojsonschema.Validate(schemaLoader, documentLoader) | ||
| if err != nil { | ||
| panic(err.Error()) | ||
| } | ||
|
|
||
| if result.Valid() { | ||
| fmt.Printf("✓ %s - Valid\n", path) | ||
| } else { | ||
| fmt.Printf("✗ %s - Invalid:\n", path) | ||
| for _, desc := range result.Errors() { | ||
| fmt.Printf("\t - %s\n", desc) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return nil | ||
| }) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.