Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

install & readme ERROR #1

Open
taropowder opened this issue Jul 20, 2020 · 2 comments
Open

install & readme ERROR #1

taropowder opened this issue Jul 20, 2020 · 2 comments

Comments

@taropowder
Copy link

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,

@xgfone
Copy link
Owner

xgfone commented Jul 21, 2020

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.

@xgfone
Copy link
Owner

xgfone commented Jul 21, 2020

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/[email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants