Merge pull request 'add dasel provider' (#23) from dasel into master
All checks were successful
Go Action / goaction (push) Successful in 53s

Reviewed-on: #23
This commit was merged in pull request #23.
This commit is contained in:
2025-12-27 16:12:16 +03:00
11 changed files with 332 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
name: Go Action
on:
push:
branches:
- master
paths:
- 'provider/dasel/**'
- '.gitea/workflows/dasel.yml'
pull_request:
paths:
- 'provider/dasel/**'
- '.gitea/workflows/dasel.yml'
jobs:
goaction:
runs-on: ubuntu-latest # Use a Gitea Actions runner label
steps:
- name: Check out repository code
uses: actions/checkout@v4 # Action to clone the repo
- name: Set up Go
uses: actions/setup-go@v5 # Action to install a specific Go version
with:
go-version: '1.25.5' # Specify your required Go version
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v8 # Use the golangci-lint action
with:
version: v2.7.2 # Specify the linter version
# Optional: additional arguments
args: --verbose
working-directory: ./provider/dasel
- name: Run go test
run: go test ./...
working-directory: ./provider/dasel

View File

@@ -18,6 +18,9 @@ on:
- '.gitea/workflows/vault.yml'
- 'provider/yaml/**'
- '.gitea/workflows/yaml.yml'
- 'provider/dasel/**'
- '.gitea/workflows/dasel.yml'
jobs:

21
provider/dasel/go.mod Normal file
View File

@@ -0,0 +1,21 @@
module gitoa.ru/go-4devs/config/provider/dasel
go 1.25.5
require (
github.com/tomwright/dasel/v3 v3.2.0
gitoa.ru/go-4devs/config v0.0.6
)
require (
github.com/agext/levenshtein v1.2.1 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/hashicorp/hcl/v2 v2.24.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/zclconf/go-cty v1.17.0 // indirect
golang.org/x/mod v0.26.0 // indirect
golang.org/x/sync v0.16.0 // indirect
golang.org/x/text v0.28.0 // indirect
golang.org/x/tools v0.35.0 // indirect
)

30
provider/dasel/go.sum Normal file
View File

@@ -0,0 +1,30 @@
github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8=
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY=
github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4=
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/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/hashicorp/hcl/v2 v2.24.0 h1:2QJdZ454DSsYGoaE6QheQZjtKZSUs9Nh2izTWiwQxvE=
github.com/hashicorp/hcl/v2 v2.24.0/go.mod h1:oGoO1FIQYfn/AgyOhlg9qLC6/nOJPX3qGbkZpYAcqfM=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
github.com/tomwright/dasel/v3 v3.2.0 h1:wyF2A4jVADx10E0kjbzPTiaF26D0OkK2OPuQRPJRDCo=
github.com/tomwright/dasel/v3 v3.2.0/go.mod h1:XyAl6LidZuWOISIeUmKlCqJDz4IWEDp83epNngZgOQA=
github.com/zclconf/go-cty v1.17.0 h1:seZvECve6XX4tmnvRzWtJNHdscMtYEx5R7bnnVyd/d0=
github.com/zclconf/go-cty v1.17.0/go.mod h1:wqFzcImaLTI6A5HfsRwB0nj5n0MRZFwmey8YoFPPs3U=
github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940 h1:4r45xpDWB6ZMSMNJFMOjqrGHynW3DIBuR2H9j0ug+Mo=
github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940/go.mod h1:CmBdvvj3nqzfzJ6nTCIwDTPZ56aVGvDrmztiO5g3qrM=
gitoa.ru/go-4devs/config v0.0.6 h1:BbOH2KHBRMWtSwcFyHKzMXzxYbJpSwZpA8LGnnzBwk8=
gitoa.ru/go-4devs/config v0.0.6/go.mod h1:UINWnObZA0nLiJro+TtavUBBvN0cSt17aRHOk20pP74=
golang.org/x/mod v0.26.0 h1:EGMPT//Ezu+ylkCijjPc+f4Aih7sZvaAr+O3EHBxvZg=
golang.org/x/mod v0.26.0/go.mod h1:/j6NAhSk8iQ723BGAUyoAcn7SlD7s15Dp9Nd/SfeaFQ=
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng=
golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
golang.org/x/tools v0.35.0 h1:mBffYraMEf7aa0sB+NuKnuCy8qI/9Bughn8dC2Gu5r0=
golang.org/x/tools v0.35.0/go.mod h1:NKdj5HkL/73byiZSJjqJgKn3ep7KjFkBOkR/Hps3VPw=

View File

@@ -0,0 +1,18 @@
app {
name {
var = [
"name"
]
title = "config title"
timeout = "1m"
success = true
}
}
cfg {
duration = 1260000000000
enabled = true
type = "json"
}

View File

@@ -0,0 +1,28 @@
package hcl
import (
"fmt"
"github.com/tomwright/dasel/v3/parsing"
"github.com/tomwright/dasel/v3/parsing/hcl"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/provider/dasel"
)
const Name = "dasel:hcl"
func New(data []byte) (dasel.Provider, error) {
readOption := parsing.DefaultReaderOptions()
reader, err := hcl.HCL.NewReader(readOption)
if err != nil {
return dasel.Provider{}, fmt.Errorf("%w:%w", config.ErrInitFactory, err)
}
val, verr := reader.Read(data)
if verr != nil {
return dasel.Provider{}, fmt.Errorf("%w:%w", config.ErrInitFactory, verr)
}
return dasel.New(val, dasel.WithName(Name)), nil
}

View File

@@ -0,0 +1,35 @@
package hcl_test
import (
"embed"
"testing"
"time"
dhcl "gitoa.ru/go-4devs/config/provider/dasel/hcl"
"gitoa.ru/go-4devs/config/test"
"gitoa.ru/go-4devs/config/test/require"
)
//go:embed fixture/*
var fixture embed.FS
func TestProvider(t *testing.T) {
t.Parallel()
js, err := fixture.ReadFile("fixture/config.hcl")
require.NoError(t, err)
prov, derr := dhcl.New(js)
require.NoError(t, derr)
sl := []string{}
read := []test.Read{
test.NewRead("config title", "app", "name", "title"),
test.NewRead(time.Minute, "app.name.timeout"),
test.NewReadUnmarshal(&[]string{"name"}, &sl, "app.name.var"),
test.NewReadConfig("cfg"),
test.NewRead(true, "app", "name", "success"),
}
test.Run(t, prov, read)
}

View File

@@ -0,0 +1,17 @@
{
"app": {
"name": {
"var": [
"name"
],
"title": "config title",
"timeout": "1m",
"success": true
}
},
"cfg": {
"duration": 1260000000000,
"enabled": true,
"type":"json"
}
}

View File

@@ -0,0 +1,28 @@
package json //nolint:revive
import (
"fmt"
"github.com/tomwright/dasel/v3/parsing"
"github.com/tomwright/dasel/v3/parsing/json"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/provider/dasel"
)
const Name = "dasel:json"
func New(data []byte) (dasel.Provider, error) {
readOption := parsing.DefaultReaderOptions()
reader, err := json.JSON.NewReader(readOption)
if err != nil {
return dasel.Provider{}, fmt.Errorf("%w:%w", config.ErrInitFactory, err)
}
val, verr := reader.Read(data)
if verr != nil {
return dasel.Provider{}, fmt.Errorf("%w:%w", config.ErrInitFactory, verr)
}
return dasel.New(val, dasel.WithName(Name)), nil
}

View File

@@ -0,0 +1,35 @@
package json_test
import (
"embed"
"testing"
"time"
djson "gitoa.ru/go-4devs/config/provider/dasel/json"
"gitoa.ru/go-4devs/config/test"
"gitoa.ru/go-4devs/config/test/require"
)
//go:embed fixture/*
var fixture embed.FS
func TestProvider(t *testing.T) {
t.Parallel()
js, err := fixture.ReadFile("fixture/config.json")
require.NoError(t, err)
prov, derr := djson.New(js)
require.NoError(t, derr)
sl := []string{}
read := []test.Read{
test.NewRead("config title", "app.name.title"),
test.NewRead(time.Minute, "app.name.timeout"),
test.NewReadUnmarshal(&[]string{"name"}, &sl, "app.name.var"),
test.NewReadConfig("cfg"),
test.NewRead(true, "app", "name", "success"),
}
test.Run(t, prov, read)
}

View File

@@ -0,0 +1,79 @@
package dasel
import (
"context"
"encoding/json"
"fmt"
"strings"
dasel "github.com/tomwright/dasel/v3"
"github.com/tomwright/dasel/v3/model"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/value"
)
var _ config.Provider = New(nil)
const Name = "dasel"
type Option func(*Provider)
func WithName(in string) Option {
return func(p *Provider) {
p.name = in
}
}
func New(data *model.Value, opts ...Option) Provider {
prov := Provider{
data: data,
key: func(path ...string) string {
return strings.Join(path, ".")
},
name: Name,
}
for _, opt := range opts {
opt(&prov)
}
return prov
}
type Provider struct {
data *model.Value
key func(path ...string) string
name string
}
func (p Provider) Value(ctx context.Context, path ...string) (config.Value, error) {
selector := p.key(path...)
data, cnt, err := dasel.Query(ctx, p.data, selector)
if err != nil {
return nil, fmt.Errorf("query: %w:%w", config.ErrInvalidValue, err)
}
if cnt > 1 {
return nil, fmt.Errorf("count: %v:%w", cnt, config.ErrToManyArgs)
}
if cnt == 0 {
return value.EmptyValue(), nil
}
val, verr := data[0].GoValue()
if verr != nil {
return nil, fmt.Errorf("go value: %w:%w", config.ErrInvalidValue, verr)
}
res, merr := json.Marshal(val)
if merr != nil {
return nil, fmt.Errorf("marshal: %w:%w", config.ErrInvalidValue, merr)
}
return value.JBytes(res), nil
}
func (p Provider) Name() string {
return p.name
}