// ToYaml returns a yaml.Map containing the data from Config. func (c *Config) ToYaml() yaml.Node { cfg := make(map[string]yaml.Node, 5) cfg["package"] = yaml.Scalar(c.Name) if len(c.InCommand) > 0 { cfg["incmd"] = yaml.Scalar(c.InCommand) } if c.Flatten == true { cfg["flatten"] = yaml.Scalar("true") } imps := make([]yaml.Node, len(c.Imports)) for i, imp := range c.Imports { imps[i] = imp.ToYaml() } devimps := make([]yaml.Node, len(c.DevImports)) for i, dimp := range c.DevImports { devimps[i] = dimp.ToYaml() } // Fixed in 0.5.0. Prior to that, these were not being printed. Worried // that the "fix" might introduce an unintended side effect. if len(imps) > 0 { cfg["import"] = yaml.List(imps) } if len(devimps) > 0 { cfg["devimport"] = yaml.List(devimps) } return yaml.Map(cfg) }
// MergeToYaml converts a Config object and a yaml.File to a single yaml.File. // // Params: // - conf (*Config): The configuration to merge. // - overwriteImports (bool, default true): If this is true, old config will // overwritten. If false, we attempt to merge the old and new config, with // preference to the old. // // Returns: // - The root yaml.Node of the modified config. // // Uses: // - cxt.Get("yaml.File") as the source for the YAML file. func MergeToYaml(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) { root := c.Get("yaml.File", nil).(*yaml.File).Root cfg := p.Get("conf", nil).(*Config) overwrite := p.Get("overwriteImports", true).(bool) rootMap, ok := root.(yaml.Map) if !ok { return nil, fmt.Errorf("Expected root node to be a map.") } if len(cfg.Name) > 0 { rootMap["package"] = yaml.Scalar(cfg.Name) } if cfg.InCommand != "" { rootMap["incmd"] = yaml.Scalar(cfg.InCommand) } if overwrite { // Imports imports := make([]yaml.Node, len(cfg.Imports)) for i, imp := range cfg.Imports { imports[i] = imp.ToYaml() } rootMap["import"] = yaml.List(imports) } else { var err error rootMap, err = mergeImports(rootMap, cfg) if err != nil { Warn("Problem merging imports: %s\n", err) } } return root, nil }
// ToYaml converts a *Dependency to a YAML Map node. func (d *Dependency) ToYaml() yaml.Node { dep := make(map[string]yaml.Node, 8) dep["package"] = yaml.Scalar(d.Name) if len(d.Subpackages) > 0 { subp := make([]yaml.Node, len(d.Subpackages)) for i, item := range d.Subpackages { subp[i] = yaml.Scalar(item) } dep["subpackages"] = yaml.List(subp) } vcs := d.VcsType if len(vcs) > 0 { dep["vcs"] = yaml.Scalar(vcs) } if len(d.Reference) > 0 { dep["ref"] = yaml.Scalar(d.Reference) } if len(d.Repository) > 0 { dep["repo"] = yaml.Scalar(d.Repository) } if len(d.Arch) > 0 { archs := make([]yaml.Node, len(d.Arch)) for i, a := range d.Arch { archs[i] = yaml.Scalar(a) } dep["arch"] = yaml.List(archs) } if len(d.Os) > 0 { oses := make([]yaml.Node, len(d.Os)) for i, a := range d.Os { oses[i] = yaml.Scalar(a) } dep["os"] = yaml.List(oses) } // Note, the yaml package we use sorts strings of scalars so flatten // will always be the top item. if d.Flatten == true { dep["flatten"] = yaml.Scalar("true") } return yaml.Map(dep) }
// ToYaml converts a *Dependency to a YAML Map node. func (d *Dependency) ToYaml() yaml.Node { dep := make(map[string]yaml.Node, 5) dep["package"] = yaml.Scalar(d.Name) if len(d.Subpackages) > 0 { subp := make([]yaml.Node, len(d.Subpackages)) for i, item := range d.Subpackages { subp[i] = yaml.Scalar(item) } dep["subpackages"] = yaml.List(subp) } vcs := d.VcsType if len(vcs) > 0 { dep["vcs"] = yaml.Scalar(vcs) } if len(d.Reference) > 0 { dep["ref"] = yaml.Scalar(d.Reference) } if len(d.Repository) > 0 { dep["repo"] = yaml.Scalar(d.Repository) } if len(d.Arch) > 0 { archs := make([]yaml.Node, len(d.Arch)) for i, a := range d.Arch { archs[i] = yaml.Scalar(a) } dep["arch"] = yaml.List(archs) } if len(d.Os) > 0 { oses := make([]yaml.Node, len(d.Os)) for i, a := range d.Os { oses[i] = yaml.Scalar(a) } dep["os"] = yaml.List(oses) } return yaml.Map(dep) }