Browse Source

Merge pull request 'add provider ini' (#6) from ini into master

Reviewed-on: https://gitoa.ru/go-4devs/config/pulls/6
pull/7/head
andrey 10 months ago
parent
commit
18ce790363
  1. 20
      .drone.yml
  2. 1038
      provider/ini/fixture/config.ini
  3. 15
      provider/ini/go.mod
  4. 14
      provider/ini/go.sum
  5. 58
      provider/ini/provider.go
  6. 38
      provider/ini/provider_test.go

20
.drone.yml

@ -52,3 +52,23 @@ steps:
commands:
- cd provider/yaml
- golangci-lint run
---
kind: pipeline
type: docker
name: ini
steps:
- name: test
image: golang
failure: ignore # runtime/cgo: pthread_create failed: Operation not permitted
commands:
- cd provider/ini
- go test ./...
- name: golangci-lint
image: golangci/golangci-lint:v1.55
commands:
- cd provider/ini
- golangci-lint run

1038
provider/ini/fixture/config.ini

File diff suppressed because it is too large

15
provider/ini/go.mod

@ -0,0 +1,15 @@
module gitoa.ru/go-4devs/config/provider/ini
go 1.21
require (
github.com/stretchr/testify v1.8.4
gitoa.ru/go-4devs/config v0.0.1
gopkg.in/ini.v1 v1.67.0
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

14
provider/ini/go.sum

@ -0,0 +1,14 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gitoa.ru/go-4devs/config v0.0.1 h1:9KrOO09YbIMO8qL8aVn/G74DurGdOIW5y3O02bays4I=
gitoa.ru/go-4devs/config v0.0.1/go.mod h1:xfEC2Al9xnMLJUuekYs3KhJ5BIzWAseNwkMwbN6/xss=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

58
provider/ini/provider.go

@ -0,0 +1,58 @@
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(_ 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
}

38
provider/ini/provider_test.go

@ -0,0 +1,38 @@
package ini_test
import (
"embed"
"testing"
"time"
"github.com/stretchr/testify/require"
"gitoa.ru/go-4devs/config/provider/ini"
"gitoa.ru/go-4devs/config/test"
lib "gopkg.in/ini.v1"
)
//go:embed fixture/*
var fixtures embed.FS
func TestProvider(t *testing.T) {
t.Parallel()
data, derr := fixtures.ReadFile("fixture/config.ini")
require.NoError(t, derr)
file, err := lib.Load(data)
require.NoError(t, err)
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)
}
Loading…
Cancel
Save