Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
bb3f6c2a7c | |||
b579ffb5ea | |||
6b4202956e | |||
37e0380939 | |||
|
3cac13e35b | ||
d52e22c187 | |||
3cc1cf899a | |||
89b41364c3 | |||
d7ffcd19f8 | |||
6b27204711 | |||
4bee1a7041 |
29
.gitea/workflows/job_lint.yml
Normal file
29
.gitea/workflows/job_lint.yml
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
name: lint
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
types: [opened, reopened, synchronize]
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- v3
|
||||||
|
- v4
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
lint:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
filter: 'blob:none'
|
||||||
|
- name: setup go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
cache-dependency-path: "**/*.sum"
|
||||||
|
go-version: 'stable'
|
||||||
|
- name: setup deps
|
||||||
|
run: go get -v ./...
|
||||||
|
- name: run lint
|
||||||
|
uses: https://github.com/golangci/golangci-lint-action@v6
|
||||||
|
with:
|
||||||
|
version: 'latest'
|
34
.gitea/workflows/job_test.yml
Normal file
34
.gitea/workflows/job_test.yml
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
name: test
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
types: [opened, reopened, synchronize]
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- v3
|
||||||
|
- v4
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- v3
|
||||||
|
- v4
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
filter: 'blob:none'
|
||||||
|
- name: setup go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
cache-dependency-path: "**/*.sum"
|
||||||
|
go-version: 'stable'
|
||||||
|
- name: setup deps
|
||||||
|
run: go get -v ./...
|
||||||
|
- name: run test
|
||||||
|
env:
|
||||||
|
INTEGRATION_TESTS: yes
|
||||||
|
run: go test -mod readonly -v ./...
|
53
.gitea/workflows/job_tests.yml
Normal file
53
.gitea/workflows/job_tests.yml
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
name: test
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
types: [opened, reopened, synchronize]
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- v3
|
||||||
|
- v4
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- v3
|
||||||
|
- v4
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
filter: 'blob:none'
|
||||||
|
- name: checkout tests
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
ref: master
|
||||||
|
filter: 'blob:none'
|
||||||
|
repository: unistack-org/micro-tests
|
||||||
|
path: micro-tests
|
||||||
|
- name: setup go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
cache-dependency-path: "**/*.sum"
|
||||||
|
go-version: 'stable'
|
||||||
|
- name: setup go work
|
||||||
|
env:
|
||||||
|
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
||||||
|
run: |
|
||||||
|
go work init
|
||||||
|
go work use .
|
||||||
|
go work use micro-tests
|
||||||
|
- name: setup deps
|
||||||
|
env:
|
||||||
|
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
||||||
|
run: go get -v ./...
|
||||||
|
- name: run tests
|
||||||
|
env:
|
||||||
|
INTEGRATION_TESTS: yes
|
||||||
|
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
||||||
|
run: |
|
||||||
|
cd micro-tests
|
||||||
|
go test -mod readonly -v ./... || true
|
19
.github/dependabot.yml
vendored
19
.github/dependabot.yml
vendored
@@ -1,19 +0,0 @@
|
|||||||
# To get started with Dependabot version updates, you'll need to specify which
|
|
||||||
# package ecosystems to update and where the package manifests are located.
|
|
||||||
# Please see the documentation for all configuration options:
|
|
||||||
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
|
||||||
|
|
||||||
version: 2
|
|
||||||
updates:
|
|
||||||
|
|
||||||
# Maintain dependencies for GitHub Actions
|
|
||||||
- package-ecosystem: "github-actions"
|
|
||||||
directory: "/"
|
|
||||||
schedule:
|
|
||||||
interval: "daily"
|
|
||||||
|
|
||||||
# Maintain dependencies for Golang
|
|
||||||
- package-ecosystem: "gomod"
|
|
||||||
directory: "/"
|
|
||||||
schedule:
|
|
||||||
interval: "daily"
|
|
20
.github/workflows/autoapprove.yml
vendored
20
.github/workflows/autoapprove.yml
vendored
@@ -1,20 +0,0 @@
|
|||||||
name: "autoapprove"
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request_target:
|
|
||||||
types: [assigned, opened, synchronize, reopened]
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
pull-requests: write
|
|
||||||
contents: write
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
autoapprove:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: approve
|
|
||||||
uses: hmarr/auto-approve-action@v3
|
|
||||||
if: github.actor == 'vtolstov' || github.actor == 'dependabot[bot]'
|
|
||||||
id: approve
|
|
||||||
with:
|
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
21
.github/workflows/automerge.yml
vendored
21
.github/workflows/automerge.yml
vendored
@@ -1,21 +0,0 @@
|
|||||||
name: "automerge"
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request_target:
|
|
||||||
types: [assigned, opened, synchronize, reopened]
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
pull-requests: write
|
|
||||||
contents: write
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
automerge:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
if: github.actor == 'vtolstov'
|
|
||||||
steps:
|
|
||||||
- name: merge
|
|
||||||
id: merge
|
|
||||||
run: gh pr merge --auto --merge "$PR_URL"
|
|
||||||
env:
|
|
||||||
PR_URL: ${{github.event.pull_request.html_url}}
|
|
||||||
GITHUB_TOKEN: ${{secrets.TOKEN}}
|
|
47
.github/workflows/build.yml
vendored
47
.github/workflows/build.yml
vendored
@@ -1,47 +0,0 @@
|
|||||||
name: build
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- v3
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
name: test
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: setup
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: 1.17
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: cache
|
|
||||||
uses: actions/cache@v3
|
|
||||||
with:
|
|
||||||
path: ~/go/pkg/mod
|
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
|
||||||
restore-keys: ${{ runner.os }}-go-
|
|
||||||
- name: deps
|
|
||||||
run: go get -v -t -d ./...
|
|
||||||
- name: test
|
|
||||||
env:
|
|
||||||
INTEGRATION_TESTS: yes
|
|
||||||
run: go test -mod readonly -v ./...
|
|
||||||
lint:
|
|
||||||
name: lint
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: lint
|
|
||||||
uses: golangci/golangci-lint-action@v3.4.0
|
|
||||||
continue-on-error: true
|
|
||||||
with:
|
|
||||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
|
||||||
version: v1.30
|
|
||||||
# Optional: working directory, useful for monorepos
|
|
||||||
# working-directory: somedir
|
|
||||||
# Optional: golangci-lint command line arguments.
|
|
||||||
# args: --issues-exit-code=0
|
|
||||||
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
|
||||||
# only-new-issues: true
|
|
78
.github/workflows/codeql-analysis.yml
vendored
78
.github/workflows/codeql-analysis.yml
vendored
@@ -1,78 +0,0 @@
|
|||||||
# For most projects, this workflow file will not need changing; you simply need
|
|
||||||
# to commit it to your repository.
|
|
||||||
#
|
|
||||||
# You may wish to alter this file to override the set of languages analyzed,
|
|
||||||
# or to provide custom queries or build logic.
|
|
||||||
#
|
|
||||||
# ******** NOTE ********
|
|
||||||
# We have attempted to detect the languages in your repository. Please check
|
|
||||||
# the `language` matrix defined below to confirm you have the correct set of
|
|
||||||
# supported CodeQL languages.
|
|
||||||
#
|
|
||||||
name: "codeql"
|
|
||||||
|
|
||||||
on:
|
|
||||||
workflow_run:
|
|
||||||
workflows: ["prbuild"]
|
|
||||||
types:
|
|
||||||
- completed
|
|
||||||
push:
|
|
||||||
branches: [ master, v3 ]
|
|
||||||
pull_request:
|
|
||||||
# The branches below must be a subset of the branches above
|
|
||||||
branches: [ master, v3 ]
|
|
||||||
schedule:
|
|
||||||
- cron: '34 1 * * 0'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
analyze:
|
|
||||||
name: analyze
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
permissions:
|
|
||||||
actions: read
|
|
||||||
contents: read
|
|
||||||
security-events: write
|
|
||||||
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
language: [ 'go' ]
|
|
||||||
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
|
|
||||||
# Learn more:
|
|
||||||
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: setup
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: 1.17
|
|
||||||
# Initializes the CodeQL tools for scanning.
|
|
||||||
- name: init
|
|
||||||
uses: github/codeql-action/init@v2
|
|
||||||
with:
|
|
||||||
languages: ${{ matrix.language }}
|
|
||||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
|
||||||
# By default, queries listed here will override any specified in a config file.
|
|
||||||
# Prefix the list here with "+" to use these queries and those in the config file.
|
|
||||||
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
|
||||||
|
|
||||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
|
||||||
# If this step fails, then you should remove it and run the build manually (see below)
|
|
||||||
- name: autobuild
|
|
||||||
uses: github/codeql-action/autobuild@v2
|
|
||||||
|
|
||||||
# ℹ️ Command-line programs to run using the OS shell.
|
|
||||||
# 📚 https://git.io/JvXDl
|
|
||||||
|
|
||||||
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
|
||||||
# and modify them (or add more) to build your code if your project
|
|
||||||
# uses a compiled language
|
|
||||||
|
|
||||||
#- run: |
|
|
||||||
# make bootstrap
|
|
||||||
# make release
|
|
||||||
|
|
||||||
- name: analyze
|
|
||||||
uses: github/codeql-action/analyze@v2
|
|
27
.github/workflows/dependabot-automerge.yml
vendored
27
.github/workflows/dependabot-automerge.yml
vendored
@@ -1,27 +0,0 @@
|
|||||||
name: "dependabot-automerge"
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request_target:
|
|
||||||
types: [assigned, opened, synchronize, reopened]
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
pull-requests: write
|
|
||||||
contents: write
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
automerge:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
if: github.actor == 'dependabot[bot]'
|
|
||||||
steps:
|
|
||||||
- name: metadata
|
|
||||||
id: metadata
|
|
||||||
uses: dependabot/fetch-metadata@v1.3.6
|
|
||||||
with:
|
|
||||||
github-token: "${{ secrets.TOKEN }}"
|
|
||||||
- name: merge
|
|
||||||
id: merge
|
|
||||||
if: ${{contains(steps.metadata.outputs.dependency-names, 'go.unistack.org')}}
|
|
||||||
run: gh pr merge --auto --merge "$PR_URL"
|
|
||||||
env:
|
|
||||||
PR_URL: ${{github.event.pull_request.html_url}}
|
|
||||||
GITHUB_TOKEN: ${{secrets.TOKEN}}
|
|
47
.github/workflows/pr.yml
vendored
47
.github/workflows/pr.yml
vendored
@@ -1,47 +0,0 @@
|
|||||||
name: prbuild
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- v3
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
name: test
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: setup
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: 1.17
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: cache
|
|
||||||
uses: actions/cache@v3
|
|
||||||
with:
|
|
||||||
path: ~/go/pkg/mod
|
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
|
||||||
restore-keys: ${{ runner.os }}-go-
|
|
||||||
- name: deps
|
|
||||||
run: go get -v -t -d ./...
|
|
||||||
- name: test
|
|
||||||
env:
|
|
||||||
INTEGRATION_TESTS: yes
|
|
||||||
run: go test -mod readonly -v ./...
|
|
||||||
lint:
|
|
||||||
name: lint
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: lint
|
|
||||||
uses: golangci/golangci-lint-action@v3.4.0
|
|
||||||
continue-on-error: true
|
|
||||||
with:
|
|
||||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
|
||||||
version: v1.30
|
|
||||||
# Optional: working directory, useful for monorepos
|
|
||||||
# working-directory: somedir
|
|
||||||
# Optional: golangci-lint command line arguments.
|
|
||||||
# args: --issues-exit-code=0
|
|
||||||
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
|
||||||
# only-new-issues: true
|
|
5
.golangci.yml
Normal file
5
.golangci.yml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
run:
|
||||||
|
concurrency: 8
|
||||||
|
deadline: 5m
|
||||||
|
issues-exit-code: 1
|
||||||
|
tests: true
|
151
file.go
151
file.go
@@ -1,23 +1,28 @@
|
|||||||
package file // import "go.unistack.org/micro-config-file/v3"
|
package file
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
"github.com/imdario/mergo"
|
"dario.cat/mergo"
|
||||||
"go.unistack.org/micro/v3/codec"
|
"go.unistack.org/micro/v4/config"
|
||||||
"go.unistack.org/micro/v3/config"
|
rutil "go.unistack.org/micro/v4/util/reflect"
|
||||||
rutil "go.unistack.org/micro/v3/util/reflect"
|
"golang.org/x/text/transform"
|
||||||
)
|
)
|
||||||
|
|
||||||
var DefaultStructTag = "file"
|
var (
|
||||||
|
DefaultStructTag = "file"
|
||||||
|
MaxFileSize int64 = 1 * 1024 * 1024
|
||||||
|
)
|
||||||
|
|
||||||
type fileConfig struct {
|
type fileConfig struct {
|
||||||
opts config.Options
|
opts config.Options
|
||||||
path string
|
path string
|
||||||
|
reader io.Reader
|
||||||
|
transformer transform.Transformer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *fileConfig) Options() config.Options {
|
func (c *fileConfig) Options() config.Options {
|
||||||
@@ -37,13 +42,16 @@ func (c *fileConfig) Init(opts ...config.Option) error {
|
|||||||
if v, ok := c.opts.Context.Value(pathKey{}).(string); ok {
|
if v, ok := c.opts.Context.Value(pathKey{}).(string); ok {
|
||||||
c.path = v
|
c.path = v
|
||||||
}
|
}
|
||||||
|
if v, ok := c.opts.Context.Value(transformerKey{}).(transform.Transformer); ok {
|
||||||
|
c.transformer = v
|
||||||
|
}
|
||||||
|
if v, ok := c.opts.Context.Value(readerKey{}).(io.Reader); ok {
|
||||||
|
c.reader = v
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.path == "" {
|
if c.opts.Codec == nil {
|
||||||
err := fmt.Errorf("file path not exists: %v", c.path)
|
return fmt.Errorf("Codec must be specified")
|
||||||
if !c.opts.AllowFail {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
||||||
@@ -63,17 +71,40 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
path := c.path
|
path := c.path
|
||||||
|
transformer := c.transformer
|
||||||
|
reader := c.reader
|
||||||
|
|
||||||
options := config.NewLoadOptions(opts...)
|
options := config.NewLoadOptions(opts...)
|
||||||
if options.Context != nil {
|
if options.Context != nil {
|
||||||
if v, ok := options.Context.Value(pathKey{}).(string); ok && v != "" {
|
if v, ok := options.Context.Value(pathKey{}).(string); ok && v != "" {
|
||||||
path = v
|
path = v
|
||||||
}
|
}
|
||||||
|
if v, ok := c.opts.Context.Value(transformerKey{}).(transform.Transformer); ok {
|
||||||
|
transformer = v
|
||||||
|
}
|
||||||
|
if v, ok := c.opts.Context.Value(readerKey{}).(io.Reader); ok {
|
||||||
|
reader = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var fp io.Reader
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if path != "" {
|
||||||
|
fp, err = os.OpenFile(path, os.O_RDONLY, os.FileMode(0o400))
|
||||||
|
} else if c.reader != nil {
|
||||||
|
fp = reader
|
||||||
|
} else {
|
||||||
|
err = fmt.Errorf("Path or Reader must be specified")
|
||||||
}
|
}
|
||||||
|
|
||||||
fp, err := os.OpenFile(path, os.O_RDONLY, os.FileMode(0400))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !c.opts.AllowFail {
|
if !c.opts.AllowFail {
|
||||||
|
if c.path != "" {
|
||||||
return fmt.Errorf("file load path %s error: %w", path, err)
|
return fmt.Errorf("file load path %s error: %w", path, err)
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("file load error: %w", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if err = config.DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
if err = config.DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
||||||
return err
|
return err
|
||||||
@@ -82,9 +113,18 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
defer fp.Close()
|
if fpc, ok := fp.(io.Closer); ok {
|
||||||
|
defer fpc.Close()
|
||||||
|
}
|
||||||
|
|
||||||
buf, err := ioutil.ReadAll(io.LimitReader(fp, int64(codec.DefaultMaxMsgSize)))
|
var r io.Reader
|
||||||
|
if transformer != nil {
|
||||||
|
r = transform.NewReader(fp, c.transformer)
|
||||||
|
} else {
|
||||||
|
r = fp
|
||||||
|
}
|
||||||
|
|
||||||
|
buf, err := io.ReadAll(io.LimitReader(r, MaxFileSize))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !c.opts.AllowFail {
|
if !c.opts.AllowFail {
|
||||||
return err
|
return err
|
||||||
@@ -162,7 +202,7 @@ func (c *fileConfig) Save(ctx context.Context, opts ...config.SaveOption) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
fp, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, os.FileMode(0600))
|
fp, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, os.FileMode(0o600))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !c.opts.AllowFail {
|
if !c.opts.AllowFail {
|
||||||
return err
|
return err
|
||||||
@@ -228,3 +268,82 @@ func NewConfig(opts ...config.Option) config.Config {
|
|||||||
}
|
}
|
||||||
return &fileConfig{opts: options}
|
return &fileConfig{opts: options}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type EnvTransformer struct {
|
||||||
|
maxMatchSize int
|
||||||
|
Regexp *regexp.Regexp
|
||||||
|
TransformerFunc TransformerFunc
|
||||||
|
overflow []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ transform.Transformer = (*EnvTransformer)(nil)
|
||||||
|
|
||||||
|
// Transform implements golang.org/x/text/transform#Transformer
|
||||||
|
func (t *EnvTransformer) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
|
||||||
|
t.maxMatchSize = 1024
|
||||||
|
var n int
|
||||||
|
// copy any overflow from the last call
|
||||||
|
if len(t.overflow) > 0 {
|
||||||
|
n, err = fullcopy(dst, t.overflow)
|
||||||
|
nDst += n
|
||||||
|
if err != nil {
|
||||||
|
t.overflow = t.overflow[n:]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t.overflow = nil
|
||||||
|
}
|
||||||
|
for _, index := range t.Regexp.FindAllSubmatchIndex(src, -1) {
|
||||||
|
// copy everything up to the match
|
||||||
|
n, err = fullcopy(dst[nDst:], src[nSrc:index[0]])
|
||||||
|
nSrc += n
|
||||||
|
nDst += n
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// skip the match if it ends at the end the src buffer.
|
||||||
|
// it could potentially match more
|
||||||
|
if index[1] == len(src) && !atEOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// copy the replacement
|
||||||
|
rep := t.TransformerFunc(src, index)
|
||||||
|
n, err = fullcopy(dst[nDst:], rep)
|
||||||
|
nDst += n
|
||||||
|
nSrc = index[1]
|
||||||
|
if err != nil {
|
||||||
|
t.overflow = rep[n:]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// if we're at the end, tack on any remaining bytes
|
||||||
|
if atEOF {
|
||||||
|
n, err = fullcopy(dst[nDst:], src[nSrc:])
|
||||||
|
nDst += n
|
||||||
|
nSrc += n
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// skip any bytes which exceede the max match size
|
||||||
|
if skip := len(src[nSrc:]) - t.maxMatchSize; skip > 0 {
|
||||||
|
n, err = fullcopy(dst[nDst:], src[nSrc:nSrc+skip])
|
||||||
|
nSrc += n
|
||||||
|
nDst += n
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = transform.ErrShortSrc
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset resets the state and allows a Transformer to be reused.
|
||||||
|
func (t *EnvTransformer) Reset() {
|
||||||
|
t.overflow = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func fullcopy(dst, src []byte) (n int, err error) {
|
||||||
|
n = copy(dst, src)
|
||||||
|
if n < len(src) {
|
||||||
|
err = transform.ErrShortDst
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
59
file_test.go
Normal file
59
file_test.go
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
package file
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"go.unistack.org/micro/v4/codec"
|
||||||
|
"go.unistack.org/micro/v4/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
type jsoncodec struct{}
|
||||||
|
|
||||||
|
func (*jsoncodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
||||||
|
return json.Marshal(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*jsoncodec) Unmarshal(buf []byte, v interface{}, opts ...codec.Option) error {
|
||||||
|
return json.Unmarshal(buf, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*jsoncodec) String() string {
|
||||||
|
return "json"
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadReplace(t *testing.T) {
|
||||||
|
type Config struct {
|
||||||
|
Key string
|
||||||
|
Pass string
|
||||||
|
}
|
||||||
|
os.Setenv("PLACEHOLDER", "test")
|
||||||
|
cfg := &Config{}
|
||||||
|
ctx := context.TODO()
|
||||||
|
buf := bytes.NewReader([]byte(`{"key":"val","pass":"${PLACEHOLDER}"}`))
|
||||||
|
tr, err := NewEnvTransformer(`(?s)\$\{.*?\}`, 2, 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
c := NewConfig(config.Codec(
|
||||||
|
&jsoncodec{}),
|
||||||
|
config.Struct(cfg),
|
||||||
|
Reader(buf),
|
||||||
|
Transformer(tr),
|
||||||
|
)
|
||||||
|
|
||||||
|
if err := c.Init(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.Load(ctx); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Pass != "test" {
|
||||||
|
t.Fatalf("not works %#+v\n", cfg)
|
||||||
|
}
|
||||||
|
}
|
22
go.mod
22
go.mod
@@ -1,10 +1,22 @@
|
|||||||
module go.unistack.org/micro-config-file/v3
|
module go.unistack.org/micro-config-file/v4
|
||||||
|
|
||||||
go 1.18
|
go 1.22.0
|
||||||
|
|
||||||
|
toolchain go1.24.1
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/imdario/mergo v0.3.16
|
dario.cat/mergo v1.0.1
|
||||||
go.unistack.org/micro/v3 v3.10.36
|
go.unistack.org/micro/v4 v4.1.3
|
||||||
|
golang.org/x/text v0.21.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require github.com/google/uuid v1.5.0 // indirect
|
require (
|
||||||
|
github.com/ash3in/uuidv8 v1.2.0 // indirect
|
||||||
|
github.com/google/go-cmp v0.6.0 // indirect
|
||||||
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
|
github.com/matoous/go-nanoid v1.5.1 // indirect
|
||||||
|
github.com/spf13/cast v1.7.1 // indirect
|
||||||
|
go.unistack.org/micro-proto/v4 v4.1.0 // indirect
|
||||||
|
google.golang.org/protobuf v1.36.3 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
)
|
||||||
|
45
go.sum
45
go.sum
@@ -1,16 +1,35 @@
|
|||||||
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
|
dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
|
||||||
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
|
||||||
github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
|
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
|
||||||
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
|
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
|
||||||
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
|
github.com/ash3in/uuidv8 v1.2.0 h1:2oogGdtCPwaVtyvPPGin4TfZLtOGE5F+W++E880G6SI=
|
||||||
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
|
github.com/ash3in/uuidv8 v1.2.0/go.mod h1:BnU0wJBxnzdEKmVg4xckBkD+VZuecTFTUP3M0dWgyY4=
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
||||||
github.com/silas/dag v0.0.0-20211117232152-9d50aa809f35/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
||||||
go.unistack.org/micro/v3 v3.10.16 h1:2er/SKKYbV60M+UuJM4eYCF0MZYAIq/yNUrAbTfgq8Q=
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
go.unistack.org/micro/v3 v3.10.16/go.mod h1:uMAc0U/x7dmtICCrblGf0ZLgYegu3VwQAquu+OFCw1Q=
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
go.unistack.org/micro/v3 v3.10.36 h1:+XPhpguDC0HldSePp5TODdMJ06+IUy1AQkYN7ub84DA=
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
go.unistack.org/micro/v3 v3.10.36/go.mod h1:eUgtvbtiiz6te93m0ZdmoecbitWwjdBmmr84srmEIKA=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||||
|
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||||
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
|
github.com/matoous/go-nanoid v1.5.1 h1:aCjdvTyO9LLnTIi0fgdXhOPPvOHjpXN6Ik9DaNjIct4=
|
||||||
|
github.com/matoous/go-nanoid v1.5.1/go.mod h1:zyD2a71IubI24efhpvkJz+ZwfwagzgSO6UNiFsZKN7U=
|
||||||
|
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
|
||||||
|
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
|
||||||
|
github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
|
||||||
|
github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
|
||||||
|
go.unistack.org/micro-proto/v4 v4.1.0 h1:qPwL2n/oqh9RE3RTTDgt28XK3QzV597VugQPaw9lKUk=
|
||||||
|
go.unistack.org/micro-proto/v4 v4.1.0/go.mod h1:ArmK7o+uFvxSY3dbJhKBBX4Pm1rhWdLEFf3LxBrMtec=
|
||||||
|
go.unistack.org/micro/v4 v4.1.3 h1:9QHiLHBTfPtfKyoY3HsLPNVSK0wlAAoMV+9saLz2iR4=
|
||||||
|
go.unistack.org/micro/v4 v4.1.3/go.mod h1:lr3oYED8Ay1vjK68QqRw30QOtdk/ffpZqMFDasOUhKw=
|
||||||
|
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
||||||
|
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||||
|
google.golang.org/protobuf v1.36.3 h1:82DV7MYdb8anAVi3qge1wSnMDrnKK7ebr+I0hHRN1BU=
|
||||||
|
google.golang.org/protobuf v1.36.3/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
43
options.go
43
options.go
@@ -1,7 +1,12 @@
|
|||||||
package file
|
package file
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go.unistack.org/micro/v3/config"
|
"io"
|
||||||
|
"os"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
|
"go.unistack.org/micro/v4/config"
|
||||||
|
"golang.org/x/text/transform"
|
||||||
)
|
)
|
||||||
|
|
||||||
type pathKey struct{}
|
type pathKey struct{}
|
||||||
@@ -21,3 +26,39 @@ func SavePath(path string) config.SaveOption {
|
|||||||
func WatchPath(path string) config.WatchOption {
|
func WatchPath(path string) config.WatchOption {
|
||||||
return config.SetWatchOption(pathKey{}, path)
|
return config.SetWatchOption(pathKey{}, path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type readerKey struct{}
|
||||||
|
|
||||||
|
func Reader(r io.Reader) config.Option {
|
||||||
|
return config.SetOption(readerKey{}, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
type transformerKey struct{}
|
||||||
|
|
||||||
|
type TransformerFunc func(src []byte, index []int) []byte
|
||||||
|
|
||||||
|
func Transformer(t transform.Transformer) config.Option {
|
||||||
|
return config.SetOption(transformerKey{}, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEnvTransformer(rs string, trimLeft, trimRight int) (*EnvTransformer, error) {
|
||||||
|
re, err := regexp.Compile(rs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &EnvTransformer{
|
||||||
|
Regexp: re,
|
||||||
|
TransformerFunc: func(src []byte, index []int) []byte {
|
||||||
|
var envKey string
|
||||||
|
if len(src) > index[1]-trimRight {
|
||||||
|
envKey = string(src[index[0]+trimLeft : index[1]-trimRight])
|
||||||
|
}
|
||||||
|
|
||||||
|
if envVal, ok := os.LookupEnv(envKey); ok {
|
||||||
|
return []byte(envVal)
|
||||||
|
}
|
||||||
|
|
||||||
|
return src[index[0]:index[1]]
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
12
watcher.go
12
watcher.go
@@ -3,14 +3,12 @@ package file
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/codec"
|
"go.unistack.org/micro/v4/config"
|
||||||
"go.unistack.org/micro/v3/config"
|
"go.unistack.org/micro/v4/util/jitter"
|
||||||
"go.unistack.org/micro/v3/util/jitter"
|
rutil "go.unistack.org/micro/v4/util/reflect"
|
||||||
rutil "go.unistack.org/micro/v3/util/reflect"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type fileWatcher struct {
|
type fileWatcher struct {
|
||||||
@@ -39,12 +37,12 @@ func (w *fileWatcher) run() {
|
|||||||
dst, err := rutil.Zero(src)
|
dst, err := rutil.Zero(src)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
var fp *os.File
|
var fp *os.File
|
||||||
if fp, err = os.OpenFile(w.path, os.O_RDONLY, os.FileMode(0400)); err != nil {
|
if fp, err = os.OpenFile(w.path, os.O_RDONLY, os.FileMode(0o400)); err != nil {
|
||||||
w.echan <- fmt.Errorf("failed to open: %s, error: %w", w.path, err)
|
w.echan <- fmt.Errorf("failed to open: %s, error: %w", w.path, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err = ioutil.ReadAll(io.LimitReader(fp, int64(codec.DefaultMaxMsgSize)))
|
buf, err = io.ReadAll(io.LimitReader(fp, MaxFileSize))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = w.opts.Codec.Unmarshal(buf, dst)
|
err = w.opts.Codec.Unmarshal(buf, dst)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user