Skip to content

Commit 8c92df4

Browse files
committed
refactor: simplify configuration provider selection and update dependency to v0.0.3
1 parent ae604a3 commit 8c92df4

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed

config/config.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package config
22

33
import (
4-
"context"
54
"encoding/json"
6-
"errors"
75

86
"github.com/go-sphere/confstore"
97
"github.com/go-sphere/confstore/codec"
@@ -120,22 +118,20 @@ func ToRaw[T any](conf T) json.RawMessage {
120118
return raw
121119
}
122120

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-
133121
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+
)
135131
if err != nil {
136132
return nil, err
137133
}
138-
config, err := confstore.Load[SyncConfig](context.Background(), pro, codec.JsonCodec())
134+
config, err := confstore.Load[SyncConfig](prov, codec.JsonCodec())
139135
if err != nil {
140136
return nil, err
141137
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ go 1.23.0
55
toolchain go1.23.5
66

77
require (
8-
github.com/go-sphere/confstore v0.0.2
8+
github.com/go-sphere/confstore v0.0.3
99
github.com/robfig/cron/v3 v3.0.1
1010
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
github.com/go-sphere/confstore v0.0.2 h1:9nuPS86wlv5Rpi+fnnFNq9tDVxgfAOK6uOPiASsgubo=
2-
github.com/go-sphere/confstore v0.0.2/go.mod h1:rvp2oSOW4x3E8JU0efD9JtHpBM2M3VIqM4rohoSMr34=
1+
github.com/go-sphere/confstore v0.0.3 h1:LRMRnEDu++JZGt4onMUHKAcQ27f+8qz553S7knIyHTE=
2+
github.com/go-sphere/confstore v0.0.3/go.mod h1:rvp2oSOW4x3E8JU0efD9JtHpBM2M3VIqM4rohoSMr34=
33
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
44
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=

0 commit comments

Comments
 (0)