-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconf.go
More file actions
165 lines (155 loc) · 4.48 KB
/
conf.go
File metadata and controls
165 lines (155 loc) · 4.48 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package ox
import (
"errors"
"os"
"os/user"
"path/filepath"
"runtime"
"slices"
"strconv"
)
// ConfigType is a config type.
type ConfigType string
// Config types.
const (
AnyT ConfigType = ""
EnvT ConfigType = "env"
JSONT ConfigType = "json"
YAMLT ConfigType = "yaml"
HCLT ConfigType = "hcl"
TOMLT ConfigType = "toml"
)
// DefaultLoader is the default config loader.
var DefaultLoader = func(ctx *Context, typ ConfigType, key string) (any, bool, error) {
// forced type, check only one
if typ != "" {
if loader, ok := loaders[typ]; ok {
return loader(ctx, key)
}
return nil, false, ErrUnknownLoader
}
// check each loader
for _, typ := range loaderOrder {
if loader, ok := loaders[typ]; ok {
switch v, ok, err := loader(ctx, key); {
case errors.Is(err, ErrUnknownKey), errors.Is(err, ErrInvalidKey):
continue
case err != nil:
return nil, false, err
case ok:
return v, true, nil
}
}
}
return nil, false, ErrUnknownKey
}
// DefaultKeyLoader is the default config key loader.
//
// The following keys are recognized:
//
// $PWD - the current working directory, taken from [os.Getwd] (ex: /some/path)
// $HOME - the current user's home directory, taken from [os.UserHomeDir] (ex: ~/)
// $USER - the current user's user name, taken from [user.Current] (ex: user)
// $APPNAME - the root command's name (ex: appName)
// $APPVERSION - the root command's version (ex: v0.1.0)
// $CONFIG - the current user's config directory, taken from [os.UserConfigDir] (ex: ~/.config)
// $APPCONFIG - the current user's config directory, with the root command's name added as a subdir (ex: ~/.config/appName)
// $CACHE - the current user's cache directory, taken from [os.UserCacheDir] (ex: ~/.cache)
// $APPCACHE - the current user's cache directory, with the root command's name added as a subdir (ex: ~/.cache/appName)
// $NUMCPU - the value of [runtime.NumCPU] (ex: 4)
// $NUMCPU2 - the value of [runtime.NumCPU], plus 2 (ex: 6)
// $NUMCPU2X - the value of [runtime.NumCPU], times 2 (ex: 8)
// $ARCH - the value of [runtime.GOARCH] (ex: amd64)
// $OS - the value of [runtime.GOOS] (ex: windows)
var DefaultKeyLoader = func(ctx *Context, key string) (any, bool, error) {
var f func() (string, error)
switch key {
case "PWD":
f = DefaultGetwd
case "HOME":
f = userHomeDir
case "USER":
f = func() (string, error) {
u, err := user.Current()
if err != nil {
return "", err
}
return u.Username, nil
}
case "APPNAME":
return ctx.Root.Name, true, nil
case "APPVERSION":
return ctx.Root.Version, true, nil
case "CONFIG":
f = userConfigDir
case "APPCONFIG":
f = func() (string, error) {
dir, err := userConfigDir()
if err != nil {
return "", err
}
return filepath.Join(dir, ctx.Root.Name), nil
}
case "CACHE":
f = userCacheDir
case "APPCACHE":
f = func() (string, error) {
dir, err := userCacheDir()
if err != nil {
return "", err
}
return filepath.Join(dir, ctx.Root.Name), nil
}
case "NUMCPU":
return strconv.Itoa(runtime.NumCPU()), true, nil
case "NUMCPU2":
return strconv.Itoa(runtime.NumCPU() + 2), true, nil
case "NUMCPU2X":
return strconv.Itoa(runtime.NumCPU() * 2), true, nil
case "ARCH":
return runtime.GOARCH, true, nil
case "OS":
return runtime.GOOS, true, nil
default:
return nil, false, ErrUnknownKey
}
s, err := f()
if err != nil {
return "", false, err
}
return s, true, nil
}
// DefaultEnvLoader is the default environment config key loader.
var DefaultEnvLoader = func(_ *Context, key string) (any, bool, error) {
if s, ok := os.LookupEnv(key); ok {
return s, true, nil
}
return "", false, ErrUnknownKey
}
var (
// loaders are config loaders.
loaders map[ConfigType]func(*Context, string) (any, bool, error)
// loaderOrder is the order that loaders were registered.
loaderOrder []ConfigType
)
func init() {
loaders = make(map[ConfigType]func(*Context, string) (any, bool, error))
RegisterConfigLoader(AnyT, DefaultKeyLoader)
RegisterConfigLoader(EnvT, DefaultEnvLoader)
}
// RegisterConfigLoader registers a config loader.
func RegisterConfigLoader(typ ConfigType, loader func(*Context, string) (any, bool, error), extensions ...string) {
loaders[typ] = loader
if !slices.Contains(loaderOrder, typ) {
loaderOrder = append(loaderOrder, typ)
}
}
// RegisterConfigLoaderOrder sets the loader order.
func RegisterConfigLoaderOrder(order ...ConfigType) {
loaderOrder = loaderOrder[:0]
for _, typ := range order {
if !slices.Contains(loaderOrder, typ) {
loaderOrder = append(loaderOrder, typ)
}
}
}