Skip to content

Commit

Permalink
feat(config): add basic validation for cli config
Browse files Browse the repository at this point in the history
  • Loading branch information
ramfox committed Apr 9, 2018
1 parent c353200 commit 0bea261
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
26 changes: 23 additions & 3 deletions config/cli.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
package config

import "github.com/qri-io/jsonschema"

// CLI defines configuration details for the qri command line client (CLI)
type CLI struct {
ColorizeOutput bool
ColorizeOutput bool `json:"colorizeoutput"`
}

// Default returns a new default CLI configuration
func (CLI) Default() *CLI {
// DefaultCLI returns a new default CLI configuration
func DefaultCLI() *CLI {
return &CLI{
ColorizeOutput: true,
}
}

// Validate validates all fields of cli returning all errors found.
func (c CLI) Validate() error {
schema := jsonschema.Must(`{
"$schema": "http://json-schema.org/draft-06/schema#",
"title": "CLI",
"description": "Config for the CLI",
"type": "object",
"required": ["colorizeoutput"],
"properties": {
"colorizeoutput": {
"description": "When true, output to the command line will be colorized",
"type": "boolean"
}
}
}`)
return validate(schema, &c)
}
15 changes: 15 additions & 0 deletions config/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package config

import (
"testing"
)

func TestCLIValidate(t *testing.T) {
cliDefault := CLI{
ColorizeOutput: true,
}
err := cliDefault.Validate()
if err != nil {
t.Errorf("error validating cliDefault: %s", err)
}
}
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (Config) Default() *Config {
Repo: Repo{}.Default(),
Store: Store{}.Default(),

CLI: CLI{}.Default(),
CLI: DefaultCLI(),
API: DefaultAPI(),
P2P: P2P{}.Default(),
Webapp: Webapp{}.Default(),
Expand Down

0 comments on commit 0bea261

Please sign in to comment.