{"id":24269813,"url":"https://github.com/pteich/configstruct","last_synced_at":"2025-09-24T08:31:41.249Z","repository":{"id":54485121,"uuid":"225390221","full_name":"pteich/configstruct","owner":"pteich","description":"Simple Go module to parse a configuration from environment and cli flags using struct tags. Also supports commands and sub-commands.","archived":false,"fork":false,"pushed_at":"2024-04-28T07:33:40.000Z","size":40,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-06-20T00:29:15.145Z","etag":null,"topics":["cli","cli-flags","commandline","config","configuration","environment","go","golang"],"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/pteich.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-12-02T14:11:46.000Z","updated_at":"2024-04-28T07:31:39.000Z","dependencies_parsed_at":"2024-06-20T00:22:20.970Z","dependency_job_id":null,"html_url":"https://github.com/pteich/configstruct","commit_stats":{"total_commits":25,"total_committers":4,"mean_commits":6.25,"dds":0.24,"last_synced_commit":"59b68acd995afaa28a438a6548d5963b1ff7691e"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pteich%2Fconfigstruct","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pteich%2Fconfigstruct/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pteich%2Fconfigstruct/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pteich%2Fconfigstruct/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pteich","download_url":"https://codeload.github.com/pteich/configstruct/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234058838,"owners_count":18772996,"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":["cli","cli-flags","commandline","config","configuration","environment","go","golang"],"created_at":"2025-01-15T15:08:19.553Z","updated_at":"2025-09-24T08:31:35.966Z","avatar_url":"https://github.com/pteich.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# configstruct\nSimple Go module to parse a configuration from environment values and CLI flags or arguments using struct tags.\nStarting with v1.3.0 there is also support for CLI commands and subcommands\n\nSince v1.5.0 it is possible to define arguments that are also parsed into struct values.\n\nStarting with v1.6.0 parsing a config file is supported (YAML only for now). Use `WithYamlConfig(path)` option to pass\na path to a YAML file that should be parsed. Values passed by flag or env will override values from the config file.\n\n## Usage without commands\n```Go\n// define a struct with tags for env name, cli flag and usage\ntype Config struct {\n\tFilename string `arg:\"1\" name:\"filename\" required:\"true\"`\n\tHostname string `env:\"CONFIGSTRUCT_HOSTNAME\" cli:\"hostname\" usage:\"hostname value\"`\n\tPort     int    `env:\"CONFIGSTRUCT_PORT\" cli:\"port\" usage:\"listen port\"`\n\tDebug    bool   `env:\"CONFIGSTRUCT_DEBUG\" cli:\"debug\" usage:\"debug mode\"`\n}\n\n// create a variable of the struct type and define defaults if needed\nconf := Config{\n    Hostname: \"localhost\",\n    Port:     8000,\n    Debug:    true,\n}\n\n// imagine the programm is called like this:\n// ./myprogram -hostname=myhost -port=9000 testfile\n// the flag values (hostname, port) and argument (filename) are parsed into the struct\n// all pre-set defaults are overwritten if a value is provided otherwise it is left as is\nerr := configstruct.Parse(\u0026conf)\nif err != nil {...}\n\n// if you prefer env with precedence over cli than use option\nerr := configstruct.Parse(\u0026conf, configstruct.WithPrecedenceEnv())\nif err != nil {...}\n\n// you can also use a special option for the default first cli, then env\nerr := configstruct.Parse(\u0026conf, configstruct.WithPrecedenceCli())\nif err != nil {...}\n\n// after parsing you can pass through you config struct and access values\nport := conf.Port\nhost := conf.Hostname\nfilename := conf.Filename\nif conf.Debug {...}\n\n// cli arguments are also possible\n\n```\n\n## Usage with commands\nYou can also define \"commands\" that can be used to execute callback functions. \nThe program with global flags and a command `count` should be called like this:\n````bash\nmycmd -hostname=localhost count -number=2\n\n```` \n\nThis is the code to model this behaviour:\n\n```Go\n// define a struct with tags for env name, cli flag and usage\ntype RootConfig struct {\n\tHostname string `env:\"CONFIGSTRUCT_HOSTNAME\" cli:\"hostname\" usage:\"hostname value\"`\n\tDebug    bool   `env:\"CONFIGSTRUCT_DEBUG\" cli:\"debug\" usage:\"debug mode\"`\n}\n\ntype CountConfig struct {\n    Number int `cli:\"number\" usage:\"number to count\"`\n} \n\n// create a variable of the struct type and define defaults if needed\nrootCfg := RootConfig{\n    Hostname: \"localhost\",\n    Debug:    true,\n}\n\ncountCfg := CountConfig {\n    Number: 1\n}\n\ncountCmd := NewCommand(\"count\", \u0026subConfig, func(c *configstruct.Command, cfg interface{}) error {\n    cfgValues := cfg.(*CountConfig)\n    ...\n    return nil\n})\n\ncmd := NewCommand(\"\", \u0026rootCfg, func(c *configstruct.Command, cfg interface{}) error {\n    cfgValues := cfg.(*RootConfig)\n    ...\n    return nil\n}, subCmd)\n\nerr := cmd.ParseAndRun(os.Args)\n```\n\n## Share dependencies across commands\nIt is possible to share dependencies with the command functions `c.SetDependency(name, dep)` and `dep, err := c.GetDependency(name)`.\nIf you for instance initialize a logger in the root command and register it as dependency every sub-command has\naccess to it. Keep in mind that dependencies are saved as `interface{}` so you have to take care of asserting the right type.\n\nTaking the example from above further it could look like this:\n```Go\ntype Logger struct {}\n\ncountCmd := NewCommand(\"count\", \u0026subConfig, func(c *configstruct.Command, cfg interface{}) error {\n    cfgValues := cfg.(*CountConfig)\n\tloggerDep, err := c.GetDependency(\"logger\")\n\tif err != nil {\n\t    return err\t\n\t}\n\t\n\tlogger := loggerDep.(*Logger)\n\t\n    ...\n    return nil\n})\n\ncmd := NewCommand(\"\", \u0026rootCfg, func(c *configstruct.Command, cfg interface{}) error {\n    cfgValues := cfg.(*RootConfig)\n    logger := \u0026Logger{}\n    c.SetDependency(\"logger\", logger)\n    ...\n    return nil\n}, subCmd)\n\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpteich%2Fconfigstruct","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpteich%2Fconfigstruct","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpteich%2Fconfigstruct/lists"}