add definition config
This commit is contained in:
@@ -10,55 +10,49 @@ import (
|
||||
"gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
const (
|
||||
Name = "ini"
|
||||
Separator = "."
|
||||
)
|
||||
|
||||
var _ config.Provider = (*Provider)(nil)
|
||||
|
||||
func New(data *ini.File) *Provider {
|
||||
const nameParts = 2
|
||||
|
||||
return &Provider{
|
||||
data: data,
|
||||
resolve: func(ctx context.Context, key config.Key) (string, string) {
|
||||
keys := strings.SplitN(key.Name, "/", nameParts)
|
||||
if len(keys) == 1 {
|
||||
return "", keys[0]
|
||||
resolve: func(path []string) (string, string) {
|
||||
if len(path) == 1 {
|
||||
return "", path[0]
|
||||
}
|
||||
|
||||
return keys[0], keys[1]
|
||||
return strings.Join(path[:len(path)-1], Separator), strings.ToUpper(path[len(path)-1])
|
||||
},
|
||||
name: Name,
|
||||
}
|
||||
}
|
||||
|
||||
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 != ""
|
||||
resolve func(path []string) (string, string)
|
||||
name string
|
||||
}
|
||||
|
||||
func (p *Provider) Name() string {
|
||||
return "ini"
|
||||
return p.name
|
||||
}
|
||||
|
||||
func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, error) {
|
||||
section, name := p.resolve(ctx, key)
|
||||
func (p *Provider) Value(ctx context.Context, path ...string) (config.Value, error) {
|
||||
section, name := p.resolve(path)
|
||||
|
||||
iniSection, err := p.data.GetSection(section)
|
||||
if err != nil {
|
||||
return config.Variable{}, fmt.Errorf("%w: %s: %w", config.ErrVariableNotFound, p.Name(), err)
|
||||
return nil, fmt.Errorf("%w: %s: %w", config.ErrValueNotFound, p.Name(), err)
|
||||
}
|
||||
|
||||
iniKey, err := iniSection.GetKey(name)
|
||||
if err != nil {
|
||||
return config.Variable{}, fmt.Errorf("%w: %s: %w", config.ErrVariableNotFound, p.Name(), err)
|
||||
return nil, fmt.Errorf("%w: %s: %w", config.ErrValueNotFound, p.Name(), err)
|
||||
}
|
||||
|
||||
return config.Variable{
|
||||
Name: section + ":" + name,
|
||||
Provider: p.Name(),
|
||||
Value: value.JString(iniKey.String()),
|
||||
}, nil
|
||||
return value.JString(iniKey.String()), nil
|
||||
}
|
||||
|
||||
@@ -14,13 +14,13 @@ func TestProvider(t *testing.T) {
|
||||
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),
|
||||
test.NewRead("To Do, In Progress, Done", "project", "PROJECT_BOARD_BASIC_KANBAN_TYPE"),
|
||||
test.NewRead("markdown", "repository.editor", "PREVIEWABLE_FILE_MODES"),
|
||||
test.NewRead("http://0.0.0.0:3000/", "server", "LOCAL_ROOT_URL"),
|
||||
test.NewRead(20*time.Minute, "server", "LFS_HTTP_AUTH_EXPIRY"),
|
||||
test.NewRead(5120, "repository.pull-request", "DEFAULT_MERGE_MESSAGE_SIZE"),
|
||||
test.NewRead(true, "ui", "SHOW_USER_EMAIL"),
|
||||
test.NewRead(false, "cors", "enabled"),
|
||||
}
|
||||
|
||||
prov := ini.New(file)
|
||||
|
||||
Reference in New Issue
Block a user