func TestStringVal(t *testing.T) { props := func(s string) *properties.Properties { p, err := properties.Load([]byte(s), properties.UTF8) if err != nil { t.Fatal(err) } return p } tests := []struct { env map[string]string props *properties.Properties keys []string val string def string }{ { env: nil, props: nil, keys: []string{"key"}, val: "default", def: "default", }, { env: map[string]string{"key": "env"}, props: nil, keys: []string{"key"}, val: "env", }, { env: nil, props: props("key=props"), keys: []string{"key"}, val: "props", }, { env: map[string]string{"key": "env"}, props: props("key=props"), keys: []string{"key"}, val: "env", }, { env: map[string]string{"key": "env"}, props: props("other=props"), keys: []string{"other"}, val: "props", }, { env: map[string]string{"key": "env"}, props: props("other=props"), keys: []string{"key", "other"}, val: "env", }, } for i, tt := range tests { for k, v := range tt.env { os.Setenv(k, v) } if got, want := stringVal(tt.props, tt.def, tt.keys...), tt.val; got != want { t.Errorf("%d: got %s want %s", i, got, want) } for k := range tt.env { os.Unsetenv(k) } } }
func marshallConfigReader(in io.Reader, c map[string]interface{}, configType string) error { buf := new(bytes.Buffer) buf.ReadFrom(in) switch strings.ToLower(configType) { case "yaml", "yml": if err := yaml.Unmarshal(buf.Bytes(), &c); err != nil { return ConfigParseError{err} } case "json": if err := json.Unmarshal(buf.Bytes(), &c); err != nil { return ConfigParseError{err} } case "toml": if _, err := toml.Decode(buf.String(), &c); err != nil { return ConfigParseError{err} } case "properties", "props", "prop": var p *properties.Properties var err error if p, err = properties.Load(buf.Bytes(), properties.UTF8); err != nil { return ConfigParseError{err} } for _, key := range p.Keys() { value, _ := p.Get(key) c[key] = value } } insensitiviseMap(c) return nil }
func loadRemoteComponents() *properties.Properties { var configFile string if runtime.GOOS == "windows" { configFile = "win" } else if runtime.GOOS == "linux" && ARCH_BITS == 32 { configFile = "lnx-32" } else if runtime.GOOS == "linux" && ARCH_BITS == 64 { configFile = "lnx-64" } else { panic("Platform not supported") } body, err := fetchURLContent("http://install-versions.risevision.com/remote-components-" + configFile + ".cfg") if err != nil { fmt.Println("Error loading remote components file") } props, err := properties.Load(body, properties.UTF8) if err != nil { fmt.Println("Error reading remote components file") } return props }
func Config() *properties.Properties { if configProperties == nil { cfg := defaultConfig cfgPath, err := filepath.Abs(ConfigPath) if err != nil { panic(err) } if cfgFile, err := ioutil.ReadFile(cfgPath); os.IsNotExist(err) { // pass } else if err != nil { panic(err) } else { cfg = append(cfg, cfgFile...) } if props, err := properties.Load(cfg, properties.UTF8); err != nil { panic(err) } else { for k, v := range ConfigOverrides { if _, _, err := props.Set(k, v); err != nil { panic(err) } } configPath = cfgPath configProperties = props } } return configProperties }
func marshallConfigReader(in io.Reader, c map[string]interface{}, configType string) { buf := new(bytes.Buffer) buf.ReadFrom(in) switch configType { case "yaml", "yml": if err := yaml.Unmarshal(buf.Bytes(), &c); err != nil { jww.ERROR.Fatalf("Error parsing config: %s", err) } case "json": if err := json.Unmarshal(buf.Bytes(), &c); err != nil { jww.ERROR.Fatalf("Error parsing config: %s", err) } case "toml": if _, err := toml.Decode(buf.String(), &c); err != nil { jww.ERROR.Fatalf("Error parsing config: %s", err) } case "properties", "props", "prop": var p *properties.Properties var err error if p, err = properties.Load(buf.Bytes(), properties.UTF8); err != nil { jww.ERROR.Fatalf("Error parsing config: %s", err) } for _, key := range p.Keys() { value, _ := p.Get(key) c[key] = value } } insensitiviseMap(c) }
func unmarshallConfigReader(in io.Reader, c map[string]interface{}, configType string) error { buf := new(bytes.Buffer) buf.ReadFrom(in) switch strings.ToLower(configType) { case "yaml", "yml": if err := yaml.Unmarshal(buf.Bytes(), &c); err != nil { return ConfigParseError{err} } case "json": if err := json.Unmarshal(buf.Bytes(), &c); err != nil { return ConfigParseError{err} } case "hcl": obj, err := hcl.Parse(string(buf.Bytes())) if err != nil { return ConfigParseError{err} } if err = hcl.DecodeObject(&c, obj); err != nil { return ConfigParseError{err} } case "toml": tree, err := toml.LoadReader(buf) if err != nil { return ConfigParseError{err} } tmap := tree.ToMap() for k, v := range tmap { c[k] = v } case "properties", "props", "prop": var p *properties.Properties var err error if p, err = properties.Load(buf.Bytes(), properties.UTF8); err != nil { return ConfigParseError{err} } for _, key := range p.Keys() { value, _ := p.Get(key) // recursively build nested maps path := strings.Split(key, ".") lastKey := strings.ToLower(path[len(path)-1]) deepestMap := deepSearch(c, path[0:len(path)-1]) // set innermost value deepestMap[lastKey] = value } } insensitiviseMap(c) return nil }
func (propertiesParser *PropertiesParser) Parse(content []byte, c map[string]interface{}) error { var p *properties.Properties p, err := properties.Load(content, properties.UTF8) if err != nil { return err } for _, key := range p.Keys() { value, _ := p.Get(key) c[key] = value } return err }
func unmarshallConfigReader(in io.Reader, c map[string]interface{}, configType string) error { buf := new(bytes.Buffer) buf.ReadFrom(in) switch strings.ToLower(configType) { case "yaml", "yml": if err := yaml.Unmarshal(buf.Bytes(), &c); err != nil { return ConfigParseError{err} } case "json": if err := json.Unmarshal(buf.Bytes(), &c); err != nil { return ConfigParseError{err} } case "hcl": obj, err := hcl.Parse(string(buf.Bytes())) if err != nil { return ConfigParseError{err} } if err = hcl.DecodeObject(&c, obj); err != nil { return ConfigParseError{err} } case "toml": tree, err := toml.LoadReader(buf) if err != nil { return ConfigParseError{err} } tmap := tree.ToMap() for k, v := range tmap { c[k] = v } case "properties", "props", "prop": var p *properties.Properties var err error if p, err = properties.Load(buf.Bytes(), properties.UTF8); err != nil { return ConfigParseError{err} } for _, key := range p.Keys() { value, _ := p.Get(key) c[key] = value } } insensitiviseMap(c) return nil }
func unmarshallConfigReader(in io.Reader, c map[string]interface{}, configType string) error { buf := new(bytes.Buffer) buf.ReadFrom(in) switch strings.ToLower(configType) { case "yaml", "yml": if err := yaml.Unmarshal(buf.Bytes(), &c); err != nil { return ConfigParseError{err} } case "json": if err := json.Unmarshal(buf.Bytes(), &c); err != nil { return ConfigParseError{err} } case "hcl": obj, err := hcl.Parse(string(buf.Bytes())) if err != nil { return ConfigParseError{err} } if err = hcl.DecodeObject(&c, obj); err != nil { return ConfigParseError{err} } case "toml": if _, err := toml.Decode(buf.String(), &c); err != nil { return ConfigParseError{err} } case "properties", "props", "prop": var p *properties.Properties var err error if p, err = properties.Load(buf.Bytes(), properties.UTF8); err != nil { return ConfigParseError{err} } if err = p.Decode(&c); err != nil { return ConfigParseError{err} } } insensitiviseMap(c) return nil }
func TestFromProperties(t *testing.T) { in := ` proxy.addr = :1234 proxy.localip = 4.4.4.4 proxy.strategy = rr proxy.shutdownwait = 500ms proxy.timeout = 3s proxy.dialtimeout = 60s proxy.readtimeout = 5s proxy.writetimeout = 10s proxy.maxconn = 666 proxy.header.clientip = clientip proxy.header.tls = tls proxy.header.tls.value = tls-true registry.backend = something registry.file.path = /foo/bar registry.static.routes = route add svc / http://127.0.0.1:6666/ registry.consul.addr = 1.2.3.4:5678 registry.consul.token = consul-token registry.consul.kvpath = /some/path registry.consul.tagprefix = p- registry.consul.register.addr = 6.6.6.6:7777 registry.consul.register.name = fab registry.consul.register.checkInterval = 5s registry.consul.register.checkTimeout = 10s metrics.target = graphite metrics.prefix = someprefix metrics.interval = 5s metrics.graphite.addr = 5.6.7.8:9999 runtime.gogc = 666 runtime.gomaxprocs = 12 ui.addr = 7.8.9.0:1234 ui.color = fonzy ui.title = fabfab ` out := &Config{ Proxy: Proxy{ MaxConn: 666, LocalIP: "4.4.4.4", Strategy: "rr", ShutdownWait: 500 * time.Millisecond, DialTimeout: 60 * time.Second, KeepAliveTimeout: 3 * time.Second, ResponseHeaderTimeout: 3 * time.Second, ClientIPHeader: "clientip", TLSHeader: "tls", TLSHeaderValue: "tls-true", }, Registry: Registry{ Backend: "something", File: File{ Path: "/foo/bar", }, Static: Static{ Routes: "route add svc / http://127.0.0.1:6666/", }, Consul: Consul{ Addr: "1.2.3.4:5678", Token: "consul-token", KVPath: "/some/path", TagPrefix: "p-", ServiceAddr: "6.6.6.6:7777", ServiceName: "fab", CheckInterval: 5 * time.Second, CheckTimeout: 10 * time.Second, }, }, Listen: []Listen{ { Addr: ":1234", ReadTimeout: 5 * time.Second, WriteTimeout: 10 * time.Second, }, }, Metrics: []Metrics{ { Target: "graphite", Prefix: "someprefix", Interval: 5 * time.Second, Addr: "5.6.7.8:9999", }, }, Runtime: Runtime{ GOGC: 666, GOMAXPROCS: 12, }, UI: UI{ Addr: "7.8.9.0:1234", Color: "fonzy", Title: "fabfab", }, } p, err := properties.Load([]byte(in), properties.UTF8) if err != nil { t.Fatalf("got %v want nil", err) } cfg, err := fromProperties(p) if err != nil { t.Fatalf("got %v want nil", err) } got, want := cfg, out verify.Values(t, "cfg", got, want) }
func TestFromProperties(t *testing.T) { in := ` proxy.cs = cs=name;type=path;cert=foo;clientca=bar;refresh=99s;hdr=a: b;caupgcn=furb proxy.addr = :1234 proxy.localip = 4.4.4.4 proxy.strategy = rr proxy.matcher = prefix proxy.noroutestatus = 929 proxy.shutdownwait = 500ms proxy.responseheadertimeout = 3s proxy.keepalivetimeout = 4s proxy.dialtimeout = 60s proxy.readtimeout = 5s proxy.writetimeout = 10s proxy.maxconn = 666 proxy.header.clientip = clientip proxy.header.tls = tls proxy.header.tls.value = tls-true registry.backend = something registry.file.path = /foo/bar registry.static.routes = route add svc / http://127.0.0.1:6666/ registry.consul.addr = https://1.2.3.4:5678 registry.consul.token = consul-token registry.consul.kvpath = /some/path registry.consul.tagprefix = p- registry.consul.register.enabled = false registry.consul.register.addr = 6.6.6.6:7777 registry.consul.register.name = fab registry.consul.register.tags = a, b, c , registry.consul.register.checkInterval = 5s registry.consul.register.checkTimeout = 10s registry.consul.service.status = a,b metrics.target = graphite metrics.prefix = someprefix metrics.interval = 5s metrics.graphite.addr = 5.6.7.8:9999 runtime.gogc = 666 runtime.gomaxprocs = 12 ui.addr = 7.8.9.0:1234 ui.color = fonzy ui.title = fabfab aws.apigw.cert.cn = furb ` out := &Config{ ListenerValue: []string{":1234"}, CertSourcesValue: []map[string]string{{"cs": "name", "type": "path", "cert": "foo", "clientca": "bar", "refresh": "99s", "hdr": "a: b", "caupgcn": "furb"}}, CertSources: map[string]CertSource{ "name": CertSource{ Name: "name", Type: "path", CertPath: "foo", ClientCAPath: "bar", CAUpgradeCN: "furb", Refresh: 99 * time.Second, Header: http.Header{"A": []string{"b"}}, }, }, Proxy: Proxy{ MaxConn: 666, LocalIP: "4.4.4.4", Strategy: "rr", Matcher: "prefix", NoRouteStatus: 929, ShutdownWait: 500 * time.Millisecond, DialTimeout: 60 * time.Second, ResponseHeaderTimeout: 3 * time.Second, KeepAliveTimeout: 4 * time.Second, ReadTimeout: 5 * time.Second, WriteTimeout: 10 * time.Second, ClientIPHeader: "clientip", TLSHeader: "tls", TLSHeaderValue: "tls-true", }, Registry: Registry{ Backend: "something", File: File{ Path: "/foo/bar", }, Static: Static{ Routes: "route add svc / http://127.0.0.1:6666/", }, Consul: Consul{ Addr: "1.2.3.4:5678", Scheme: "https", Token: "consul-token", KVPath: "/some/path", TagPrefix: "p-", Register: false, ServiceAddr: "6.6.6.6:7777", ServiceName: "fab", ServiceTags: []string{"a", "b", "c"}, ServiceStatus: []string{"a", "b"}, CheckInterval: 5 * time.Second, CheckTimeout: 10 * time.Second, }, }, Listen: []Listen{ { Addr: ":1234", Scheme: "http", ReadTimeout: 5 * time.Second, WriteTimeout: 10 * time.Second, }, }, Metrics: Metrics{ Target: "graphite", Prefix: "someprefix", Interval: 5 * time.Second, GraphiteAddr: "5.6.7.8:9999", }, Runtime: Runtime{ GOGC: 666, GOMAXPROCS: 12, }, UI: UI{ Addr: "7.8.9.0:1234", Color: "fonzy", Title: "fabfab", }, } p, err := properties.Load([]byte(in), properties.UTF8) if err != nil { t.Fatalf("got %v want nil", err) } cfg, err := load(p) if err != nil { t.Fatalf("got %v want nil", err) } got, want := cfg, out verify.Values(t, "cfg", got, want) }