Skip to content

Commit

Permalink
v0.6.2 - added -s and -m options
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-szabo committed May 28, 2021
1 parent b175578 commit 33a4393
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 19 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -84,6 +98,8 @@ Source and documentation is available at https://github.com/freshautomations/sto
}
rootCmd.Use = "stoml <filename> <key>"
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()
}
2 changes: 1 addition & 1 deletion defaults/defaults.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package defaults

// Application version
const Version = "0.6.1"
const Version = "0.6.2"
5 changes: 5 additions & 0 deletions exit/exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package exit
import (
"fmt"
"os"
"strings"
)

var Quiet bool
var Strict bool

func Fail(err error) {
if !Quiet {
Expand All @@ -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)
}
1 change: 1 addition & 0 deletions test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ numbers = [1, 1, 2, 3, 5]
strings = ["The answer is", "42"]

[emptysection]

0 comments on commit 33a4393

Please sign in to comment.