/* Write config property value from .gnvmrc file Param: - key: config property, include: registry noderoot latestversion globalversion - value: config property value */ func SetConfig(key string, value interface{}) string { if key == "registry" { if !strings.HasPrefix(value.(string), "http://") { P(WARING, "%v need %v", value.(string), "http://", "\n") value = "http://" + value.(string) } if !strings.HasSuffix(value.(string), "/") { value = value.(string) + "/" } reg, _ := regexp.Compile(`^https?:\/\/(w{3}\.)?([-a-zA-Z0-9.])+(\.[a-zA-Z]+)(:\d{1,4})?(\/)+`) if !reg.MatchString(value.(string)) { P(ERROR, "%v value %v must valid url.\n", "registry", value.(string)) return "" } } // set new value config.Set(key, value) // delete old config if err := os.Remove(configPath); err != nil { P(ERROR, "remove config file Error: %v\n", err.Error()) } // write new config if err := config.WriteConfigFile(configPath, 0777); err != nil { P(ERROR, "write config file Error: %v\n", err.Error()) } return value.(string) }
func writeConfig(path string, cfg Config) error { configFile := filepath.Join(path, "config.yaml") config.Set("id", cfg.Id) config.Set("hostname", cfg.Hostname) config.Set("disk", cfg.Disk) config.Set("cpu", cfg.Cpu) config.Set("memory", cfg.Memory) config.Set("dns", cfg.DNS) config.Set("docker", cfg.Docker) config.Set("extra", cfg.Extra) config.Set("route", cfg.Route) return config.WriteConfigFile(configFile, 0644) }
func SetConfig(key string, value interface{}) string { if key == "registry" { if !strings.HasPrefix(value.(string), "http://") { P(WARING, "%v need %v", value.(string), "http://", "\n") value = "http://" + value.(string) } reg := regexp.MustCompile(`(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?`) switch { case !reg.MatchString(value.(string)): P(ERROR, "registry value %v must url valid.\n", value.(string)) return "" case !strings.HasSuffix(value.(string), "/"): value = value.(string) + "/" } } // set new value config.Set(key, value) // delete old config if err := os.Remove(configPath); err != nil { P(ERROR, "remove config file Error: %v\n", err.Error()) } // write new config if err := config.WriteConfigFile(configPath, 0777); err != nil { P(ERROR, "write config file Error: %v\n", err.Error()) } return value.(string) }