-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_mustparse_test.go
94 lines (79 loc) · 2.15 KB
/
example_mustparse_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package proteus_test
import (
"fmt"
"os"
"github.com/simplesurance/proteus"
"github.com/simplesurance/proteus/sources/cfgenv"
"github.com/simplesurance/proteus/sources/cfgflags"
)
func ExampleMustParse() {
params := struct {
Server string
Port uint16
}{}
parsed, err := proteus.MustParse(¶ms)
if err != nil {
parsed.WriteError(os.Stderr, err)
os.Exit(1)
}
fmt.Printf("Server: %s:%d\n", params.Server, params.Port)
}
func ExampleMustParse_withTags() {
params := struct {
Enabled bool `param:"is_enabled,optional" param_desc:"Allows enabling or disabling the HTTP server"`
Port uint16 `param:",optional" param_desc:"Port to bind for the HTTP server"`
Token string `param:",secret" param_desc:"Client authentication token"`
}{
Enabled: true,
Port: 8080,
}
parsed, err := proteus.MustParse(¶ms)
if err != nil {
parsed.WriteError(os.Stderr, err)
os.Exit(1)
}
if params.Enabled {
fmt.Printf("Starting HTTP server on :%d\n", params.Port)
}
}
// ExampleMustParse_providers changes how and from where proteus reads
// configuration.
func ExampleMustParse_providers() {
params := struct {
Server string
Port uint16
}{}
parsed, err := proteus.MustParse(¶ms,
proteus.WithProviders(
cfgenv.New("CONFIG"), // change env var prefix to CONFIG
cfgflags.New())) // flags are used, but priority is to env vars
if err != nil {
parsed.WriteError(os.Stderr, err)
os.Exit(1)
}
fmt.Printf("Server: %s:%d\n", params.Server, params.Port)
}
// ExampleMustParse_trimSpaces instructs proteus to trim values of parameters,
// removing leading and trailing spaces. This also removes trailing new lines.
func ExampleMustParse_trimSpaces() {
params := struct {
Server string
Port uint16
}{}
parsed, err := proteus.MustParse(¶ms,
proteus.WithValueFormatting(proteus.ValueFormattingOptions{
TrimSpace: true,
}))
if err != nil {
parsed.WriteError(os.Stderr, err)
os.Exit(1)
}
fmt.Printf("Server: %s:%d\n", params.Server, params.Port)
// Calling with:
//
// ./app -server "localhost" -port "8080"
//
// is the same as:
//
// ./app -server " localhost \n" -port "8080\n"
}