Compare commits
78 Commits
Author | SHA1 | Date | |
---|---|---|---|
d52e22c187 | |||
3cc1cf899a | |||
89b41364c3 | |||
d7ffcd19f8 | |||
6b27204711 | |||
4bee1a7041 | |||
f8f36b157f | |||
7018485fde | |||
dcae9f14ee | |||
510dd16e48 | |||
|
257964fb26 | ||
11fe3f3ef7 | |||
|
ba9aa37eae | ||
d54d71403c | |||
|
d9c127339d | ||
2290d7573b | |||
|
7f449da60b | ||
c01614781a | |||
|
95606b8f21 | ||
c70f6386d0 | |||
|
28c896b8fb | ||
|
0c0e1307bd | ||
|
1f82ecea93 | ||
76101e6ce1 | |||
|
b830dc9749 | ||
8988703465 | |||
|
0ec641747a | ||
fa03d3b557 | |||
|
2cebca91a6 | ||
5204de7cbb | |||
|
dcebdb62f2 | ||
5877222e8a | |||
|
811c83a189 | ||
0b610c430e | |||
|
e441398ab4 | ||
|
df29357782 | ||
dc0b42f998 | |||
|
c9657f0b20 | ||
150d1e57f6 | |||
|
2c31e3b350 | ||
|
7794d7a281 | ||
|
083ef551a0 | ||
|
0ecb1c1659 | ||
|
e49cae189a | ||
909ad599bb | |||
|
784b309405 | ||
9f71fdea5f | |||
|
dc84a2eb2a | ||
|
ad3e3e3247 | ||
|
006ed2929c | ||
3920f11b69 | |||
|
a84556fc6f | ||
|
6a936fa610 | ||
|
a358f95f2e | ||
23e0b7671b | |||
|
a60d877ed1 | ||
3728d5ed05 | |||
|
2ee7357913 | ||
4fbfd6da4d | |||
|
747def6210 | ||
f5fe1f7318 | |||
|
792d9734c2 | ||
697673296b | |||
|
58860cbb4c | ||
76722cd0de | |||
764e827a9e | |||
|
0f185974fe | ||
|
659f0a2446 | ||
|
345f9cc563 | ||
|
d1b85de9ec | ||
|
4fc4a78a77 | ||
|
f9828bcbf2 | ||
|
5810834afd | ||
|
ca1d9e8da5 | ||
|
bded061837 | ||
|
93c1ac7971 | ||
|
3cce97dc16 | ||
|
b09c5e52e3 |
20
.github/workflows/autoapprove.yml
vendored
Normal file
20
.github/workflows/autoapprove.yml
vendored
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
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
Normal file
21
.github/workflows/automerge.yml
vendored
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
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}}
|
13
.github/workflows/build.yml
vendored
13
.github/workflows/build.yml
vendored
@ -3,19 +3,20 @@ on:
|
|||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
|
- v3
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
name: test
|
name: test
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: setup
|
- name: setup
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: 1.16
|
go-version: 1.17
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: cache
|
- name: cache
|
||||||
uses: actions/cache@v2
|
uses: actions/cache@v3
|
||||||
with:
|
with:
|
||||||
path: ~/go/pkg/mod
|
path: ~/go/pkg/mod
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
@ -31,9 +32,9 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: lint
|
- name: lint
|
||||||
uses: golangci/golangci-lint-action@v2
|
uses: golangci/golangci-lint-action@v3.4.0
|
||||||
continue-on-error: true
|
continue-on-error: true
|
||||||
with:
|
with:
|
||||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||||
|
12
.github/workflows/codeql-analysis.yml
vendored
12
.github/workflows/codeql-analysis.yml
vendored
@ -43,14 +43,14 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: setup
|
- name: setup
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: 1.16
|
go-version: 1.17
|
||||||
# Initializes the CodeQL tools for scanning.
|
# Initializes the CodeQL tools for scanning.
|
||||||
- name: init
|
- name: init
|
||||||
uses: github/codeql-action/init@v1
|
uses: github/codeql-action/init@v2
|
||||||
with:
|
with:
|
||||||
languages: ${{ matrix.language }}
|
languages: ${{ matrix.language }}
|
||||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||||
@ -61,7 +61,7 @@ jobs:
|
|||||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
# 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)
|
# If this step fails, then you should remove it and run the build manually (see below)
|
||||||
- name: autobuild
|
- name: autobuild
|
||||||
uses: github/codeql-action/autobuild@v1
|
uses: github/codeql-action/autobuild@v2
|
||||||
|
|
||||||
# ℹ️ Command-line programs to run using the OS shell.
|
# ℹ️ Command-line programs to run using the OS shell.
|
||||||
# 📚 https://git.io/JvXDl
|
# 📚 https://git.io/JvXDl
|
||||||
@ -75,4 +75,4 @@ jobs:
|
|||||||
# make release
|
# make release
|
||||||
|
|
||||||
- name: analyze
|
- name: analyze
|
||||||
uses: github/codeql-action/analyze@v1
|
uses: github/codeql-action/analyze@v2
|
||||||
|
14
.github/workflows/dependabot-automerge.yml
vendored
14
.github/workflows/dependabot-automerge.yml
vendored
@ -1,4 +1,4 @@
|
|||||||
name: "prautomerge"
|
name: "dependabot-automerge"
|
||||||
|
|
||||||
on:
|
on:
|
||||||
pull_request_target:
|
pull_request_target:
|
||||||
@ -9,21 +9,17 @@ permissions:
|
|||||||
contents: write
|
contents: write
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
dependabot:
|
automerge:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
if: ${{ github.actor == 'dependabot[bot]' }}
|
if: github.actor == 'dependabot[bot]'
|
||||||
steps:
|
steps:
|
||||||
- name: metadata
|
- name: metadata
|
||||||
id: metadata
|
id: metadata
|
||||||
uses: dependabot/fetch-metadata@v1.1.1
|
uses: dependabot/fetch-metadata@v1.3.6
|
||||||
with:
|
with:
|
||||||
github-token: "${{ secrets.TOKEN }}"
|
github-token: "${{ secrets.TOKEN }}"
|
||||||
- name: approve
|
|
||||||
run: gh pr review --approve "$PR_URL"
|
|
||||||
env:
|
|
||||||
PR_URL: ${{github.event.pull_request.html_url}}
|
|
||||||
GITHUB_TOKEN: ${{secrets.TOKEN}}
|
|
||||||
- name: merge
|
- name: merge
|
||||||
|
id: merge
|
||||||
if: ${{contains(steps.metadata.outputs.dependency-names, 'go.unistack.org')}}
|
if: ${{contains(steps.metadata.outputs.dependency-names, 'go.unistack.org')}}
|
||||||
run: gh pr merge --auto --merge "$PR_URL"
|
run: gh pr merge --auto --merge "$PR_URL"
|
||||||
env:
|
env:
|
||||||
|
13
.github/workflows/pr.yml
vendored
13
.github/workflows/pr.yml
vendored
@ -3,19 +3,20 @@ on:
|
|||||||
pull_request:
|
pull_request:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
|
- v3
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
name: test
|
name: test
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: setup
|
- name: setup
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: 1.16
|
go-version: 1.17
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: cache
|
- name: cache
|
||||||
uses: actions/cache@v2
|
uses: actions/cache@v3
|
||||||
with:
|
with:
|
||||||
path: ~/go/pkg/mod
|
path: ~/go/pkg/mod
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
@ -31,9 +32,9 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: lint
|
- name: lint
|
||||||
uses: golangci/golangci-lint-action@v2
|
uses: golangci/golangci-lint-action@v3.4.0
|
||||||
continue-on-error: true
|
continue-on-error: true
|
||||||
with:
|
with:
|
||||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||||
|
250
file.go
250
file.go
@ -4,20 +4,25 @@ 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/v3/config"
|
"go.unistack.org/micro/v3/config"
|
||||||
rutil "go.unistack.org/micro/v3/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 {
|
||||||
@ -25,6 +30,10 @@ func (c *fileConfig) Options() config.Options {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *fileConfig) Init(opts ...config.Option) error {
|
func (c *fileConfig) Init(opts ...config.Option) error {
|
||||||
|
if err := config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&c.opts)
|
o(&c.opts)
|
||||||
}
|
}
|
||||||
@ -33,53 +42,106 @@ 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")
|
||||||
c.opts.Logger.Error(c.opts.Context, err)
|
}
|
||||||
if !c.opts.AllowFail {
|
|
||||||
return err
|
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
||||||
}
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
|
func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
|
||||||
if err := config.DefaultBeforeLoad(ctx, c); err != nil {
|
if c.opts.SkipLoad != nil && c.opts.SkipLoad(ctx, c) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := config.DefaultBeforeLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
c.opts.Logger.Errorf(c.opts.Context, "file load path %s error: %v", path, err)
|
if !c.opts.AllowFail {
|
||||||
|
if c.path != "" {
|
||||||
|
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 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if fpc, ok := fp.(io.Closer); ok {
|
||||||
|
defer fpc.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
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 !c.opts.AllowFail {
|
if !c.opts.AllowFail {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return config.DefaultAfterLoad(ctx, c)
|
if err = config.DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
||||||
}
|
|
||||||
|
|
||||||
defer fp.Close()
|
|
||||||
|
|
||||||
buf, err := ioutil.ReadAll(io.LimitReader(fp, int64(codec.DefaultMaxMsgSize)))
|
|
||||||
if err != nil {
|
|
||||||
c.opts.Logger.Errorf(c.opts.Context, "file load path %s error: %v", path, err)
|
|
||||||
if !c.opts.AllowFail {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return config.DefaultAfterLoad(ctx, c)
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
src, err := rutil.Zero(c.opts.Struct)
|
dst := c.opts.Struct
|
||||||
|
if options.Struct != nil {
|
||||||
|
dst = options.Struct
|
||||||
|
}
|
||||||
|
|
||||||
|
src, err := rutil.Zero(dst)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = c.opts.Codec.Unmarshal(buf, src)
|
err = c.opts.Codec.Unmarshal(buf, src)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@ -91,18 +153,15 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error
|
|||||||
if options.Append {
|
if options.Append {
|
||||||
mopts = append(mopts, mergo.WithAppendSlice)
|
mopts = append(mopts, mergo.WithAppendSlice)
|
||||||
}
|
}
|
||||||
err = mergo.Merge(c.opts.Struct, src, mopts...)
|
err = mergo.Merge(dst, src, mopts...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil && !c.opts.AllowFail {
|
||||||
c.opts.Logger.Errorf(c.opts.Context, "file load path %s error: %v", path, err)
|
return err
|
||||||
if !c.opts.AllowFail {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := config.DefaultAfterLoad(ctx, c); err != nil {
|
if err := config.DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,7 +169,11 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *fileConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
|
func (c *fileConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
|
||||||
if err := config.DefaultBeforeSave(ctx, c); err != nil {
|
if c.opts.SkipSave != nil && c.opts.SkipSave(ctx, c) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := config.DefaultBeforeSave(ctx, c); err != nil && !c.opts.AllowFail {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,22 +185,33 @@ func (c *fileConfig) Save(ctx context.Context, opts ...config.SaveOption) error
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
buf, err := c.opts.Codec.Marshal(c.opts.Struct)
|
dst := c.opts.Struct
|
||||||
if err != nil {
|
if options.Struct != nil {
|
||||||
c.opts.Logger.Errorf(c.opts.Context, "file save path %s error: %v", path, err)
|
dst = options.Struct
|
||||||
if !c.opts.AllowFail {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return config.DefaultAfterSave(ctx, c)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fp, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, os.FileMode(0600))
|
buf, err := c.opts.Codec.Marshal(dst)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.opts.Logger.Errorf(c.opts.Context, "file save path %s error: %v", path, err)
|
|
||||||
if !c.opts.AllowFail {
|
if !c.opts.AllowFail {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return config.DefaultAfterSave(ctx, c)
|
if err = config.DefaultAfterSave(ctx, c); err != nil && !c.opts.AllowFail {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
fp, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, os.FileMode(0o600))
|
||||||
|
if err != nil {
|
||||||
|
if !c.opts.AllowFail {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = config.DefaultAfterSave(ctx, c); err != nil && !c.opts.AllowFail {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
defer fp.Close()
|
defer fp.Close()
|
||||||
|
|
||||||
@ -145,14 +219,11 @@ func (c *fileConfig) Save(ctx context.Context, opts ...config.SaveOption) error
|
|||||||
err = fp.Close()
|
err = fp.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil && !c.opts.AllowFail {
|
||||||
c.opts.Logger.Errorf(c.opts.Context, "file save path %s error: %v", path, err)
|
return err
|
||||||
if !c.opts.AllowFail {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := config.DefaultAfterSave(ctx, c); err != nil {
|
if err := config.DefaultAfterSave(ctx, c); err != nil && !c.opts.AllowFail {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,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/v3/codec"
|
||||||
|
"go.unistack.org/micro/v3/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)
|
||||||
|
}
|
||||||
|
}
|
16
go.mod
16
go.mod
@ -1,8 +1,18 @@
|
|||||||
module go.unistack.org/micro-config-file/v3
|
module go.unistack.org/micro-config-file/v3
|
||||||
|
|
||||||
go 1.16
|
go 1.22
|
||||||
|
|
||||||
|
toolchain go1.23.1
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/imdario/mergo v0.3.12
|
dario.cat/mergo v1.0.1
|
||||||
go.unistack.org/micro/v3 v3.8.9
|
go.unistack.org/micro/v3 v3.10.100
|
||||||
|
golang.org/x/text v0.20.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
|
go.unistack.org/micro-proto/v3 v3.4.1 // indirect
|
||||||
|
google.golang.org/protobuf v1.35.2 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
45
go.sum
45
go.sum
@ -1,28 +1,25 @@
|
|||||||
github.com/ef-ds/deque v1.0.4/go.mod h1:gXDnTC3yqvBcHbq2lcExjtAcVrOnJCbMcZXmuj8Z4tg=
|
dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
|
||||||
github.com/golang-jwt/jwt/v4 v4.1.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
|
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
|
||||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
|
||||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q=
|
||||||
github.com/silas/dag v0.0.0-20210626123444-3804bac2d6d4/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo=
|
||||||
go.unistack.org/micro-proto/v3 v3.1.0 h1:q39FwjFiRZn+Ux/tt+d3bJTmDtsQQWa+3SLYVo1vLfA=
|
go.unistack.org/micro/v3 v3.10.97 h1:8l7fv+i06/PjPrBBhRC/ZQkWGIOuHPg3jJN0vktYE78=
|
||||||
go.unistack.org/micro-proto/v3 v3.1.0/go.mod h1:DpRhYCBXlmSJ/AAXTmntvlh7kQkYU6eFvlmYAx4BQS8=
|
go.unistack.org/micro/v3 v3.10.97/go.mod h1:YzMldzHN9Ei+zy5t/Psu7RUWDZwUfrNYiStSQtTz90g=
|
||||||
go.unistack.org/micro/v3 v3.8.9 h1:F+HAQSHI86F8Xx5D6XKvvuN0TtOuVvJM+OBV8aYTg0Y=
|
go.unistack.org/micro/v3 v3.10.100 h1:yWOaU0ImCGm5k5MUzlIobJUOr+KLfrR/BoDZvcHyKxM=
|
||||||
go.unistack.org/micro/v3 v3.8.9/go.mod h1:KMMmOmbgo/D52/rCAbqeKbBsgEEbSKM69he54J3ZIuA=
|
go.unistack.org/micro/v3 v3.10.100/go.mod h1:YzMldzHN9Ei+zy5t/Psu7RUWDZwUfrNYiStSQtTz90g=
|
||||||
golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
|
||||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
|
||||||
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
|
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||||
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
|
||||||
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.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
41
options.go
41
options.go
@ -1,7 +1,12 @@
|
|||||||
package file
|
package file
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/config"
|
"go.unistack.org/micro/v3/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
|
||||||
|
}
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/codec"
|
|
||||||
"go.unistack.org/micro/v3/config"
|
"go.unistack.org/micro/v3/config"
|
||||||
"go.unistack.org/micro/v3/util/jitter"
|
"go.unistack.org/micro/v3/util/jitter"
|
||||||
rutil "go.unistack.org/micro/v3/util/reflect"
|
rutil "go.unistack.org/micro/v3/util/reflect"
|
||||||
@ -44,7 +43,7 @@ func (w *fileWatcher) run() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err = ioutil.ReadAll(io.LimitReader(fp, int64(codec.DefaultMaxMsgSize)))
|
buf, err = ioutil.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)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user