Check config file syntax

This commit is contained in:
Iwasaki Yudai 2015-09-01 20:28:45 -07:00
parent 62f5d4aaf3
commit 83923b6f39
8 changed files with 23 additions and 240 deletions

4
Godeps/Godeps.json generated
View File

@ -16,10 +16,6 @@
"ImportPath": "github.com/elazarl/go-bindata-assetfs", "ImportPath": "github.com/elazarl/go-bindata-assetfs",
"Rev": "d5cac425555ca5cf00694df246e04f05e6a55150" "Rev": "d5cac425555ca5cf00694df246e04f05e6a55150"
}, },
{
"ImportPath": "github.com/fatih/camelcase",
"Rev": "332844f2fb0193cce955f4687646abbdcc43ceeb"
},
{ {
"ImportPath": "github.com/fatih/structs", "ImportPath": "github.com/fatih/structs",
"Rev": "a9f7daa9c2729e97450c2da2feda19130a367d8f" "Rev": "a9f7daa9c2729e97450c2da2feda19130a367d8f"

View File

@ -1,3 +0,0 @@
language: go
go: 1.4

View File

@ -1,20 +0,0 @@
The MIT License (MIT)
Copyright (c) 2015 Fatih Arslan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -1,40 +0,0 @@
# CamelCase [![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/fatih/camelcase) [![Build Status](http://img.shields.io/travis/fatih/camelcase.svg?style=flat-square)](https://travis-ci.org/fatih/camelcase)
CamelCase is a Golang (Go) package to split the words of a camelcase type
string into a slice of words. It can be used to convert a camelcase word (lower
or upper case) into any type of word.
## Install
```bash
go get github.com/fatih/camelcase
```
## Usage and examples
```go
splitted := camelcase.Split("GolangPackage")
fmt.Println(splitted[0], splitted[1]) // prints: "Golang", "Package"
```
Both lower camel case and upper camel case are supported. For more info please
check: [http://en.wikipedia.org/wiki/CamelCase](http://en.wikipedia.org/wiki/CamelCase)
Below are some example cases:
```
lowercase => ["lowercase"]
Class => ["Class"]
MyClass => ["My", "Class"]
MyC => ["My", "C"]
HTML => ["HTML"]
PDFLoader => ["PDF", "Loader"]
AString => ["A", "String"]
SimpleXMLParser => ["Simple", "XML", "Parser"]
vimRPCPlugin => ["vim", "RPC", "Plugin"]
GL11Version => ["GL", "11", "Version"]
99Bottles => ["99", "Bottles"]
May5 => ["May", "5"]
BFG9000 => ["BFG", "9000"]
```

View File

@ -1,91 +0,0 @@
// Package camelcase is a micro package to split the words of a camelcase type
// string into a slice of words.
package camelcase
import "unicode"
// Split splits the camelcase word and returns a list of words. It also
// supports digits. Both lower camel case and upper camel case are supported.
// For more info please check: http://en.wikipedia.org/wiki/CamelCase
//
// Below are some example cases:
// lowercase => ["lowercase"]
// Class => ["Class"]
// MyClass => ["My", "Class"]
// MyC => ["My", "C"]
// HTML => ["HTML"]
// PDFLoader => ["PDF", "Loader"]
// AString => ["A", "String"]
// SimpleXMLParser => ["Simple", "XML", "Parser"]
// vimRPCPlugin => ["vim", "RPC", "Plugin"]
// GL11Version => ["GL", "11", "Version"]
// 99Bottles => ["99", "Bottles"]
// May5 => ["May", "5"]
// BFG9000 => ["BFG", "9000"]
func Split(src string) []string {
if src == "" {
return []string{}
}
splitIndex := []int{}
for i, r := range src {
// we don't care about first index
if i == 0 {
continue
}
// search till we find an upper case
if unicode.IsLower(r) {
continue
}
prevRune := rune(src[i-1])
// for cases like: GL11Version, BFG9000
if unicode.IsDigit(r) && !unicode.IsDigit(prevRune) {
splitIndex = append(splitIndex, i)
continue
}
if !unicode.IsDigit(r) && !unicode.IsUpper(prevRune) {
// for cases like: MyC
if i+1 == len(src) {
splitIndex = append(splitIndex, i)
continue
}
// for cases like: SimpleXMLParser, eclipseRCPExt
if unicode.IsUpper(rune(src[i+1])) {
splitIndex = append(splitIndex, i)
continue
}
}
// If the next char is lower case, we have found a split index
if i+1 != len(src) && unicode.IsLower(rune(src[i+1])) {
splitIndex = append(splitIndex, i)
}
}
// nothing to split, such as "hello", "Class", "HTML"
if len(splitIndex) == 0 {
return []string{src}
}
// now split the input string into pieces
splitted := make([]string, len(splitIndex)+1)
for i := 0; i < len(splitIndex)+1; i++ {
if i == 0 {
// first index
splitted[i] = src[:splitIndex[0]]
} else if i == len(splitIndex) {
// last index
splitted[i] = src[splitIndex[i-1]:]
} else {
// between first and last index
splitted[i] = src[splitIndex[i-1]:splitIndex[i]]
}
}
return splitted
}

View File

@ -1,35 +0,0 @@
package camelcase
import (
"reflect"
"testing"
)
func TestSplit(t *testing.T) {
var testCases = []struct {
input string
output []string
}{
{input: "", output: []string{}},
{input: "lowercase", output: []string{"lowercase"}},
{input: "Class", output: []string{"Class"}},
{input: "MyClass", output: []string{"My", "Class"}},
{input: "MyC", output: []string{"My", "C"}},
{input: "HTML", output: []string{"HTML"}},
{input: "PDFLoader", output: []string{"PDF", "Loader"}},
{input: "AString", output: []string{"A", "String"}},
{input: "SimpleXMLParser", output: []string{"Simple", "XML", "Parser"}},
{input: "vimRPCPlugin", output: []string{"vim", "RPC", "Plugin"}},
{input: "GL11Version", output: []string{"GL", "11", "Version"}},
{input: "99Bottles", output: []string{"99", "Bottles"}},
{input: "May5", output: []string{"May", "5"}},
{input: "BFG9000", output: []string{"BFG", "9000"}},
}
for _, c := range testCases {
res := Split(c.input)
if !reflect.DeepEqual(res, c.output) {
t.Errorf("input: '%s'\n\twant: %v\n\tgot : %v\n", c.input, c.output, res)
}
}
}

View File

@ -18,8 +18,6 @@ import (
"github.com/braintree/manners" "github.com/braintree/manners"
"github.com/elazarl/go-bindata-assetfs" "github.com/elazarl/go-bindata-assetfs"
"github.com/fatih/camelcase"
"github.com/fatih/structs"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/hashicorp/hcl" "github.com/hashicorp/hcl"
"github.com/kr/pty" "github.com/kr/pty"
@ -36,22 +34,22 @@ type App struct {
} }
type Options struct { type Options struct {
Address string Address string `hcl:"address"`
Port string Port string `hcl:"port"`
PermitWrite bool PermitWrite bool `hcl:"permit_write"`
EnableBasicAuth bool EnableBasicAuth bool `hcl:"enable_basic_auth"`
Credential string Credential string `hcl:"credential"`
EnableRandomUrl bool EnableRandomUrl bool `hcl:"enable_random_url"`
RandomUrlLength int RandomUrlLength int `hcl:"random_url_length"`
IndexFile string IndexFile string `hcl:"index_file"`
EnableTLS bool EnableTLS bool `hcl:"enable_tls"`
TLSCrtFile string TLSCrtFile string `hcl:"tls_crt_file"`
TLSKeyFile string TLSKeyFile string `hcl:"tls_key_file"`
TitleFormat string TitleFormat string `hcl:"title_format"`
EnableReconnect bool EnableReconnect bool `hcl:"enable_reconnect"`
ReconnectTime int ReconnectTime int `hcl:"reconnect_time"`
Once bool Once bool `hcl:"once"`
Preferences map[string]interface{} Preferences map[string]interface{} `hcl:"preferences"`
} }
var DefaultOptions = Options{ var DefaultOptions = Options{
@ -106,34 +104,8 @@ func ApplyConfigFile(options *Options, filePath string) error {
return err return err
} }
config := make(map[string]interface{}) if err := hcl.Decode(options, string(fileString)); err != nil {
hcl.Decode(&config, string(fileString)) return err
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
}
}
} }
return nil return nil

View File

@ -128,7 +128,11 @@ func (context *clientContext) sendInitialize() error {
} }
writer.Close() 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( context.connection.WriteMessage(
websocket.TextMessage, websocket.TextMessage,
append([]byte{SetPreferences}, prefs...), append([]byte{SetPreferences}, prefs...),