Skip to content

Commit

Permalink
feat: Add strict flag to validate, only report missing vars under str…
Browse files Browse the repository at this point in the history
…ict mode
  • Loading branch information
cszatmary committed Jan 5, 2021
1 parent 0bcb1b1 commit dff156c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
9 changes: 8 additions & 1 deletion cmd/registry/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import (
"github.com/spf13/cobra"
)

type validateOptions struct {
strict bool
}

var validateOpts validateOptions

var validateCmd = &cobra.Command{
Use: "validate <path>",
Args: cobra.ExactArgs(1),
Expand All @@ -24,7 +30,7 @@ Example:
log.Infof(color.Cyan("Validating registry files at path %s..."), registryPath)

valid := true
result := registry.Validate(registryPath)
result := registry.Validate(registryPath, validateOpts.strict)
if errors.Is(result.AppsErr, registry.ErrFileNotExist) {
log.Infof(color.Yellow("No %s file"), registry.AppsFileName)
} else if result.AppsErr == nil {
Expand Down Expand Up @@ -60,5 +66,6 @@ Example:
}

func init() {
validateCmd.Flags().BoolVar(&validateOpts.strict, "strict", false, "Strict mode, treat more cases as errors")
registryCmd.AddCommand(validateCmd)
}
10 changes: 6 additions & 4 deletions registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ type readServicesOptions struct {
rootPath string
reposPath string
overrides map[string]service.ServiceOverride
strict bool
}

func readServices(r Registry, opts readServicesOptions) ([]service.Service, serviceGlobalConfig, error) {
Expand Down Expand Up @@ -281,7 +282,8 @@ func readServices(r Registry, opts readServicesOptions) ([]service.Service, serv

}

if len(errMsgs) > 0 {
// Report unknown vars as an error if in strict mode
if len(errMsgs) > 0 && opts.strict {
errs = append(errs, &ValidationError{
ResourceType: resourceTypeService,
ResourceName: s.Name,
Expand Down Expand Up @@ -497,13 +499,13 @@ type ValidateResult struct {
}

// Validate checks to see if the registry located at path is valid. It will read and validate
// each configuration file in the registry.
// each configuration file in the registry. If strict is true unknown variables will be considered errors.
//
// Validate returns a ValidateResult struct that contains errors encountered for each resource.
// If a configuration file is valid, then the corresponding error value will be nil. Otherwise,
// the error will be a non-nil value containing the details of why validation failed.
// If a configuration file does not exist, then the corresponding error will be ErrFileNotExist.
func Validate(path string) ValidateResult {
func Validate(path string, strict bool) ValidateResult {
r := Registry{
Name: filepath.Base(path),
Path: path,
Expand Down Expand Up @@ -540,7 +542,7 @@ func Validate(path string) ValidateResult {

servicesPath := filepath.Join(path, ServicesFileName)
if file.FileOrDirExists(servicesPath) {
services, _, err := readServices(r, readServicesOptions{})
services, _, err := readServices(r, readServicesOptions{strict: strict})
if err == nil {
// Keep track of ports to check for conflicting ports
usedPorts := make(map[string]string)
Expand Down
4 changes: 2 additions & 2 deletions registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,14 @@ func TestReadRegistries(t *testing.T) {
}

func TestValidate(t *testing.T) {
result := registry.Validate("testdata/registry-2")
result := registry.Validate("testdata/registry-2", true)
assert.NoError(t, result.AppsErr)
assert.NoError(t, result.PlaylistsErr)
assert.NoError(t, result.ServicesErr)
}

func TestValidateErrors(t *testing.T) {
result := registry.Validate("testdata/invalid-registry-1")
result := registry.Validate("testdata/invalid-registry-1", true)

var errList registry.ErrorList
var validationErr *registry.ValidationError
Expand Down

0 comments on commit dff156c

Please sign in to comment.