https://github.com/clever/configure
Seamlessly configure applications with either flags or JSON
https://github.com/clever/configure
Last synced: 9 months ago
JSON representation
Seamlessly configure applications with either flags or JSON
- Host: GitHub
- URL: https://github.com/clever/configure
- Owner: Clever
- Created: 2016-01-12T19:40:21.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2025-04-24T19:43:49.000Z (about 1 year ago)
- Last Synced: 2025-04-24T20:38:25.210Z (about 1 year ago)
- Language: Go
- Homepage:
- Size: 93.8 KB
- Stars: 0
- Watchers: 60
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# configure
Easily fill in a configuration struct with either flag arguments or a JSON blob argument.
## Usage
Given a simple program that uses two arguments, `district_id` and `collection`:
```go
var config struct {
DistrictID string `config:"district_id,required"`
Collection string `config:"collection"`
}
func main() {
if err := configure.Configure(&config); err != nil {
log.Fatalf("err: %#v", err)
}
log.Printf("config: %#v", config)
// go use arguments to do something
}
```
It can be invoked two styles:
```bash
./example -h
Usage of ./example:
-collection string
generated field
-district_id string
generated field
./example -district_id="abc123"
> config: {DistrictID:abc123 Collection:}
./example '{"district_id":"abc123","collection":"schools"}'
> config: {DistrictID:abc123 Collection:schools}
./example -district_id="abc123" -collection="schools"
> config: {DistrictID:abc123 Collection:schools}
# fails when not provided with the required district_id argument
./example -collection=schools
> err: Missing required fields: [district_id]
./example '{"collection":"schools"}'
> err: Missing required fields: [district_id]
```
### Defining defaults
You can also define defaults by passing a pre-populated struct:
```go
func main() {
config := struct {
DistrictID string `config:"district_id,required"`
Collection string `config:"collection"`
}{
Collection: "default-collection",
}
if err := configure.Configure(&config); err != nil {
log.Fatalf("err: %#v", err)
}
log.Printf("config: %#v\n", config)
// go use arguments to do something
}
```