Skip to content

Commit

Permalink
Added: Reload and AutoReload functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Takacs Attila authored and takattila committed Feb 2, 2020
1 parent c2a0d2d commit 87a54f4
Show file tree
Hide file tree
Showing 10 changed files with 977 additions and 47 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@
# Dependency directories (remove the comment below to include it)
# vendor/

# Example test related files
example*
141 changes: 128 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

This package was made, to easily get needed settings from a file.

- Supported file types are: **'json'** and **'yaml'**.
- Supported file types are: **'json'**, **'yaml'** and **'ini'**.
- The configuration keys are **case insensitive**.

This package uses [github.com/spf13/viper](https://github.com/spf13/viper)
Expand All @@ -20,8 +20,10 @@ This package uses [github.com/spf13/viper](https://github.com/spf13/viper)
* [Initialize settings from a given content](#initialize-settings-from-a-given-content)
* [Get all keys from the settings](#get-all-keys-from-the-settings)
* [Get all settings](#get-all-settings)
* [Add prefix](#add-prefix)
* [Add a sub tree](#add-a-sub-tree)
* [Type assertions](#type-assertions)
* [Reload the settings data manually](#reload-the-settings-data-manually)
* [Automatic reload the settings data in the background](#automatic-reload-the-settings-data-in-the-background)

## Example usage

Expand Down Expand Up @@ -62,7 +64,7 @@ other:
string: 'text'
bool: true
`
sm := settings.New(NewFromSource)
sm := settings.NewFromContent(content)
```

[Back to top](#table-of-contents)
Expand All @@ -79,16 +81,15 @@ other:
string: 'text'
bool: true
`
sm := settings.New(NewFromSource)
sm := settings.NewFromContent(content)
keys, err := sm.GetAllKeys()
if err != nil {
log.Fatal(err)
}

log.Println("keys", keys)

// Output:
// 2020/01/29 19:09:52 keys [other content.int content.string content.bool]
log.Println("keys", keys)
```

[Back to top](#table-of-contents)
Expand All @@ -105,24 +106,23 @@ other:
string: 'text'
bool: true
`
sm := settings.New(NewFromSource)
sm := settings.NewFromContent(content)
allSettings, err := sm.GetAllSettings()
if err != nil {
log.Fatal(err)
}

log.Println("allSettings", allSettings)

// Output:
// 2020/01/29 19:11:58 allSettings map[content:map[bool:true int:1 string:text]]
log.Println("allSettings", allSettings)
```

[Back to top](#table-of-contents)

### Add prefix
### Add a sub tree

Returning a new settings instance representing a sub tree of this instance.
AddPrefix is case-insensitive for a key.
SubTree is case-insensitive for a key.

```go
var content = `
Expand All @@ -132,15 +132,17 @@ a:
value: 1
`

sm := settings.NewFromSource(content)
sm := settings.NewFromContent(content)

sm = sm.AddPrefix("a.b")
sm = sm.SubTree("a.b")

intValue, err := sm.GetInt("c.value")
if err != nil {
log.Fatal(err)
}

// Output:
// 2020/02/02 15:52:55 allSettings 1
log.Println("allSettings", intValue)
```

Expand All @@ -165,3 +167,116 @@ timeDurationKey, err := sm.GetDuration("time.duration.key")
```

[Back to top](#table-of-contents)

### Reload the settings data manually

Re-read the settings data by calling the Reload function.

```go
content := `
config:
config_key: config_value`

err := ioutil.WriteFile("./example/settings/example_config.yaml", []byte(content), os.ModePerm)
if err != nil {
log.Fatal(err)
}

sm := settings.New("./example/settings/example_config.yaml")

v, err := sm.Get("config.config_key")
if err != nil {
log.Fatal(err)
}

// Output:
// 2020/02/02 15:31:58 config_value
log.Println(v)

content = strings.ReplaceAll(content, "config_key: config_value", "foo: bar")
err = ioutil.WriteFile("./example/settings/example_config.yaml", []byte(content), os.ModePerm)
if err != nil {
log.Fatal(err)
}

// Reload the configuration ...
sm.Reload()

v, err = sm.Get("config.foo")
if err != nil {
log.Fatal(err)
}

// Output:
// 2020/02/02 15:31:58 bar
log.Println(v)
```

[Back to top](#table-of-contents)

### Automatic reload the settings data in the background

AutoReload is watching for settings file changes in the background and reloads configuration if needed.

```go
content := `
config:
config_key: config_value`

err := ioutil.WriteFile("./example/settings/example_config.yaml", []byte(content), os.ModePerm)
if err != nil {
log.Fatal(err)
}

sm := settings.New("./example/settings/example_config.yaml")

// Activate the automatic reload function ...
sm.AutoReload()

v, err := sm.Get("config.config_key")
if err != nil {
log.Fatal(err)
}

// Output:
// 2020/02/02 15:42:49 config_value
log.Println(v)

content = strings.ReplaceAll(content, "config_key: config_value", "foo: bar")
err = ioutil.WriteFile("./example/settings/example_config.yaml", []byte(content), os.ModePerm)
if err != nil {
log.Fatal(err)
}

time.Sleep(5 * time.Millisecond)

v, err = sm.Get("config.foo")
if err != nil {
log.Fatal(err)
}

// Output:
// 2020/02/02 15:42:49 settings.AutoReload settings reloaded
// 2020/02/02 15:42:49 bar
log.Println(v)

content = strings.ReplaceAll(content, "foo: bar", "config_key: config_value")
err = ioutil.WriteFile("./example/settings/example_config.yaml", []byte(content), os.ModePerm)
if err != nil {
log.Fatal(err)
}

time.Sleep(5 * time.Millisecond)

v, err = sm.Get("config.config_key")
if err != nil {
log.Fatal(err)
}

// Output:
// 2020/02/02 15:42:49 settings.AutoReload settings reloaded
// 2020/02/02 15:42:49 config_value
log.Println(v)
```

[Back to top](#table-of-contents)

0 comments on commit 87a54f4

Please sign in to comment.