{"id":22572745,"url":"https://github.com/dmfed/conf","last_synced_at":"2025-06-27T03:37:00.027Z","repository":{"id":57572451,"uuid":"349799828","full_name":"dmfed/conf","owner":"dmfed","description":"Basic config parser","archived":false,"fork":false,"pushed_at":"2021-11-04T22:05:38.000Z","size":26,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-28T14:44:57.352Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dmfed.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-03-20T18:02:55.000Z","updated_at":"2021-11-04T22:05:35.000Z","dependencies_parsed_at":"2022-08-24T08:40:52.428Z","dependency_job_id":null,"html_url":"https://github.com/dmfed/conf","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/dmfed/conf","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmfed%2Fconf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmfed%2Fconf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmfed%2Fconf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmfed%2Fconf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dmfed","download_url":"https://codeload.github.com/dmfed/conf/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmfed%2Fconf/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262182821,"owners_count":23271693,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-08T02:11:33.612Z","updated_at":"2025-06-27T03:36:59.997Z","avatar_url":"https://github.com/dmfed.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# module conf\n\n**go get github.com/dmfed/conf** to download.\n\n**import \"github.com/dmfed/conf\"** to use in your code.\n\nModule conf implements a very simple config parser with two types of\nvalues: key value pairs and single word options. Each of these must be\nput on separate line in a file like this:\n```\nkey1 = value\nkey2 = value1, value2, value3\noption1\noption2\n```\nValues can also be read from any io.Reader or io.ReadCloser. Note that\nvalues in key value pairs mey either be separated with spaces or not.\n\nTypical use case would look like this:\n```go\nconfig, err := conf.ParseFile(\"filename\")\nif err != nil {\n\t// Means we failed to read from file\n\t// config variable is now nil and unusable\n}\nvalue, err := config.Find(\"mykey\")\nif err != nil {\n\t// Means that key has not been found\n}\n// value now holds conf.Setting type which can be accessed directly.\nfmt.Println(value.Value)\nn, err := value.Float64() // tries to parse float from Setting.Value field.\n// if err is nil then n holds float64.\n```\nShorter syntax is also available with Get() method which drops errors if \nkey was not found and simply returns Setting with empty Value field:\n```go\nlistnumbers, err := config.Get(\"numbers\").IntSlice()\n// listnumbers holds slice of ints. If err is nil all of found values\n// have converted successfully.\n```\nNote that we'd still like to check for errors in above example even if\nwe're sure the key exists.  This way we'll configrm that all values have been converted.\n\nThere is also a GetDefault() method:\n```go\ndef := config.GetDefault(\"non-existant-key\", \"myvalue\")\nfmt.Println(def.Value) // \"myvalue\"\n```\nTo check whether single-word options were found use:\n```go\nif config.HasOption(\"wordoption\") {\n\t// do something\n}\n```\nSee description of module's types and methods which are\nquite self-explanatory.\n\nSee also **[https://pkg.go.dev/github.com/dmfed/conf](https://pkg.go.dev/github.com/dmfed/conf)** for a complete description of module's \nfunctions.\n\nBelow is listing of a working example of a program parsing config (also found **example/main.go** in the repository).\n```go\npackage main\n\nimport (\n\t\"bytes\"\n\t\"fmt\"\n\n\t\"github.com/dmfed/conf\"\n)\n\nvar testConf = []byte(`\n# commented\nport=10000\nservers = 10.0.0.1, 10.0.0.2, 10.0.0.3\nbool0=0\nbooltrue = true\ndistance=13.42\niamsure = hopefully you're not mistaken\nfloat = 13.1984\ncolor`)\n\nfunc main() {\n\tr := bytes.NewReader(testConf) // creating io.Reader from []byte()\n\tconfig := conf.ParseReader(r)  // we could call conf.ParseFile(\"filename\") here\n\n\t// First of all we can access parsed values directly:\n\tfor key, value := range config.Settings {\n\t\tfmt.Println(key, value)\n\t}\n\tfor opt := range config.Options {\n\t\tfmt.Println(opt)\n\t}\n\tfmt.Println()\n\n\t// Find( key string) returns instance of Setting and an\n\t// error if key was not found\n\t//\n\t// type Setting struct {\n\t// \t\tValue \tstring\t// value if found\n\t// }\n\t//\n\t// You can access Setting's Value field (type string) directly.\n\tport, err := config.Find(\"port\")\n\tfmt.Printf(\"variable port has type: %T, port.Value == %v, type of port.Value is: %T, error returned: %v\\n\", port, port.Value, port.Value, err)\n\n\t// We can cast Setting.Value to a desired type including int, float64,\n\t// bool and string. Method will return an error if Setting Value field\n\t// can not be interpreted as desired type.\n\tn, err := port.Int()\n\tfmt.Printf(\"n has value: %v, type: %T, err: %v\\n\", n, n, err)\n\n\t// Another syntax for getting Setting instance is to use Get() method\n\t// which never returns errors. Get will return Setting with empty string\n\t// in Value filed if requested key was now found.\n\td := config.Get(\"distance\")\n\tdistance, err := d.Float64()\n\tfmt.Printf(\"var distance has value: %v, type: %T, error value: %v\\n\", distance, distance, err)\n\n\t// Get() syntax can be is slightly shorter if we're \"sure\" that key exists in\n\t// the config.\n\tsure := config.Get(\"iamsure\").String() // String method never returns errors\n\t// or simply sure := config.Get(\"iamsure\").Value:\n\tfmt.Println(sure)\n\n\t// or like this:\n\tfl, _ := config.Get(\"float\").Float64() // we're dropping error here. Bad if value fails to convert.\n\tfmt.Println(\"fl, _ := config.Get(\\\"float\\\").Float64() Value of var fl is:\", fl)\n\n\t// We can access comma-separated values of key like this:\n\tservers := config.Get(\"servers\").StringSlice()\n\tfmt.Println(\"Found servers:\")\n\tfor i, s := range servers {\n\t\tfmt.Println(i+1, \"\\t\", s)\n\t}\n\t// There is also a GetDefault() method which sets output Setting value\n\t// to requested default if key was not found.\n\tdef := config.GetDefault(\"non-existant-key\", \"myvalue\")\n\tfmt.Println(def.Value) // \"myvalue\"\n\n\t// You can use HasOption method to find whether single-word options were\n\t// present in the the config\n\tif config.HasOption(\"color\") {\n\t\tfmt.Println(\"Hooray, we've found option \\\"color\\\"!\")\n\t\t// do something useful\n\t}\n\n\t// Below code finds two keys with bool values in the Config\n\t// and outputs those.\n\tvar t, f bool\n\tt, _ = config.Get(\"booltrue\").Bool()\n\tf, _ = config.Get(\"bool0\").Bool()\n\tfmt.Printf(\"t's type is: %T, value: %v, f's type is: %T, value: %v\\n\\n\", t, t, f, f)\n}\n```\nHere is the full output of the above code:\n```\ndistance 13.42\niamsure hopefully you're not mistaken\nfloat 13.1984\nport 10000\nservers 10.0.0.1, 10.0.0.2, 10.0.0.3\nbool0 0\nbooltrue true\ncolor\n\nvariable port has type: conf.Setting, port.Value == 10000, type of port.Value is: string, error returned: \u003cnil\u003e\nn has value: 10000, type: int, err: \u003cnil\u003e\nvar distance has value: 13.42, type: float64, error value: \u003cnil\u003e\nhopefully you're not mistaken\nfl, _ := config.Get(\"float\").Float64() Value of var fl is: 13.1984\nFound servers:\n1 \t 10.0.0.1\n2 \t 10.0.0.2\n3 \t 10.0.0.3\nHooray, we've found option \"color\"!\nt's type is: bool, value: true, f's type is: bool, value: false\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmfed%2Fconf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdmfed%2Fconf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmfed%2Fconf/lists"}