1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-18 08:26:38 +02:00
go-micro/config/source/file
Mohamed MHAMDI ad8fca255b
fix(config): fix file source watcher stop behavior when Stop is called (#2630)
Co-authored-by: Mohamed MHAMDI <mmhamdi@hubside.com>
2023-04-26 02:19:52 +02:00
..
file_test.go feat(config): add withFs option to file source (#2557) 2022-09-24 02:46:18 +02:00
file.go fix: struct field alignment (#2632) 2023-04-26 02:16:34 +02:00
format_test.go fix: some linting issues (#2563) 2022-09-30 16:27:07 +02:00
format.go go-micro.dev/v4 (#2305) 2021-10-12 12:55:53 +01:00
options.go fix: some linting issues (#2563) 2022-09-30 16:27:07 +02:00
README.md fix: linting issues (#2566) 2022-09-30 20:32:55 +02:00
watcher_linux.go fix(config): fix file source watcher stop behavior when Stop is called (#2630) 2023-04-26 02:19:52 +02:00
watcher_test.go fix(config): fix file source watcher stop behavior when Stop is called (#2630) 2023-04-26 02:19:52 +02:00
watcher.go fix(config): fix file source watcher stop behavior when Stop is called (#2630) 2023-04-26 02:19:52 +02:00

File Source

The file source reads config from a file.

It uses the File extension to determine the Format e.g config.yaml has the yaml format. It does not make use of encoders or interpet the file data. If a file extension is not present the source Format will default to the Encoder in options.

Example

A config file format in json

{
  "hosts": {
    "database": {
      "address": "10.0.0.1",
      "port": 3306
    },
    "cache": {
      "address": "10.0.0.2",
      "port": 6379
    }
  }
}

New Source

Specify file source with path to file. Path is optional and will default to config.json

fileSource := file.NewSource(
	file.WithPath("/tmp/config.json"),
)

File Format

To load different file formats e.g yaml, toml, xml simply specify them with their extension

fileSource := file.NewSource(
        file.WithPath("/tmp/config.yaml"),
)

If you want to specify a file without extension, ensure you set the encoder to the same format

e := toml.NewEncoder()

fileSource := file.NewSource(
        file.WithPath("/tmp/config"),
        source.WithEncoder(e),
)

Load Source

Load the source into config

// Create new config
conf := config.NewConfig()

// Load file source
conf.Load(fileSource)