func getAttributes(keys []string, overwriteAttributes map[string]interface{}) (map[string]interface{}, error) { var attributes map[string]interface{} var c *api.Client = util.Consul() attributes = make(map[string]interface{}) // Get attributes from consul KVS for _, key := range keys { list, _, err := c.KV().List(key, &api.QueryOptions{}) if err != nil { return nil, err } for _, kv := range list { var a map[string]interface{} if err := json.Unmarshal(kv.Value, &a); err != nil { return nil, err } if err := mergo.MergeWithOverwrite(&attributes, a); err != nil { return nil, errors.New(fmt.Sprintf("Failed to merge attributes(%v)", err)) } } } // Overwrite some attributes by specified parameter in task.yml if err := mergo.MergeWithOverwrite(&attributes, overwriteAttributes); err != nil { return nil, err } return attributes, nil }
func loadConfigFile(o *Options) { isDefault := false configPath := o.ConfigFile if o.ConfigFile == "" { isDefault = true configPath = "./deploy.yaml" } data, err := ioutil.ReadFile(configPath) if err != nil { if os.IsNotExist(err) && isDefault { return } panic(err) } var file ConfigFile err = yaml.Unmarshal(data, &file) panicIf(err) var envCfg Options if o.Env != "" { var ok bool envCfg, ok = file[o.Env] if !ok { panic("Config for specified env not found") } } defCfg, _ := file["default"] panicIf(mergo.MergeWithOverwrite(o, defCfg)) panicIf(mergo.MergeWithOverwrite(o, envCfg)) }
// Load loads the configuration from a yaml file func (c *Config) Load(file string) error { data, err := ioutil.ReadFile(file) if err != nil { log.Printf("Unable to ReadConfig File: %s -> %s", file, err.Error()) return err } conf := Config{} err = yaml.Unmarshal(data, &conf) if err != nil { log.Printf("Unable to load Config File: %s -> %s", file, err.Error()) return err } if err := mergo.MergeWithOverwrite(c, conf); err != nil { return err } c.Env = strings.TrimSpace(c.Env) env := strings.ToLower(c.Env) if !strings.Contains(env, "dev") && !strings.Contains(env, "development") && !strings.Contains(env, "debug") { c.Mode = 1 } return nil }
func (s *SystemTestSuite) TestSetGetConfigSuccess(c *C) { // read current config, to prepare the test config with just ansible changes cmdStr := `clusterctl config get --json` out, err := s.tbn1.RunCommandWithOutput(cmdStr) s.Assert(c, err, IsNil, Commentf("output: %s", out)) var config map[string]interface{} err = json.Unmarshal([]byte(out), &config) s.Assert(c, err, IsNil) var testConfig map[string]interface{} err = json.Unmarshal([]byte(`{ "ansible": { "playbook_location": "foo" } }`), &testConfig) s.Assert(c, err, IsNil) err = mergo.MergeWithOverwrite(&config, &testConfig) s.Assert(c, err, IsNil) configStr, err := json.Marshal(config) s.Assert(c, err, IsNil) c.Logf("config: %+v", config) c.Logf("json: %q", configStr) cmdStr = fmt.Sprintf(`clusterctl config set - <<EOF %s EOF`, configStr) out, err = s.tbn1.RunCommandWithOutput(cmdStr) s.Assert(c, err, IsNil, Commentf("output: %s", out)) cmdStr = `clusterctl config get --json` out, err = s.tbn1.RunCommandWithOutput(cmdStr) s.Assert(c, err, IsNil, Commentf("output: %s", out)) exptdOut := `.*"playbook_location":.*"foo".*` s.assertMatch(c, exptdOut, out) }
func (p *Player) AddDebt(d Debt) error { newDebt := new(Debt) if err := mergo.MergeWithOverwrite(newDebt, d); err != nil { return errors.New(err.Error() + " - Could not set Debt data") } newDebt.UUID, _ = uuid.V4() if d.Created.IsZero() { newDebt.Created = time.Now() } else { newDebt.Created = d.Created } if !d.Settled.IsZero() { newDebt.Settled = d.Settled } newDebt.Debitor = p.UUID p.Debts = append(p.Debts, *newDebt) err := storage.Store(p) if err != nil { return errors.New("Could not add debt") } eventqueue.Publish(utils.CKPTEvent{ Type: utils.PLAYER_EVENT, RestrictedTo: []uuid.UUID{p.UUID}, Subject: "Gjeld registrert", Message: "Det er registrert et nytt gjeldskrav mot deg på ckpt.no!"}) return nil }
func (p *Player) SetVotes(v Votes) error { if err := mergo.MergeWithOverwrite(&p.Votes, v); err != nil { return errors.New(err.Error() + " - Could not set Votes data") } err := storage.Store(p) if err != nil { return errors.New("Could not set votes") } return nil }
func (l *Location) UpdateProfile(lp Profile) error { if err := mergo.MergeWithOverwrite(&l.Profile, lp); err != nil { return errors.New(err.Error() + " - Could not update location profile") } err := storage.Store(l) if err != nil { return errors.New(err.Error() + " - Could not store updated location profile") } return nil }
func (c *Catering) UpdateInfo(ci Info) error { if err := mergo.MergeWithOverwrite(&c.Info, ci); err != nil { return errors.New(err.Error() + " - Could not update catering info") } err := storage.Store(c) if err != nil { return errors.New(err.Error() + " - Could not store updated catering info") } return nil }
// Create a Catering func NewCatering(tournament uuid.UUID, ci Info) (*Catering, error) { c := new(Catering) c.UUID, _ = uuid.V4() c.Tournament = tournament if err := mergo.MergeWithOverwrite(&c.Info, ci); err != nil { return nil, errors.New(err.Error() + " - Could not set initial catering info") } if err := storage.Store(c); err != nil { return nil, errors.New(err.Error() + " - Could not write catering to storage") } return c, nil }
// Create a Location func NewLocation(host uuid.UUID, lp Profile) (*Location, error) { l := new(Location) l.UUID, _ = uuid.V4() l.Active = true l.Host = host if err := mergo.MergeWithOverwrite(&l.Profile, lp); err != nil { return nil, errors.New(err.Error() + " - Could not set initial location profile") } if err := storage.Store(l); err != nil { return nil, errors.New(err.Error() + " - Could not write location to storage") } return l, nil }
// Load loads the configuration from a yaml file func (c *BuildConfig) Load(file string) error { data, err := ioutil.ReadFile(file) if err != nil { log.Printf("Unable to ReadConfig File: %s -> %s", file, err.Error()) return err } conf := BuildConfig{} err = yaml.Unmarshal(data, &conf) if err != nil { log.Printf("Unable to load Config File: %s -> %s", file, err.Error()) return err } if conf.Package == "" { return errors.New("package option can not be empty, provide the project package name please") } if err := mergo.MergeWithOverwrite(c, conf); err != nil { return err } if mano, err := strconv.ParseBool(c.UseMain); err == nil { c.GoMain = mano } if vbo, err := strconv.ParseBool(c.Client.Verbose); err == nil { c.Client.UseVerbose = vbo } if doge, err := strconv.ParseBool(c.DoGoGet); err == nil { c.Goget = doge } c.ClientPackage = filepath.Join(c.Package, "client") if strings.Contains(c.ClientPackage, `\`) { c.ClientPackage = filepath.ToSlash(c.ClientPackage) } env := strings.ToLower(c.Env) if !strings.Contains(env, "dev") && !strings.Contains(env, "development") && !strings.Contains(env, "debug") { c.Mode = 1 } return nil }
// Shell creates an interactive Docker session to the specified service // starting it if not currently running func (c *DockerCompose) Shell(config parity.ShellConfig) (err error) { log.Stage("Interactive Shell") log.Debug("Compose - starting docker compose services") mergedConfig := *parity.DEFAULT_INTERACTIVE_SHELL_OPTIONS mergo.MergeWithOverwrite(&mergedConfig, &config) // Check if services running // TODO: if running, attach to running container // if NOT running, start services and attach if c.project != nil { log.Step("Starting compose services") injectDisplayEnvironmentVariables(c.project) } container := fmt.Sprintf("parity-%s_%s_1", c.pluginConfig.ProjectNameSafe, mergedConfig.Service) client := utils.DockerClient() createExecOptions := dockerclient.CreateExecOptions{ AttachStdin: true, AttachStdout: true, AttachStderr: true, Tty: true, Cmd: mergedConfig.Command, User: mergedConfig.User, Container: container, } startExecOptions := dockerclient.StartExecOptions{ Detach: false, Tty: true, InputStream: os.Stdin, OutputStream: os.Stdout, ErrorStream: os.Stderr, RawTerminal: true, } log.Step("Attaching to container '%s'", container) if id, err := client.CreateExec(createExecOptions); err == nil { client.StartExec(id.ID, startExecOptions) } else { log.Error("error: %v", err.Error()) } log.Debug("Docker Compose Run() finished") return err }
func (c *Config) ApplyDefaults() { // config := defaultConfig setDebug := !c.Debug var shouldDebug bool if setDebug { shouldDebug = c.Debug } mergo.MergeWithOverwrite(c, defaultConfig) if setDebug { c.Debug = shouldDebug } }
func (c *NewsItem) UpdateNewsItem(ci NewsItem) error { d := new(NewsItem) *d = *c if err := mergo.MergeWithOverwrite(c, ci); err != nil { return errors.New(err.Error() + " - Could not update NewsItem info") } c.UUID = d.UUID c.Author = d.Author c.Created = d.Created c.Tag = d.Tag err := storage.Store(c) if err != nil { return errors.New(err.Error() + " - Could not store updated NewsItem info") } return nil }
// Create a NewsItem func NewNewsItem(itemdata NewsItem, author uuid.UUID) (*NewsItem, error) { c := new(NewsItem) if err := mergo.MergeWithOverwrite(c, itemdata); err != nil { return nil, errors.New(err.Error() + " - Could not set initial NewsItem data") } c.UUID, _ = uuid.V4() c.Author = author c.Created = time.Now() if err := storage.Store(c); err != nil { return nil, errors.New(err.Error() + " - Could not write NewsItem to storage") } eventqueue.Publish(utils.CKPTEvent{ Type: utils.NEWS_EVENT, Subject: "Nytt bidrag lagt ut", Message: "Det er lagt ut et nytt bidrag på ckpt.no!"}) return c, nil }
// Create a Tournament func NewTournament(tdata Info) (*Tournament, error) { if err := validateTournamentInfo(tdata); err != nil { return nil, errors.New(err.Error() + " - Could not create tournament") } t := new(Tournament) t.UUID, _ = uuid.V4() if err := mergo.MergeWithOverwrite(&t.Info, tdata); err != nil { return nil, errors.New(err.Error() + " - Could not set initial tournament data") } // Merge seems to not handle time.Time for some reason, thus fixup fixupTournamentInfo(&t.Info, tdata) if err := storage.Store(t); err != nil { return nil, errors.New(err.Error() + " - Could not write tournament to storage") } return t, nil }
// LoadJSON loads the configuration from a yaml file func (t *TemplateConfig) LoadJSON(file string) error { data, err := ioutil.ReadFile(file) if err != nil { log.Printf("Unable to ReadConfig File: %s -> %s", file, err.Error()) return err } conf := TemplateConfig{} err = json.Unmarshal(data, &conf) if err != nil { log.Printf("Unable to load Config File: %s -> %s", file, err.Error()) return err } return mergo.MergeWithOverwrite(t, conf) }
// Load a single JSON file, and merge it into the specified Config func LoadJSONFile(filePath string, conf Config) error { defer Track("LoadJSONFile", Now(), debugOut) buf, err := ioutil.ReadFile(filePath) if err != nil { debugOut.Printf("Error reading config file '%s': %s\n", filePath, err) return err } newConf, err := JsonToConfig(buf) if err != nil { debugOut.Printf("Error parsing JSON in config file '%s': %s\n", filePath, err) return err } err = mergo.MergeWithOverwrite(&conf, newConf) return nil }
// Load loads the configuration from a yaml file func (c *ClientConfig) Load(file string) error { data, err := ioutil.ReadFile(file) if err != nil { log.Printf("Unable to ReadConfig File: %s -> %s", file, err.Error()) return err } conf := ClientConfig{} err = yaml.Unmarshal(data, &conf) if err != nil { log.Printf("Unable to load Config File: %s -> %s", file, err.Error()) return err } return mergo.MergeWithOverwrite(c, conf) }
// Attach attaches to the specified service in the running container func (c *DockerCompose) Attach(config parity.ShellConfig) (err error) { log.Stage("Interactive Shell") log.Debug("Compose - starting docker compose services") mergedConfig := *parity.DEFAULT_INTERACTIVE_SHELL_OPTIONS mergo.MergeWithOverwrite(&mergedConfig, &config) client := utils.DockerClient() container := fmt.Sprintf("parity-%s_%s_1", c.pluginConfig.ProjectNameSafe, mergedConfig.Service) opts := dockerclient.AttachToContainerOptions{ Stdin: true, Stdout: true, Stderr: true, InputStream: os.Stdin, OutputStream: os.Stdout, ErrorStream: os.Stderr, RawTerminal: true, Container: container, Stream: true, Logs: true, } if c.project.Configs[config.Service] == nil { return fmt.Errorf("Service %s does not exist", config.Service) } log.Step("Attaching to container '%s'", container) if err := client.AttachToContainer(opts); err == nil { err = c.project.Up(config.Service) if err != nil { log.Error("error: %s", err.Error()) return err } } else { return err } _, err = client.WaitContainer(container) log.Error("wc error: %s", err.Error()) log.Debug("Docker Compose Run() finished") return err }
func (t *Tournament) UpdateInfo(tdata Info) error { locationChange := (tdata.Location != t.Info.Location) if err := mergo.MergeWithOverwrite(&t.Info, tdata); err != nil { return errors.New(err.Error() + " - Could not update tournament info") } // Merge seems to not handle time.Time for some reason, thus fixup fixupTournamentInfo(&t.Info, tdata) err := storage.Store(t) if err != nil { return errors.New(err.Error() + " - Could not store updated tournament info") } if locationChange { eventqueue.Publish(utils.CKPTEvent{ Type: utils.TOURNAMENT_EVENT, Subject: "Vertskap registrert", Message: "Det er registrert nytt vertskap for en turnering på ckpt.no!"}) } return nil }
func mergeExtraVars(dst, src string) (string, error) { var ( d map[string]interface{} s map[string]interface{} ) if err := json.Unmarshal([]byte(dst), &d); err != nil { return "", errored.Errorf("failed to unmarshal dest extra vars %q. Error: %v", dst, err) } if err := json.Unmarshal([]byte(src), &s); err != nil { return "", errored.Errorf("failed to unmarshal src extra vars %q. Error: %v", src, err) } if err := mergo.MergeWithOverwrite(&d, &s); err != nil { return "", errored.Errorf("failed to merge extra vars, dst: %q src: %q. Error: %v", dst, src, err) } o, err := json.Marshal(d) if err != nil { return "", errored.Errorf("failed to marshal resulting extra vars %q. Error: %v", o, err) } return string(o), nil }
func (o *ChefOperation) createConf(vars map[string]string) (string, error) { // Overwrite configuration by user specified configuration in task.yml m, err := o.defaultConfig() if err != nil { return "", err } if err := mergo.MergeWithOverwrite(&m, o.Configurations); err != nil { return "", err } // Output configuration to temporary file f, err := ioutil.TempFile("", "chef-conf") if err != nil { return "", err } defer f.Close() for k, v := range m { if _, err := f.WriteString(fmt.Sprintf("%s %s\n", k, convertRubyCode(v))); err != nil { return "", err } } return f.Name(), nil }
// AddOnTop takes another output and adds it at a higher priority to this output // merging the two func (o Output) AddOnTop(higherPriority Output) error { return mergo.MergeWithOverwrite(&o, higherPriority) }
func compileArtifact(context *pongo2.Context, artifact *data.Artifact, providerName string, baseDir *string, workDir *string) error { outputFile := filepath.Join(*workDir, "build", providerName, artifact.Name, "user-data") outputDir := filepath.Dir(outputFile) // Create output directory if missing if exists, _ := util.FileExists(outputDir); !exists { if err := os.MkdirAll(outputDir, 0755); err != nil { return errors.New(fmt.Sprintf("Could not create output directory:\n %s\n\nnested error:\n %s", outputDir, err.Error())) } } log.Notice("▶ Compiling artifact [%s] to:\n %s", artifact.Name, outputFile) log.Debug(" Setting artifact name to the context") c := pongo2.Context{ "env": map[string]interface{}{ "provider": providerName, "artifact": artifact.Name, }, } if err := mergo.MergeWithOverwrite(context, c); err != nil { return err } printContextData(context, true) // Create the initial user data objects buffer := bytes.NewBufferString("#cloud-config\n---\n") writeFiles := make([]data.WriteFile, 0, 10) units := make([]data.Unit, 0, 10) // Process bricks bricks := artifact.Bricks for _, brick := range bricks { log.Info(" Processing brick:\n %s", brick) // Load metadata if metadata, brickBaseDir, err := loadBrickMetadata(context, baseDir, &brick); err == nil { // Process brick files log.Debug(" Processing brick files") if brickWriteFiles, err := processMetadataFiles(context, metadata, brickBaseDir); err != nil { return err } else { writeFiles = append(writeFiles, brickWriteFiles...) } // Process brick units log.Debug(" Processing brick unit") if brickUnit, err := processMetadataUnits(context, metadata, brickBaseDir); err != nil { return err } else if brickUnit != nil { units = append(units, *brickUnit) } } else { return err } } // Write compiled write-files section if len(writeFiles) > 0 { writeFilesMap := make(map[string]interface{}) writeFilesMap["write-files"] = writeFiles if output, err := yaml.Marshal(writeFilesMap); err != nil { return err } else { buffer.Write(output) } } // Write rest of config file cloudConfig := artifact.CloudConfig // Custom CoreOS if cloudConfig.CoreOS != nil { coreOs := cloudConfig.CoreOS // Add units // if coreOs["units"] != nil { coreOs["units"] = units // } // Write CoreOS section coreOsMap := make(map[string]interface{}) coreOsMap["coreos"] = coreOs if output, err := yaml.Marshal(coreOsMap); err != nil { return err } else { buffer.Write(output) } } // Append everything else to the end of the file if cloudConfig.Generic != nil { if output, err := yaml.Marshal(cloudConfig.Generic); err != nil { return err } else { buffer.Write(output) } } log.Info(" Writing output file to:\n %s", outputFile) if err := ioutil.WriteFile(outputFile, buffer.Bytes(), 0755); err != nil { return err } return nil }
// Set implements the OptionSetter func (c Configuration) Set(main *Configuration) { mergo.MergeWithOverwrite(main, c) }
// MergeFromConfig merges the specified configuration into the receiver configuration // On success, it also return the updated receiver configuration func (c *Config) MergeFromConfig(src *Config) (*Config, error) { if err := mergo.MergeWithOverwrite(c, src); err != nil { return nil, errored.Errorf("failed to merge configuration. Error: %s", err) } return c, nil }