commands

package
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 30, 2018 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConvertCmd = &cobra.Command{
	Use:   "convert [file]",
	Args:  cobra.ExactArgs(1),
	Short: "convert a .(toml|yml|json) file to a .san one",
	Long:  "convert a .(toml|yml|json) file to a .san one",
	Run: func(cmd *cobra.Command, args []string) {
		file := args[0]
		fileInfo, err := os.Stat(file)
		if err != nil {
			log.Fatal(fmt.Sprintf("error: opening %s: %s", file, err))
		}

		if fileInfo.IsDir() {
			log.Fatal("error: " + file + " is a directory")
		}

		ext := filepath.Ext(file)
		fileWithotuExt := strings.TrimSuffix(file, ext)
		data, err := ioutil.ReadFile(file)

		s := map[string]interface{}{}
		switch ext {
		case ".toml":
			err = toml.Unmarshal(data, &s)
		case ".yml", ".yaml":
			err = yaml.Unmarshal(data, &s)
		case ".json":
			err = json.Unmarshal(data, &s)
		default:
			log.Fatal("extension " + ext + " not recgnized, valid extension are one of the following: [toml, yml, yaml, json]")
		}
		if err != nil {
			log.Fatal(err.Error())
		}

		dataToWrite, err := san.Marshal(s)
		if err != nil {
			log.Fatal(err.Error())
		}

		if convertOutput == "" {
			convertOutput = fileWithotuExt + ".san"
		}
		err = ioutil.WriteFile(convertOutput, dataToWrite, fileInfo.Mode())
		if err != nil {
			log.Fatal(err.Error())
		}
	},
}

ConvertCmd is the `convert` command. It permit to convert configuration file in other formats to SAN

View Source
var FmtCmd = &cobra.Command{
	Use:   "fmt [file]",
	Args:  cobra.ExactArgs(1),
	Short: "automatically formats a SAN file",
	Long:  "automatically formats a SAN file",
	Run: func(cmd *cobra.Command, args []string) {
		file := args[0]
		fileInfo, err := os.Stat(file)
		s := map[string]interface{}{}
		if err != nil {
			log.Fatal(fmt.Sprintf("error: opening %s: %s", file, err))
		}

		if fileInfo.IsDir() {
			log.Fatal("error: " + file + " is a directory")
		}

		data, err := ioutil.ReadFile(file)
		if err != nil {
			log.Fatal(err.Error())
		}

		err = san.Unmarshal(data, &s)
		if err != nil {
			log.Fatal(err.Error())
		}

		data, err = san.Marshal(s)
		if err != nil {
			log.Fatal(err.Error())
		}

		if fmtOutput == "" {
			fmtOutput = file
		}
		err = ioutil.WriteFile(fmtOutput, data, fileInfo.Mode())
		if err != nil {
			log.Fatal(err.Error())
		}
	},
}

FmtCmd is the `fmt` command. It permit to automatically format a SAN file TODO:

View Source
var RootCmd = &cobra.Command{
	Use:   "san",
	Short: "the Simple And Needed configuration format",
	Long:  "the Simple And Needed configuration format. https://astrocorp.net/san",
	Run: func(cmd *cobra.Command, args []string) {
		cmd.Help()
	},
}

RootCmd is the san CLI's root command.

View Source
var ValidateCmd = &cobra.Command{
	Use:   "validate [file]",
	Args:  cobra.ExactArgs(1),
	Short: "check if a .san file validity",
	Long:  "check if a .san file validity",
	Run: func(cmd *cobra.Command, args []string) {
		file := args[0]
		fileInfo, err := os.Stat(file)
		if err != nil {
			log.Fatal(fmt.Sprintf("error: opening %s: %s", file, err))
		}

		if fileInfo.IsDir() {
			log.Fatal("error: " + file + " is a directory")
		}

		data, err := ioutil.ReadFile(file)
		if err != nil {
			log.Fatal(err.Error())
		}

		_, err = parser.Parse(data)
		if err != nil {
			log.Fatal(err.Error())
		}
	},
}

ValidateCmd is the `validate` command. It permit to check if a .san file is valid or not

View Source
var VersionCmd = &cobra.Command{
	Use:   "version",
	Short: "display version information",
	Long:  "display version information",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Printf("v%s\n", san.Version)
	},
}

VersionCmd display the version information

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL