Skip to content

Commit 3c99e3b

Browse files
committed
Added support for bool
1 parent 7d65469 commit 3c99e3b

3 files changed

Lines changed: 104 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Getting vars out has the same level of complexity as setting the value.
1616
## Features
1717
- Simple to use
1818
- supports command line and environment variables
19-
- Support for types: str, int, ~~bool~~, ~~map~~
19+
- Support for types: str, int, bool, ~~map~~
2020
- Singleton, makes it easy to use in program anywhere in the code-base
2121
- Supports help text with --help on the binary
2222
- Ease of use in any environment examples: linux, docker, k8

settingo/main.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,20 @@ import (
66
"strconv"
77
)
88

9+
func truthiness(s string) bool {
10+
thruthy := map[string]bool{
11+
"y": true,
12+
"true": true,
13+
"yes": true,
14+
}
15+
return thruthy[s]
16+
}
17+
918
type Settings struct {
1019
msg map[string]string
1120
VarString map[string]string
1221
VarInt map[string]int
22+
VarBool map[string]bool
1323
Parsers map[string]func(string) string
1424
ParsersInt map[string]func(int) int
1525
}
@@ -28,6 +38,11 @@ func (s *Settings) SetInt(flagName string, defaultVar int, message string) {
2838
s.VarInt[flagName] = defaultVar
2939
}
3040

41+
func (s *Settings) SetBool(flagName string, defaultVar bool, message string) {
42+
s.msg[flagName] = message
43+
s.VarBool[flagName] = defaultVar
44+
}
45+
3146
func (s *Settings) SetParsed(flagName, defaultVar, message string, parserFunc func(string) string) {
3247
s.msg[flagName] = message
3348
s.VarString[flagName] = defaultVar
@@ -48,6 +63,10 @@ func (s Settings) GetInt(flagName string) int {
4863
return s.VarInt[flagName]
4964
}
5065

66+
func (s Settings) GetBool(flagName string) bool {
67+
return s.VarBool[flagName]
68+
}
69+
5170
func (s *Settings) HandleCMDLineInput() {
5271
parsedString := make(map[string]*string)
5372
for key, val := range s.VarString {
@@ -59,6 +78,11 @@ func (s *Settings) HandleCMDLineInput() {
5978
var newV = flag.Int(key, val, s.msg[key])
6079
parsedInt[key] = newV
6180
}
81+
parsedBool := make(map[string]*string)
82+
for key, val := range s.VarBool {
83+
var newV = flag.String(key, strconv.FormatBool(val), s.msg[key])
84+
parsedBool[key] = newV
85+
}
6286
flag.Parse()
6387

6488
for key, val := range parsedString {
@@ -75,6 +99,9 @@ func (s *Settings) HandleCMDLineInput() {
7599
s.VarInt[key] = *val
76100
}
77101
}
102+
for key, val := range parsedBool {
103+
s.VarBool[key] = truthiness(*val)
104+
}
78105
}
79106

80107
func (s *Settings) HandleOSInput() {
@@ -92,6 +119,12 @@ func (s *Settings) HandleOSInput() {
92119
}
93120
}
94121
}
122+
for key, _ := range s.VarBool {
123+
varEnv, found := os.LookupEnv(key)
124+
if found {
125+
s.VarBool[key] = truthiness(varEnv)
126+
}
127+
}
95128
}
96129

97130
func (s *Settings) Parse() {
@@ -105,4 +138,5 @@ var SETTINGS = Settings{
105138
VarInt: make(map[string]int),
106139
Parsers: make(map[string]func(string) string),
107140
ParsersInt: make(map[string]func(int) int),
141+
VarBool: make(map[string]bool),
108142
}

settingo/main_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package settingo
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
func Test_types_default(t *testing.T) {
9+
expected := "default_value_for_foobar"
10+
expectedInt := 42
11+
expectedBool := true
12+
13+
SETTINGS.Set("FOOBAR", expected, "help text")
14+
SETTINGS.SetInt("FOOBAR_INT", expectedInt, "help text")
15+
SETTINGS.SetBool("FOOBAR_BOOL", expectedBool, "help text")
16+
17+
//SETTINGS.Parse()
18+
19+
foobar := SETTINGS.Get("FOOBAR")
20+
if foobar != expected {
21+
t.Error(foobar, " != ", expected)
22+
}
23+
24+
foobarInt := SETTINGS.GetInt("FOOBAR_INT")
25+
if foobarInt != expectedInt {
26+
t.Error(foobarInt, " != ", expectedInt)
27+
}
28+
29+
foobarBool := SETTINGS.GetBool("FOOBAR_BOOL")
30+
if foobarBool != expectedBool {
31+
t.Error(foobarBool, " != ", expectedBool)
32+
}
33+
}
34+
35+
func Test_types_os_env(t *testing.T) {
36+
37+
expected := "other value"
38+
os.Setenv("FOOBAR", expected)
39+
defaultStr := "default value"
40+
41+
expectedInt := 44
42+
os.Setenv("FOOBAR_INT", "44")
43+
defaultInt := 42
44+
45+
os.Setenv("FOOBAR_BOOL", "y")
46+
expectedBool := true
47+
defaultBool := false
48+
49+
SETTINGS.Set("FOOBAR", defaultStr, "help text")
50+
SETTINGS.SetInt("FOOBAR_INT", defaultInt, "help text")
51+
SETTINGS.SetBool("FOOBAR_BOOL", defaultBool, "help text")
52+
53+
SETTINGS.Parse()
54+
55+
foobar := SETTINGS.Get("FOOBAR")
56+
if foobar != expected {
57+
t.Error(foobar, " != ", expected)
58+
}
59+
60+
foobarInt := SETTINGS.GetInt("FOOBAR_INT")
61+
if foobarInt != expectedInt {
62+
t.Error(foobarInt, " != ", expectedInt)
63+
}
64+
65+
foobarBool := SETTINGS.GetBool("FOOBAR_BOOL")
66+
if foobarBool != expectedBool {
67+
t.Error(foobarBool, " != ", expectedBool)
68+
}
69+
}

0 commit comments

Comments
 (0)