Commit 6d5e52db authored by ibuler's avatar ibuler

[Update] 暂存

parent 60328525
......@@ -6,6 +6,7 @@ import (
"io/ioutil"
"log"
"os"
"reflect"
"strings"
"sync"
)
......@@ -41,7 +42,7 @@ type Config struct {
Language string `yaml:"LANG"`
LanguageCode string `yaml:"LANGUAGE_CODE"` // Abandon
Mux sync.RWMutex
mu sync.RWMutex
}
func (c *Config) EnsureConfigValid() {
......@@ -56,8 +57,8 @@ func (c *Config) EnsureConfigValid() {
}
func (c *Config) LoadFromYAML(body []byte) error {
c.Mux.Lock()
defer c.Mux.Unlock()
c.mu.Lock()
defer c.mu.Unlock()
err := yaml.Unmarshal(body, c)
if err != nil {
log.Printf("Load yaml error: %v", err)
......@@ -74,8 +75,8 @@ func (c *Config) LoadFromYAMLPath(filepath string) error {
}
func (c *Config) LoadFromJSON(body []byte) error {
c.Mux.Lock()
defer c.Mux.Unlock()
c.mu.Lock()
defer c.mu.Unlock()
err := json.Unmarshal(body, c)
if err != nil {
log.Printf("Config load yaml error")
......@@ -97,6 +98,44 @@ func (c *Config) LoadFromEnv() error {
return c.LoadFromYAML(envYAML)
}
func (c *Config) Get(key string) reflect.Value {
c.mu.Lock()
defer c.mu.Unlock()
//t := reflect.TypeOf(c)
v := reflect.ValueOf(c)
return v.FieldByName(key)
//for i := 0; i < v.NumField(); i++ {
// if v.Field(i).CanInterface() && t.Field(i).Name == key {
// return v.Field(i).Interface()
// }
//}
//return nil
}
func (c *Config) GetString(key string) string {
value := c.Get(key)
return value.String()
}
func (c *Config) GetBool(key string) bool {
value := c.Get(key)
return value.Bool()
}
func (c *Config) GetInt(key string) int {
value := c.Get(key)
return int(value.Int())
}
func (c *Config) GetMap(key string) map[string]string {
//value := c.Get(key)
//
//if val, ok := value.(map[string]string); ok {
// return val
//}
return map[string]string{}
}
func (c *Config) Load(filepath string) error {
err := c.LoadFromYAMLPath(filepath)
if err != nil {
......
......@@ -2,12 +2,13 @@ package proxy
import (
"bytes"
"cocogo/pkg/utils"
"regexp"
"strings"
"cocogo/pkg/utils"
)
var ps1Pattern, _ = regexp.Compile("^\\[?.*@.*\\]?[\\$#]\\s|mysql>\\s")
var ps1Pattern = regexp.MustCompile(`^\[?.*@.*\]?[\\$#]\s|mysql>\s`)
func NewCmdParser() *CmdParser {
parser := &CmdParser{}
......
......@@ -77,15 +77,15 @@ func MustLoadServerConfigOnce() {
func LoadConfigFromServer() (err error) {
conf := config.Conf
conf.Mux.Lock()
defer conf.Mux.Unlock()
conf.mu.Lock()
defer conf.mu.Unlock()
err = authClient.Get(TerminalConfigURL, conf)
return err
}
func KeepSyncConfigWithServer() {
for {
logger.Debug("Sync config from server")
logger.Debug("Sync config with server")
err := LoadConfigFromServer()
if err != nil {
logger.Warn("Sync config with server error: ", err)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment