This commit is contained in:
@@ -1,58 +0,0 @@
|
||||
package ini
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
"gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
const (
|
||||
Name = "ini"
|
||||
Separator = "."
|
||||
)
|
||||
|
||||
var _ config.Provider = (*Provider)(nil)
|
||||
|
||||
func New(data *ini.File) *Provider {
|
||||
return &Provider{
|
||||
data: data,
|
||||
resolve: func(path []string) (string, string) {
|
||||
if len(path) == 1 {
|
||||
return "", path[0]
|
||||
}
|
||||
|
||||
return strings.Join(path[:len(path)-1], Separator), strings.ToUpper(path[len(path)-1])
|
||||
},
|
||||
name: Name,
|
||||
}
|
||||
}
|
||||
|
||||
type Provider struct {
|
||||
data *ini.File
|
||||
resolve func(path []string) (string, string)
|
||||
name string
|
||||
}
|
||||
|
||||
func (p *Provider) Name() string {
|
||||
return p.name
|
||||
}
|
||||
|
||||
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 nil, fmt.Errorf("%w: %s: %w", config.ErrValueNotFound, p.Name(), err)
|
||||
}
|
||||
|
||||
iniKey, err := iniSection.GetKey(name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: %s: %w", config.ErrValueNotFound, p.Name(), err)
|
||||
}
|
||||
|
||||
return value.JString(iniKey.String()), nil
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
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("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)
|
||||
test.Run(t, prov, read)
|
||||
}
|
||||
Reference in New Issue
Block a user