Ejemplo n.º 1
0
// if given empty string for fileName will use Service
// Definition Name
func WriteActionDefinitionFile(actDef *def.Action, fileName string) error {
	if strings.Contains(fileName, " ") {
		fileName = strings.Replace(actDef.Name, " ", "_", -1)
	}
	if filepath.Ext(fileName) == "" {
		fileName = fileName + ".toml"
	}

	logger.Debugf("Writing action def file =>\t%s:%s\n", actDef.Name, fileName)

	writer, err := os.Create(fileName)
	defer writer.Close()
	if err != nil {
		return err
	}

	switch filepath.Ext(fileName) {
	case ".json":
		mar, err := json.MarshalIndent(actDef, "", "  ")
		if err != nil {
			return err
		}
		mar = append(mar, '\n')
		writer.Write(mar)
	case ".yaml":
		mar, err := yaml.Marshal(actDef)
		if err != nil {
			return err
		}
		mar = append(mar, '\n')
		writer.Write(mar)
	default:
		writer.Write([]byte("# This is a TOML config file.\n# For more information, see https://github.com/toml-lang/toml\n\n"))
		enc := toml.NewEncoder(writer)
		enc.Indent = ""
		writer.Write([]byte("name = \"" + actDef.Name + "\"\n"))
		writer.Write([]byte("services = [ \"" + strings.Join(actDef.ServiceDeps, "\",\"") + "\" ]\n"))
		writer.Write([]byte("chain = \"" + actDef.Chain + "\"\n"))
		writer.Write([]byte("steps = [ \n"))
		for _, s := range actDef.Steps {
			if strings.Contains(s, "\"") {
				s = strings.Replace(s, "\"", "\\\"", -1)
			}
			writer.Write([]byte("  \"" + s + "\",\n"))
		}
		writer.Write([]byte("] \n"))
		writer.Write([]byte("\n[environment]\n"))
		enc.Encode(actDef.Environment)
		writer.Write([]byte("\n[maintainer]\n"))
		enc.Encode(actDef.Maintainer)
		writer.Write([]byte("\n[location]\n"))
		enc.Encode(actDef.Location)
		writer.Write([]byte("\n[machine]\n"))
		enc.Encode(actDef.Machine)
	}
	return nil
}
Ejemplo n.º 2
0
// if given empty string for fileName will use Service
// Definition Name
func WriteServiceDefinitionFile(serviceDef *def.ServiceDefinition, fileName string) error {
	// writer := os.Stdout

	if filepath.Ext(fileName) == "" {
		fileName = serviceDef.Service.Name + ".toml"
		fileName = filepath.Join(ServicesPath, fileName)
	}

	writer, err := os.Create(fileName)
	defer writer.Close()
	if err != nil {
		return err
	}

	switch filepath.Ext(fileName) {
	case ".json":
		mar, err := json.MarshalIndent(serviceDef, "", "  ")
		if err != nil {
			return err
		}
		mar = append(mar, '\n')
		writer.Write(mar)
	case ".yaml":
		mar, err := yaml.Marshal(serviceDef)
		if err != nil {
			return err
		}
		mar = append(mar, '\n')
		writer.Write(mar)
	default:
		writer.Write([]byte("# This is a TOML config file.\n# For more information, see https://github.com/toml-lang/toml\n\n"))
		enc := toml.NewEncoder(writer)
		enc.Indent = ""
		writer.Write([]byte("name = \"" + serviceDef.Name + "\"\n\n"))
		if serviceDef.ServiceID != "" {
			writer.Write([]byte("service_id = \"" + serviceDef.ServiceID + "\"\n"))
		}
		if serviceDef.Chain != "" {
			writer.Write([]byte("chain = \"" + serviceDef.Chain + "\"\n\n"))
		}
		writer.Write([]byte("[service]\n"))
		enc.Encode(serviceDef.Service)
		writer.Write([]byte("[dependencies]\n"))
		if serviceDef.Dependencies != nil {
			if len(serviceDef.Dependencies.Services) != 0 || len(serviceDef.Dependencies.Chains) != 0 {
				enc.Encode(serviceDef.Dependencies)
			}
		}
		writer.Write([]byte("\n[maintainer]\n"))
		enc.Encode(serviceDef.Maintainer)
		writer.Write([]byte("\n[location]\n"))
		enc.Encode(serviceDef.Location)
		writer.Write([]byte("\n[machine]\n"))
		enc.Encode(serviceDef.Machine)
	}
	return nil
}
Ejemplo n.º 3
0
func main() {
	if flag.NArg() != 0 {
		flag.Usage()
	}

	var tmp interface{}
	if err := json.NewDecoder(os.Stdin).Decode(&tmp); err != nil {
		log.Fatalf("Error decoding JSON: %s", err)
	}

	tomlData := translate(tmp)
	if err := toml.NewEncoder(os.Stdout).Encode(tomlData); err != nil {
		log.Fatalf("Error encoding TOML: %s", err)
	}
}
Ejemplo n.º 4
0
func SaveGlobalConfig(config *ErisConfig) error {
	writer, err := os.Create(filepath.Join(dir.ErisRoot, "eris.toml"))
	defer writer.Close()
	if err != nil {
		return err
	}

	enc := toml.NewEncoder(writer)
	enc.Indent = ""
	err = enc.Encode(config)
	if err != nil {
		return err
	}
	return nil
}
Ejemplo n.º 5
0
// if given empty string for fileName will use Service
// Definition Name
func WriteChainDefinitionFile(chainDef *def.Chain, fileName string) error {
	// writer := os.Stdout

	if filepath.Ext(fileName) == "" {
		fileName = chainDef.Name + ".toml"
		fileName = filepath.Join(ChainsPath, fileName)
	}

	writer, err := os.Create(fileName)
	defer writer.Close()
	if err != nil {
		return err
	}

	switch filepath.Ext(fileName) {
	case ".json":
		mar, err := json.MarshalIndent(chainDef, "", "  ")
		if err != nil {
			return err
		}
		mar = append(mar, '\n')
		writer.Write(mar)
	case ".yaml":
		mar, err := yaml.Marshal(chainDef)
		if err != nil {
			return err
		}
		mar = append(mar, '\n')
		writer.Write(mar)
	default:
		writer.Write([]byte("# This is a TOML config file.\n# For more information, see https://github.com/toml-lang/toml\n\n"))
		enc := toml.NewEncoder(writer)
		enc.Indent = ""
		writer.Write([]byte("name = \"" + chainDef.Name + "\"\n"))
		writer.Write([]byte("chain_id = \"" + chainDef.ChainID + "\"\n"))
		writer.Write([]byte("\n[service]\n"))
		enc.Encode(chainDef.Service)
		writer.Write([]byte("\n[maintainer]\n"))
		enc.Encode(chainDef.Maintainer)
	}
	return nil
}