Skip to content

Commit

Permalink
feat: add better flag handling; update README with examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriel Perkins committed Jun 21, 2022
1 parent 22b28d0 commit 09d0b55
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@
A CLI utility written in [go](go.dev) that validates `json` and `yaml` files
against a `schema`.

## Usage
`schemacheck` is meant to be used against one schema and one or more `yaml` or
`json` files.

After installation, you can run it like:
```
schemacheck --schema myschema.json --file myjson.json --file myyaml.yaml .......
```

You can get the usage at any time by running:
```
schemacheck --help
```

You can also call this CLI from other command line utililties like `find`.
```
find . -type f -name "*.json" -exec ./dist/bin/schemacheck -s test_data/schema.json -f {} \+
```

## Install
There are a few different methods to install `schemacheck`.

Expand Down
39 changes: 32 additions & 7 deletions schemacheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
Expand All @@ -14,11 +15,9 @@ import (

// set default constants for usage messages and default file names
const (
defaultSchema = "test_data/schema.json"
schemaUsage = "A valid JSON schema file to use for validation. Default: schema.json"
schemaUsage = "A valid JSON schema file to use for validation. Default: schema.json"

defaultFileName = "test_data/values.json"
fileUsage = "A Yaml or JSON file to check against a given schema. Default: values.json (can acceptable multiples)"
fileUsage = "A Yaml or JSON file to check against a given schema. Default: values.json (can acceptable multiples)"
)

// Gloval variables for flags and logger
Expand All @@ -34,9 +33,28 @@ var (

// initialize the flags from the command line and their shorthand counterparts
func init() {
defaultFile := []string{defaultFileName}
flag.StringVarP(&Schema, "schema", "s", defaultSchema, schemaUsage)
flag.StringSliceVarP(&File, "file", "f", defaultFile, fileUsage)
flag.StringVarP(&Schema, "schema", "s", "", schemaUsage)
flag.StringSliceVarP(&File, "file", "f", []string{}, fileUsage)
}

func CheckForEmptyArg() bool {
schemaArgEmpty := true
fileArgEmpty := true
flag.VisitAll(func(f *flag.Flag) {
if f.Name == "schema" {
if f.Changed {
schemaArgEmpty = false
}
} else if f.Name == "file" {
if f.Changed {
fileArgEmpty = false
}
}
})
if schemaArgEmpty || fileArgEmpty {
return true
}
return false
}

// Checks whether a given file is of the supported extension type and if not
Expand Down Expand Up @@ -114,6 +132,13 @@ func main() {
// parse the flags set in the init() function
flag.Parse()

// Check to ensure flags aren't empty
missingArgs := CheckForEmptyArg()
if missingArgs {
fmt.Fprintf(os.Stderr, "Usage of schemacheck\n")
flag.PrintDefaults()
errLogger.Fatal("One or more missing args not set.")
}
// Load schema file before running through and validating the other files to
// reduce how many times it's loaded.
schema, err := os.ReadFile(filepath.Clean(Schema))
Expand Down

0 comments on commit 09d0b55

Please sign in to comment.