This commit is contained in:
geek 2017-04-14 14:27:38 +09:00
parent 1ddc196741
commit c369c7eac9

View File

@ -13,10 +13,10 @@ const (
OVERFLOW_CONFIG_PATH = "/Overflow/Config" OVERFLOW_CONFIG_PATH = "/Overflow/Config"
) )
type ConfigManager struct { type ConfigManager struct {
idMap map[string]string
} }
func (config *ConfigManager) readByTempConfig(c *config.Config) (error) { func (c *ConfigManager) readByTempConfig(cn *config.Config) (error) {
pwd, err := os.Getwd() pwd, err := os.Getwd()
if err != nil { if err != nil {
@ -24,38 +24,63 @@ func (config *ConfigManager) readByTempConfig(c *config.Config) (error) {
} }
b, err := ioutil.ReadFile(pwd+"/example.json") b, err := ioutil.ReadFile(pwd+"/example.json")
err = json.Unmarshal(b,&c) err = json.Unmarshal(b,&cn)
return err return err
} }
func (config *ConfigManager) writeConfig(c config.Config) (error){ func (c *ConfigManager) writeConfig(cn config.Config) (error){
var fileName = c.Id var fileName = cn.Id
fileJson, _ := json.Marshal(c) fileJson, _ := json.Marshal(cn)
path, _ := config.getFilePath(c) path, _ := c.getFilePath(cn)
err := ioutil.WriteFile(path + "/" + fileName + ".json", fileJson, 0644) err := ioutil.WriteFile(path + "/" + fileName + ".json", fileJson, 0644)
c.idMap[cn.Id] = path + "/"
return err return err
} }
func (config *ConfigManager) getFilePath(c config.Config) (string, error){ func (c *ConfigManager) getFilePath(cn config.Config) (string, error){
var parentPath string = strings.Join(configSettingFolder,"") var parentPath string = strings.Join(configSettingFolder,"")
cctmp := c.Crawler.Container cctmp := cn.Crawler.Container
cctmps := strings.Split(cctmp,"_") cctmps := strings.Split(cctmp,"_")
containerPath := cctmps[0] containerPath := cctmps[0]
cns := strings.Split(c.Crawler.Name, "_") cns := strings.Split(cn.Crawler.Name, "_")
cn := cns[0] cnf := cns[0]
rs := parentPath + OVERFLOW_CONFIG_PATH +"/"+containerPath+"/"+cn rs := parentPath + OVERFLOW_CONFIG_PATH +"/"+containerPath+"/"+cnf
err := checkFolder(rs) err := checkFolder(rs)
return rs, err return rs, err
} }
func (c *ConfigManager) AddConfig(b []byte) (error) {
var err error
cn := config.Config{}
if len(b) == 0 || b == nil {
err = c.readByTempConfig(&cn)
} else {
err = json.Unmarshal(b, &cn)
}
if err != nil {
log.Fatal(err)
}
c.writeConfig(cn)
return err
}
func (c *ConfigManager) GetConfigById(id string) (string) {
return c.idMap[id]
}
func checkFolder(path string) (error) { func checkFolder(path string) (error) {
_, err := os.Stat(path) _, err := os.Stat(path)
@ -65,9 +90,17 @@ func checkFolder(path string) (error) {
} }
if os.IsNotExist(err) { if os.IsNotExist(err) {
err = nil
os.MkdirAll(path, os.ModePerm) os.MkdirAll(path, os.ModePerm)
} }
return nil return err
}
func NewConfigManager() *ConfigManager {
var c ConfigManager
c.idMap = make(map[string]string)
return &c
} }
func Start(b []byte) { func Start(b []byte) {
@ -77,6 +110,7 @@ func Start(b []byte) {
var err error var err error
cm := ConfigManager{} cm := ConfigManager{}
c := config.Config{} c := config.Config{}
cm.idMap = make(map[string]string)
if len(b) == 0 || b == nil { if len(b) == 0 || b == nil {
err = cm.readByTempConfig(&c) err = cm.readByTempConfig(&c)