xgfone/gconf

install & readme ERROR

taropowder opened this issue · 2 comments

I use
go get github.com/xgfone/gconf/v5
Replaced the
go get -u github.com/xgfone/gconf/v5
then successful installation,

and in readme.md,
import "github.com/xgfone/gconf/v5"
maybe should
import "github.com/xgfone/gconf" ?

By the way , can this package parser nginx/apache/redis conf file?

I am very interested in your package ,but :{ ,i did't find document,

For the current newest version v5, that's, v5.X.Y, it only supports Go 1.11+. So you must import it by import "github.com/xgfone/gconf/v5", not import "github.com/xgfone/gconf" because it is declared as github.com/xgfone/gconf/v5 in the module file go.mod.

Why appending a /v5 after the package path? See Go Module V2+

Principle of Work:

  1. Add the option specifications into the Config.
  2. Add the decoders into the Config to parse the configuration data.
  3. Config Loads and parses the configurations from kinds of sources.
    1. Read the data as []byte from a certain source.
    2. Call the decoder to parse the data []byte as the key-value map.
    3. Load the key-value map, that's, traverse the each key-value pair, and set it as the key and the value of the option.
      1. According to the key, parse the group name and the option name, then lookup the option.
      2. Call the parser function of the found option to parse the value.
      3. Set the value of the option to the parsed value.
      4. Call the observers and notice the migrated options to update their values.

Notice:

  1. The Config has added four kinds of decoders, JSON, INI, YAML and TOML by default.
  2. The package has implemented six kinds of sources.
    • The stdlib flag to parse the Command Line Arguments.
    • The third-party library github.com/urfave/cli to parse the Command Line Arguments.
    • The configuration file, such as .json file, .ini file, .yaml file, .toml file, etc.
    • HTTP URL to read the configuration data.
    • Environments
    • ZooKeeper
  3. You can implements your decoders and sources.

So you can just do it like this:

import "github.com/xgfone/gconf/v5"

// 1. Define some options.
var opts = []gconf.Opt{
      gconf.StrOpt("opt1", "help doc").D("value1"),
      gconf.IntOpt("opt2", "help doc").D(123),
      // .......
}

// 2. Add the options into the global `Config`, but you can create and use a new one.
gconf.RegisterOpts(opts...)

// 3. Load the sources and set the options.
gconf.LoadSource(gconf.NewFlagSource()) // Read the Command Line Arguments and set the option values.
gconf.LoadSource(gconf.NewEnvSource())  // Read the environments and set the option values.

For your questions, you only need to implement the nginx/apache/redis decoder function, that's, func(src []byte, dst map[string]interface{}) error, then register it into Config before loading the sources.

NewFileSource is a general file source, which maybe read the file with the any format.

So you can do it as follow.

gconf.AddDecoder(NewNginxDecoder())
gconf.LoadSource(gconf.NewFileSource("/path/to/nginx.conf", "nginx"))

In order to decode the nginx conf file, you can use github.com/xgfone/ngconf to parse the nginx conf as a tree and convert it to a map depending on your requirements.

For apache or redis conf file, just do it like nginx.

Because of the limit of Go Module v2+, you must not import it by github.com/xgfone/gconf. Or it will use the version v1.X.Y, not the newest version v5. So you should use it by referring to the doc https://pkg.go.dev/github.com/xgfone/gconf@v1.5.0