From 09d0b55883c598b1174d41d61434fb8926ca7974 Mon Sep 17 00:00:00 2001 From: Adriel Perkins Date: Tue, 21 Jun 2022 10:07:10 -0400 Subject: [PATCH] feat: add better flag handling; update README with examples --- README.md | 19 +++++++++++++++++++ schemacheck.go | 39 ++++++++++++++++++++++++++++++++------- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 1d88dd54..f7cc938c 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/schemacheck.go b/schemacheck.go index f236a931..c219904a 100644 --- a/schemacheck.go +++ b/schemacheck.go @@ -2,6 +2,7 @@ package main import ( "errors" + "fmt" "log" "os" "path/filepath" @@ -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 @@ -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 @@ -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))