Skip to content

Commit

Permalink
Lint.
Browse files Browse the repository at this point in the history
  • Loading branch information
udhos committed Mar 14, 2017
1 parent 07ea11c commit a283e25
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 69 deletions.
13 changes: 13 additions & 0 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"github.com/udhos/jazigo/store"
)

// Change stores info about last config change.
type Change struct {
When time.Time
By string
From string
}

// AppConfig is persistent global configuration.
type AppConfig struct {
MaxConfigFiles int
Holdtime time.Duration
Expand All @@ -24,6 +26,7 @@ type AppConfig struct {
Comment string // free user-defined field
}

// NewAppConfigFromString creates AppConfig from string.
func NewAppConfigFromString(str string) (*AppConfig, error) {
b := []byte(str)
c := &AppConfig{}
Expand All @@ -33,6 +36,7 @@ func NewAppConfigFromString(str string) (*AppConfig, error) {
return c, nil
}

// Dump exports AppConfig as YAML.
func (a *AppConfig) Dump() ([]byte, error) {
b, err := yaml.Marshal(a)
if err != nil {
Expand All @@ -41,6 +45,7 @@ func (a *AppConfig) Dump() ([]byte, error) {
return b, nil
}

// NewDevAttr creates a new set of DevAttributes.
func NewDevAttr() DevAttributes {
a := DevAttributes{
ErrlogHistSize: 60, // default max number of lines in errlog history
Expand All @@ -49,6 +54,7 @@ func NewDevAttr() DevAttributes {
return a
}

// DevAttributes is per-model set of default attributes for device.
type DevAttributes struct {
NeedLoginChat bool // need login chat
NeedEnabledMode bool // need enabled mode
Expand Down Expand Up @@ -83,6 +89,7 @@ type DevAttributes struct {
CommandMatchTimeout time.Duration // larger timeout for slow responses (slow show running)
}

// DevConfig is full set of device properties.
type DevConfig struct {
Debug bool
Deleted bool
Expand All @@ -98,6 +105,7 @@ type DevConfig struct {
Attr DevAttributes
}

// NewDeviceFromString creates device configuration from string.
func NewDeviceFromString(str string) (*DevConfig, error) {
b := []byte(str)
c := &DevConfig{}
Expand All @@ -107,6 +115,7 @@ func NewDeviceFromString(str string) (*DevConfig, error) {
return c, nil
}

// Dump exports device properties as YAML.
func (c *DevConfig) Dump() ([]byte, error) {
b, err := yaml.Marshal(c)
if err != nil {
Expand All @@ -115,11 +124,13 @@ func (c *DevConfig) Dump() ([]byte, error) {
return b, nil
}

// Config is full (global+devices) app configuration.
type Config struct {
Options AppConfig
Devices []DevConfig
}

// New creates new full app configuration.
func New() *Config {
return &Config{
Options: AppConfig{
Expand All @@ -133,6 +144,7 @@ func New() *Config {
}
}

// Load loads a Config from file.
func Load(path string, maxSize int64) (*Config, error) {
b, readErr := store.FileRead(path, maxSize)
if readErr != nil {
Expand All @@ -145,6 +157,7 @@ func Load(path string, maxSize int64) (*Config, error) {
return c, nil
}

// Dump exports device properties as YAML.
func (c *Config) Dump() ([]byte, error) {
b, err := yaml.Marshal(c)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions conf/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ import (
"sync"
)

// Options provides concurrency-safe access to AppConfig.
type Options struct {
options AppConfig
lock sync.RWMutex
}

// NewOptions creates a new set of options.
func NewOptions() *Options {
return &Options{}
}

// Get creates a copy of AppConfig.
func (o *Options) Get() *AppConfig {
o.lock.RLock()
defer o.lock.RUnlock()
opt := o.options // clone
return &opt
}

// Set updates the AppConfig from a copy.
func (o *Options) Set(c *AppConfig) {
o.lock.Lock()
defer o.lock.Unlock()
Expand Down
5 changes: 3 additions & 2 deletions dev/errlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"
)

// ErrlogPath builds the full pathname for errlog file.
func ErrlogPath(pathPrefix, id string) string {
dir := filepath.Dir(pathPrefix)
path := filepath.Join(dir, id) + ".errlog"
Expand All @@ -19,7 +20,7 @@ func errlog(logger hasPrintf, result FetchResult, pathPrefix string, debug bool,

now := time.Now()

path := ErrlogPath(pathPrefix, result.DevId)
path := ErrlogPath(pathPrefix, result.DevID)

f, openErr := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0640)
if openErr != nil {
Expand Down Expand Up @@ -58,7 +59,7 @@ func errlog(logger hasPrintf, result FetchResult, pathPrefix string, debug bool,
now.String(),
result.Code == fetchErrNone,
result.End.Sub(result.Begin),
result.Model, result.DevId, result.DevHostPort, result.Transport, result.Code, result.Msg)
result.Model, result.DevID, result.DevHostPort, result.Transport, result.Code, result.Msg)

if debug {
logger.Printf("errlog debug: push: '%s': [%s]", path, msg)
Expand Down
3 changes: 3 additions & 0 deletions dev/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"
)

// FilterTable stores line filters for custom line-by-line processing of configuration.
type FilterTable struct {
table map[string]FilterFunc
re1 *regexp.Regexp
Expand All @@ -13,8 +14,10 @@ type FilterTable struct {
re4 *regexp.Regexp
}

// FilterFunc is a helper function type for line filters.
type FilterFunc func(hasPrintf, bool, *FilterTable, []byte, int) []byte

// NewFilterTable creates a filter table.
func NewFilterTable(logger hasPrintf) *FilterTable {
t := &FilterTable{
table: map[string]FilterFunc{},
Expand Down
Loading

0 comments on commit a283e25

Please sign in to comment.