diff --git a/README.md b/README.md index e1f2bb1..e38793e 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,16 @@ TOML can also have tables (sections). They are referred to with dotted paths: ```bash export MYSECTIONVALUE=`stoml filename mysection.key` ``` -Error messages can be suppressed using `-q`. This is useful when running in a script. + + +Error messages can be suppressed using `-q`. +Empty strings, missing options and whitespace results can be turned into errors using `-s`. +These are useful when running in a script. + +Results are interpreted as space-separated words by default. (This is the easiest to reuse in Linux Shell scripts.) +You can change this behaviour with `-m` which will turn multi-line entries in a config file into multi-line output. +It will also use new-line instead of space when listing sections. + ## Caveats This is a `simple` implementation of parsing TOML. This means that it will work well with atomic types in the configuration, like string, int or boolean. diff --git a/cmd/root.go b/cmd/root.go index 1f03e17..fd6d215 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -12,6 +12,8 @@ import ( const epsilon = 1e-9 // Margin of error +var multiString bool + func RunRoot(cmd *cobra.Command, args []string) (output string, err error) { validateArgs := cobra.ExactArgs(2) if err = validateArgs(cmd, args); err != nil { @@ -47,7 +49,11 @@ func RunRoot(cmd *cobra.Command, args []string) (output string, err error) { r = append(r,k) } sort.Strings(r) - output = strings.Join(r, " ") + if multiString { + output = strings.Join(r, "\n") + } else { + output = strings.Join(r, " ") + } // Return list of strings as result default: // Return all section names and root section keys if "." is provided @@ -57,9 +63,17 @@ func RunRoot(cmd *cobra.Command, args []string) (output string, err error) { r = append(r,k) } sort.Strings(r) - output = strings.Join(r, " ") + if multiString { + output = strings.Join(r, "\n") + } else { + output = strings.Join(r, " ") + } } else { - output = strings.Join(viper.GetStringSlice(key), " ") + if multiString { + output = viper.GetString(key) + } else { + output = strings.Join(viper.GetStringSlice(key), " ") + } } } return @@ -84,6 +98,8 @@ Source and documentation is available at https://github.com/freshautomations/sto } rootCmd.Use = "stoml " rootCmd.PersistentFlags().BoolVarP(&exit.Quiet,"quiet", "q", false, "do not display error messages") + rootCmd.PersistentFlags().BoolVarP(&exit.Strict,"strict", "s", false, "fail if result is empty, non-existent or just whitespaces") + rootCmd.PersistentFlags().BoolVarP(&multiString,"multi", "m", false, "read the result as-is, useful with multi-line entries") return rootCmd.Execute() } diff --git a/defaults/defaults.go b/defaults/defaults.go index c8022cf..e575662 100644 --- a/defaults/defaults.go +++ b/defaults/defaults.go @@ -1,4 +1,4 @@ package defaults // Application version -const Version = "0.6.1" +const Version = "0.6.2" diff --git a/exit/exit.go b/exit/exit.go index ee9edac..237f570 100644 --- a/exit/exit.go +++ b/exit/exit.go @@ -3,9 +3,11 @@ package exit import ( "fmt" "os" + "strings" ) var Quiet bool +var Strict bool func Fail(err error) { if !Quiet { @@ -15,6 +17,9 @@ func Fail(err error) { } func Succeed(result string) { + if Strict && strings.TrimSpace(result) == "" { + os.Exit(1) + } fmt.Print(result) os.Exit(0) } diff --git a/test.toml b/test.toml index e5ac362..a390542 100644 --- a/test.toml +++ b/test.toml @@ -12,3 +12,4 @@ numbers = [1, 1, 2, 3, 5] strings = ["The answer is", "42"] [emptysection] +