Skip to content

Commit

Permalink
feat(config): add update configuration details
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed May 9, 2019
1 parent 50388e5 commit 5786d68
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 24 deletions.
57 changes: 33 additions & 24 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Config struct {
Repo *Repo
Store *Store
P2P *P2P
Update *Update

Registry *Registry
Remotes *Remotes
Expand Down Expand Up @@ -53,11 +54,14 @@ func DefaultConfig() *Config {
Profile: DefaultProfile(),
Repo: DefaultRepo(),
Store: DefaultStore(),
P2P: DefaultP2P(),
Update: DefaultUpdate(),

Registry: DefaultRegistry(),
// default to no configured remotes

CLI: DefaultCLI(),
API: DefaultAPI(),
P2P: DefaultP2P(),
Webapp: DefaultWebapp(),
RPC: DefaultRPC(),
Logging: DefaultLogging(),
Expand All @@ -68,6 +72,8 @@ func DefaultConfig() *Config {

// SummaryString creates a pretty string summarizing the
// configuration, useful for log output
// TODO (b5): this summary string doesn't confirm these services are actually
// running. we should move this elsewhere
func (cfg Config) SummaryString() (summary string) {
summary = "\n"
if cfg.Profile != nil {
Expand Down Expand Up @@ -174,6 +180,10 @@ func validate(rs *jsonschema.RootSchema, s interface{}) error {
return nil
}

type validator interface {
Validate() error
}

// Validate validates each section of the config struct,
// returning the first error
func (cfg Config) Validate() error {
Expand All @@ -199,31 +209,27 @@ func (cfg Config) Validate() error {
return err
}

if err := cfg.Profile.Validate(); err != nil {
return err
}
if err := cfg.Repo.Validate(); err != nil {
return err
}
if err := cfg.Store.Validate(); err != nil {
return err
}
if err := cfg.P2P.Validate(); err != nil {
return err
validators := []validator{
cfg.Profile,
cfg.Repo,
cfg.Store,
cfg.P2P,
cfg.CLI,
cfg.API,
cfg.Webapp,
cfg.RPC,
cfg.Update,
cfg.Logging,
}
if err := cfg.CLI.Validate(); err != nil {
return err
}
if err := cfg.API.Validate(); err != nil {
return err
}
if err := cfg.Webapp.Validate(); err != nil {
return err
for _, val := range validators {
if val != nil {
if err := val.Validate(); err != nil {
return err
}
}
}
if err := cfg.RPC.Validate(); err != nil {
return err
}
return cfg.Logging.Validate()

return nil
}

// Copy returns a deep copy of the Config struct
Expand All @@ -244,6 +250,9 @@ func (cfg *Config) Copy() *Config {
if cfg.P2P != nil {
res.P2P = cfg.P2P.Copy()
}
if cfg.Update != nil {
res.Update = cfg.Update.Copy()
}
if cfg.Registry != nil {
res.Registry = cfg.Registry.Copy()
}
Expand Down
1 change: 1 addition & 0 deletions config/testdata/simple.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ Render: null
Repo: null
Revision: 1
Store: null
Update: null
Webapp: null
53 changes: 53 additions & 0 deletions config/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package config

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

// Update configures a Remote Procedure Call (Update) listener
type Update struct {
Daemonize bool `json:"daemonize"`
Port int `json:"port"`
}

// DefaultUpdatePort is local the port Update serves on by default
var DefaultUpdatePort = 2506

// DefaultUpdate creates a new default Update configuration
func DefaultUpdate() *Update {
return &Update{
Daemonize: true,
Port: DefaultUpdatePort,
}
}

// Validate validates all fields of rpc returning all errors found.
func (cfg Update) Validate() error {
schema := jsonschema.Must(`
{
"$schema": "http://json-schema.org/draft-06/schema#",
"title": "Update",
"description": "The Update configuration",
"type": "object",
"required": ["daemonize", "port"],
"properties": {
"deamonize": {
"description": "When true, the update service starts as a daemonized process",
"type": "boolean"
},
"port": {
"description": "port update service will listen for rpc calls",
"type": "integer"
}
}
}`)
return validate(schema, &cfg)
}

// Copy makes a deep copy of the Update struct
func (cfg *Update) Copy() *Update {
res := &Update{
Daemonize: cfg.Daemonize,
Port: cfg.Port,
}

return res
}
33 changes: 33 additions & 0 deletions config/update_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package config

import (
"reflect"
"testing"
)

func TestUpdateValidate(t *testing.T) {
err := DefaultUpdate().Validate()
if err != nil {
t.Errorf("error validating default update: %s", err)
}
}

func TestUpdateCopy(t *testing.T) {
cases := []struct {
rpc *Update
}{
{DefaultUpdate()},
}
for i, c := range cases {
cpy := c.rpc.Copy()
if !reflect.DeepEqual(cpy, c.rpc) {
t.Errorf("Update Copy test case %v, rpc structs are not equal: \ncopy: %v, \noriginal: %v", i, cpy, c.rpc)
continue
}
cpy.Daemonize = false
if reflect.DeepEqual(cpy, c.rpc) {
t.Errorf("Update Copy test case %v, editing one rpc struct should not affect the other: \ncopy: %v, \noriginal: %v", i, cpy, c.rpc)
continue
}
}
}

0 comments on commit 5786d68

Please sign in to comment.