This commit is contained in:
62
provider/ini/provider.go
Normal file
62
provider/ini/provider.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package ini
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
"gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
var _ config.Provider = (*Provider)(nil)
|
||||
|
||||
func New(data *ini.File) *Provider {
|
||||
return &Provider{
|
||||
data: data,
|
||||
resolve: func(ctx context.Context, key config.Key) (string, string) {
|
||||
keys := strings.SplitN(key.Name, "/", 2)
|
||||
if len(keys) == 1 {
|
||||
return "", keys[0]
|
||||
}
|
||||
|
||||
return keys[0], keys[1]
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type Provider struct {
|
||||
data *ini.File
|
||||
resolve func(ctx context.Context, key config.Key) (string, string)
|
||||
}
|
||||
|
||||
func (p *Provider) IsSupport(ctx context.Context, key config.Key) bool {
|
||||
section, name := p.resolve(ctx, key)
|
||||
|
||||
return section != "" && name != ""
|
||||
}
|
||||
|
||||
func (p *Provider) Name() string {
|
||||
return "ini"
|
||||
}
|
||||
|
||||
func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, error) {
|
||||
section, name := p.resolve(ctx, key)
|
||||
|
||||
iniSection, err := p.data.GetSection(section)
|
||||
if err != nil {
|
||||
return config.Variable{}, fmt.Errorf("%w: %s: %v", config.ErrVariableNotFound, p.Name(), err)
|
||||
}
|
||||
|
||||
iniKey, err := iniSection.GetKey(name)
|
||||
if err != nil {
|
||||
return config.Variable{}, fmt.Errorf("%w: %s: %v", config.ErrVariableNotFound, p.Name(), err)
|
||||
}
|
||||
|
||||
return config.Variable{
|
||||
Name: section + ":" + name,
|
||||
Provider: p.Name(),
|
||||
Value: value.JString(iniKey.String()),
|
||||
}, nil
|
||||
}
|
||||
28
provider/ini/provider_test.go
Normal file
28
provider/ini/provider_test.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package ini_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitoa.ru/go-4devs/config/provider/ini"
|
||||
"gitoa.ru/go-4devs/config/test"
|
||||
)
|
||||
|
||||
func TestProvider(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
file := test.NewINI()
|
||||
|
||||
read := []test.Read{
|
||||
test.NewRead("project/PROJECT_BOARD_BASIC_KANBAN_TYPE", "To Do, In Progress, Done"),
|
||||
test.NewRead("repository.editor/PREVIEWABLE_FILE_MODES", "markdown"),
|
||||
test.NewRead("server/LOCAL_ROOT_URL", "http://0.0.0.0:3000/"),
|
||||
test.NewRead("server/LFS_HTTP_AUTH_EXPIRY", 20*time.Minute),
|
||||
test.NewRead("repository.pull-request/DEFAULT_MERGE_MESSAGE_SIZE", 5120),
|
||||
test.NewRead("ui/SHOW_USER_EMAIL", true),
|
||||
test.NewRead("cors/ENABLED", false),
|
||||
}
|
||||
|
||||
prov := ini.New(file)
|
||||
test.Run(t, prov, read)
|
||||
}
|
||||
Reference in New Issue
Block a user