|
1 | 1 | package config |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | 4 | "encoding/json" |
6 | | - "errors" |
7 | 5 |
|
8 | 6 | "github.com/go-sphere/confstore" |
9 | 7 | "github.com/go-sphere/confstore/codec" |
@@ -120,22 +118,20 @@ func ToRaw[T any](conf T) json.RawMessage { |
120 | 118 | return raw |
121 | 119 | } |
122 | 120 |
|
123 | | -func newConfProvider(path string) (provider.Provider, error) { |
124 | | - if http.IsRemoteURL(path) { |
125 | | - return http.New(path, http.WithTimeout(10)), nil |
126 | | - } |
127 | | - if file.IsLocalPath(path) { |
128 | | - return file.New(path, file.WithExpandEnv()), nil |
129 | | - } |
130 | | - return nil, errors.New("unsupported config path") |
131 | | -} |
132 | | - |
133 | 121 | func NewConfig(path string) (*SyncConfig, error) { |
134 | | - pro, err := newConfProvider(path) |
| 122 | + prov, err := provider.Selector( |
| 123 | + path, |
| 124 | + provider.If(file.IsLocalPath, func(s string) provider.Provider { |
| 125 | + return file.New(path, file.WithExpandEnv()) |
| 126 | + }), |
| 127 | + provider.If(http.IsRemoteURL, func(s string) provider.Provider { |
| 128 | + return http.New(path, http.WithTimeout(10)) |
| 129 | + }), |
| 130 | + ) |
135 | 131 | if err != nil { |
136 | 132 | return nil, err |
137 | 133 | } |
138 | | - config, err := confstore.Load[SyncConfig](context.Background(), pro, codec.JsonCodec()) |
| 134 | + config, err := confstore.Load[SyncConfig](prov, codec.JsonCodec()) |
139 | 135 | if err != nil { |
140 | 136 | return nil, err |
141 | 137 | } |
|
0 commit comments