Skip to content

Commit

Permalink
Check config file syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
yudai committed Sep 2, 2015
1 parent 62f5d4a commit 83923b6
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 240 deletions.
4 changes: 0 additions & 4 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions Godeps/_workspace/src/github.com/fatih/camelcase/.travis.yml

This file was deleted.

20 changes: 0 additions & 20 deletions Godeps/_workspace/src/github.com/fatih/camelcase/LICENSE.md

This file was deleted.

40 changes: 0 additions & 40 deletions Godeps/_workspace/src/github.com/fatih/camelcase/README.md

This file was deleted.

91 changes: 0 additions & 91 deletions Godeps/_workspace/src/github.com/fatih/camelcase/camelcase.go

This file was deleted.

35 changes: 0 additions & 35 deletions Godeps/_workspace/src/github.com/fatih/camelcase/camelcase_test.go

This file was deleted.

64 changes: 18 additions & 46 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (

"github.com/braintree/manners"
"github.com/elazarl/go-bindata-assetfs"
"github.com/fatih/camelcase"
"github.com/fatih/structs"
"github.com/gorilla/websocket"
"github.com/hashicorp/hcl"
"github.com/kr/pty"
Expand All @@ -36,22 +34,22 @@ type App struct {
}

type Options struct {
Address string
Port string
PermitWrite bool
EnableBasicAuth bool
Credential string
EnableRandomUrl bool
RandomUrlLength int
IndexFile string
EnableTLS bool
TLSCrtFile string
TLSKeyFile string
TitleFormat string
EnableReconnect bool
ReconnectTime int
Once bool
Preferences map[string]interface{}
Address string `hcl:"address"`
Port string `hcl:"port"`
PermitWrite bool `hcl:"permit_write"`
EnableBasicAuth bool `hcl:"enable_basic_auth"`
Credential string `hcl:"credential"`
EnableRandomUrl bool `hcl:"enable_random_url"`
RandomUrlLength int `hcl:"random_url_length"`
IndexFile string `hcl:"index_file"`
EnableTLS bool `hcl:"enable_tls"`
TLSCrtFile string `hcl:"tls_crt_file"`
TLSKeyFile string `hcl:"tls_key_file"`
TitleFormat string `hcl:"title_format"`
EnableReconnect bool `hcl:"enable_reconnect"`
ReconnectTime int `hcl:"reconnect_time"`
Once bool `hcl:"once"`
Preferences map[string]interface{} `hcl:"preferences"`
}

var DefaultOptions = Options{
Expand Down Expand Up @@ -106,34 +104,8 @@ func ApplyConfigFile(options *Options, filePath string) error {
return err
}

config := make(map[string]interface{})
hcl.Decode(&config, string(fileString))
o := structs.New(options)
for _, name := range o.Names() {
configName := strings.ToLower(strings.Join(camelcase.Split(name), "_"))
if val, ok := config[configName]; ok {
field, ok := o.FieldOk(name)
if !ok {
return errors.New("No such option: " + name)
}

var err error
if name == "Preferences" {
prefs := val.([]map[string]interface{})[0]
htermPrefs := make(map[string]interface{})
for key, value := range prefs {
htermPrefs[strings.Replace(key, "_", "-", -1)] = value
}
err = field.Set(htermPrefs)
} else {
err = field.Set(val)
}

if err != nil {
return err
}

}
if err := hcl.Decode(options, string(fileString)); err != nil {
return err
}

return nil
Expand Down
6 changes: 5 additions & 1 deletion app/client_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ func (context *clientContext) sendInitialize() error {
}
writer.Close()

prefs, _ := json.Marshal(context.app.options.Preferences)
htermPrefs := make(map[string]interface{})
for key, value := range context.app.options.Preferences {
htermPrefs[strings.Replace(key, "_", "-", -1)] = value
}
prefs, _ := json.Marshal(htermPrefs)
context.connection.WriteMessage(
websocket.TextMessage,
append([]byte{SetPreferences}, prefs...),
Expand Down

0 comments on commit 83923b6

Please sign in to comment.