-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.go
69 lines (64 loc) · 1.46 KB
/
environment.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
package cliz
import (
"os"
"strconv"
"github.com/hakadoriya/z.go/errorz"
)
//nolint:gocognit,cyclop
func (c *Command) loadEnvironments() error {
for _, opt := range c.Options {
if opt.GetEnv() == "" {
// If v is an empty string, o.value remains.
continue
}
switch o := opt.(type) {
case *StringOption:
if s := os.Getenv(o.Env); s != "" {
o.value = &s
}
case *BoolOption:
if s := os.Getenv(o.Env); s != "" {
v, err := strconv.ParseBool(s)
if err != nil {
return errorz.Errorf("%s: %w", o.Env, err)
}
o.value = &v
}
case *Int64Option:
if s := os.Getenv(o.Env); s != "" {
const base, bitSize = 10, 64
v, err := strconv.ParseInt(s, base, bitSize)
if err != nil {
return errorz.Errorf("%s: %w", o.Env, err)
}
o.value = &v
}
case *Uint64Option:
if s := os.Getenv(o.Env); s != "" {
const base, bitSize = 10, 64
v, err := strconv.ParseUint(s, base, bitSize)
if err != nil {
return errorz.Errorf("%s: %w", o.Env, err)
}
o.value = &v
}
case *Float64Option:
if s := os.Getenv(o.Env); s != "" {
const bitSize = 64
v, err := strconv.ParseFloat(s, bitSize)
if err != nil {
return errorz.Errorf("%s: %w", o.Env, err)
}
o.value = &v
}
default:
return errorz.Errorf("%s: %w", o.GetName(), ErrInvalidOptionType)
}
}
for _, subcmd := range c.SubCommands {
if err := subcmd.loadEnvironments(); err != nil {
return err
}
}
return nil
}