Skip to content

Commit

Permalink
chore: add some new docs for watch config files
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 9, 2022
1 parent 5aa4a79 commit 1d9e607
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 28 deletions.
67 changes: 42 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,19 +273,19 @@ myConf := config.NewWithOptions("my-conf", config.ParseEnv, config.ReadOnly)

Now, you can add a hook func for listen config data change. then, you can do something like: write data to file

Add hook func on create config:
**Add hook func on create config**:

```go
hookFn := func(event string, c *Config) {
fmt.Println("fire the:", event)
}
hookFn := func(event string, c *Config) {
fmt.Println("fire the:", event)
}

c := NewWithOptions("test", WithHookFunc(hookFn))
// for global config
config.WithOptions(WithHookFunc(hookFn))
c := NewWithOptions("test", config.WithHookFunc(hookFn))
// for global config
config.WithOptions(config.WithHookFunc(hookFn))
```

After that, when calling `LoadXXX, Set, SetData, ClearData` etc methods, it will output:
After that, when calling `LoadXXX, Set, SetData, ClearData` methods, it will output:

```text
fire the: load.data
Expand All @@ -294,6 +294,23 @@ fire the: set.data
fire the: clean.data
```

### Watch loaded config files

To listen for changes to loaded config files, and reload the config when it changes, you need to use the https://github.com/fsnotify/fsnotify library.
For usage, please refer to the example [./_example/watch_file.go](_examples/watch_file.go)

Also, you need to listen to the `reload.data` event:

```go
config.WithOptions(config.WithHookFunc(func(event string, c *config.Config) {
if event == config.OnReloadData {
fmt.Println("config reloaded, you can do something ....")
}
}))
```

When the configuration changes, you can do related things, for example: rebind the configuration to your struct.

## Dump config data

> Can use `config.DumpTo()` export the configuration data to the specified `writer`, such as: buffer,file
Expand Down Expand Up @@ -362,24 +379,24 @@ type Options struct {
Support parse default value by struct tag `default`

```go
// add option: config.ParseDefault
c := config.New("test").WithOptions(config.ParseDefault)

// only set name
c.SetData(map[string]interface{}{
"name": "inhere",
})

// age load from default tag
type User struct {
Age int `default:"30"`
Name string
Tags []int
}
// add option: config.ParseDefault
c := config.New("test").WithOptions(config.ParseDefault)

// only set name
c.SetData(map[string]interface{}{
"name": "inhere",
})

// age load from default tag
type User struct {
Age int `default:"30"`
Name string
Tags []int
}

user := &User{}
goutil.MustOk(c.Decode(user))
dump.Println(user)
user := &User{}
goutil.MustOk(c.Decode(user))
dump.Println(user)
```

**Output**:
Expand Down
23 changes: 20 additions & 3 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,9 @@ myConf := config.NewWithOptions("my-conf", config.ParseEnv, config.ReadOnly)

## 监听配置更改

现在,您可以添加一个挂钩函数来监听配置数据更改。然后,您可以执行一些自定义操作, 例如:将数据写入文件
现在,您可以添加一个钩子函数来监听配置数据更改。然后,您可以执行一些自定义操作, 例如:将数据写入文件

在创建配置时添加钩子函数:
**在创建配置时添加钩子函数**:

```go
hookFn := func(event string, c *Config) {
Expand All @@ -269,7 +269,7 @@ c := NewWithOptions("test", WithHookFunc(hookFn))
config.WithOptions(WithHookFunc(hookFn))
```

之后, 当调用 `LoadXXX, Set, SetData, ClearData` 等方法时, 就会输出:
**之后**, 当调用 `LoadXXX, Set, SetData, ClearData` 等方法时, 就会输出:

```text
fire the: load.data
Expand All @@ -278,6 +278,23 @@ fire the: set.data
fire the: clean.data
```

### 监听载入的配置文件变动

想要监听载入的配置文件变动,并在变动时重新加载配置,你需要使用 https://github.com/fsnotify/fsnotify 库。
使用方法可以参考示例 [./_example/watch_file.go](_examples/watch_file.go)

同时,你需要监听 `reload.data` 事件:

```go
config.WithOptions(config.WithHookFunc(func(event string, c *config.Config) {
if event == config.OnReloadData {
fmt.Println("config reloaded, you can do something ....")
}
}))
```

当配置发生变化并重新加载后,你可以做相关的事情,例如:重新绑定配置到你的结构体。

## 导出配置到文件

> 可以使用 `config.DumpTo(out io.Writer, format string)` 将整个配置数据导出到指定的writer, 比如 buffer,file。
Expand Down
1 change: 1 addition & 0 deletions testdata/json_base.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "app",
"debug": false,
"baseKey": "value",
"key_in_json": "hello jsond",
"age": 123,
"envKey": "${SHELL}",
"envKey1": "${NotExist|defValue}",
Expand Down

0 comments on commit 1d9e607

Please sign in to comment.