diff --git a/config.go b/config.go index ada70eb..54a06be 100644 --- a/config.go +++ b/config.go @@ -13,10 +13,10 @@ const ( OVERFLOW_CONFIG_PATH = "/Overflow/Config" ) 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() if err != nil { @@ -24,38 +24,63 @@ func (config *ConfigManager) readByTempConfig(c *config.Config) (error) { } b, err := ioutil.ReadFile(pwd+"/example.json") - err = json.Unmarshal(b,&c) + err = json.Unmarshal(b,&cn) return err } -func (config *ConfigManager) writeConfig(c config.Config) (error){ - var fileName = c.Id - fileJson, _ := json.Marshal(c) - path, _ := config.getFilePath(c) +func (c *ConfigManager) writeConfig(cn config.Config) (error){ + var fileName = cn.Id + fileJson, _ := json.Marshal(cn) + path, _ := c.getFilePath(cn) err := ioutil.WriteFile(path + "/" + fileName + ".json", fileJson, 0644) + c.idMap[cn.Id] = path + "/" + 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,"") - cctmp := c.Crawler.Container + cctmp := cn.Crawler.Container cctmps := strings.Split(cctmp,"_") containerPath := cctmps[0] - cns := strings.Split(c.Crawler.Name, "_") - cn := cns[0] + cns := strings.Split(cn.Crawler.Name, "_") + cnf := cns[0] - rs := parentPath + OVERFLOW_CONFIG_PATH +"/"+containerPath+"/"+cn + rs := parentPath + OVERFLOW_CONFIG_PATH +"/"+containerPath+"/"+cnf err := checkFolder(rs) 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) { _, err := os.Stat(path) @@ -65,9 +90,17 @@ func checkFolder(path string) (error) { } if os.IsNotExist(err) { + err = nil 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) { @@ -77,6 +110,7 @@ func Start(b []byte) { var err error cm := ConfigManager{} c := config.Config{} + cm.idMap = make(map[string]string) if len(b) == 0 || b == nil { err = cm.readByTempConfig(&c)