pax_global_header00006660000000000000000000000064125713573450014526gustar00rootroot0000000000000052 comment=61258af23f12cecf518f9583ef0a6fb2f4564135 go-config-0.1/000077500000000000000000000000001257135734500132365ustar00rootroot00000000000000go-config-0.1/.gitignore000066400000000000000000000000051257135734500152210ustar00rootroot00000000000000*swp go-config-0.1/LICENSE000066400000000000000000000020741257135734500142460ustar00rootroot00000000000000Copyright (c) Paul R. Tagliamonte , 2015 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. go-config-0.1/README.md000066400000000000000000000014341257135734500145170ustar00rootroot00000000000000pault.ag/go/config ================== This package allows you define structs which both define the configuration file format, and command line flags. ```go package main import ( "fmt" "os" "pault.ag/go/config" ) type Example struct { Option string `flag:"option" description:"This sets all sorts of things"` Value int `flag:"value" description:"This is an integer value!"` } func main() { conf := Example{ Option: "default", } flags, err := config.LoadFlags("example", &conf) // This will load the RFC822 formatted config from ~/.examplerc and // return a flag.FlagSet. The Flags will be populated by defaults from // the struct given. if err != nil { panic(err) } flags.Parse(os.Args[1:]) fmt.Printf("option %s, value %d\n", conf.Option, conf.Value) } ``` go-config-0.1/config.go000066400000000000000000000057351257135734500150440ustar00rootroot00000000000000/* {{{ Copyright (c) Paul R. Tagliamonte , 2015 * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. }}} */ package config import ( "flag" "fmt" "os" "os/user" "path" "reflect" "strings" "unsafe" "pault.ag/go/debian/control" ) func LoadFlags(name string, data interface{}) (*flag.FlagSet, error) { if err := Load(name, data); err != nil { return nil, err } return Flag(data) } // func Load(name string, data interface{}) error { localUser, err := user.Current() if err != nil { return nil } rcPath := path.Join(localUser.HomeDir, fmt.Sprintf(".%src", name)) fd, err := os.Open(rcPath) if err != nil { return nil } defer fd.Close() err = control.Unmarshal(data, fd) return err } func flagPointer(incoming reflect.Value, data *flag.FlagSet) error { if incoming.Type().Kind() == reflect.Ptr { return flagPointer(incoming.Elem(), data) } for i := 0; i < incoming.NumField(); i++ { field := incoming.Field(i) fieldType := incoming.Type().Field(i) if it := fieldType.Tag.Get("flag"); it != "" { /* Register the flag */ switch field.Type().Kind() { case reflect.Int: data.IntVar( (*int)(unsafe.Pointer(field.Addr().Pointer())), it, int(field.Int()), fieldType.Tag.Get("description"), ) continue case reflect.String: data.StringVar( (*string)(unsafe.Pointer(field.Addr().Pointer())), it, field.String(), fieldType.Tag.Get("description"), ) continue default: return fmt.Errorf("Unknown type: %s", field.Type().Kind()) } } } return nil } func Flag(data interface{}) (*flag.FlagSet, error) { dataValue := reflect.ValueOf(data) name := strings.ToLower(dataValue.Elem().Type().Name()) flagSet := flag.NewFlagSet(name, flag.ExitOnError) flagSet.Usage = func() { fmt.Fprintf(os.Stderr, "Usage of %s:\n", name) flagSet.PrintDefaults() } err := flagPointer(dataValue, flagSet) if err != nil { return nil, err } return flagSet, nil } // vim: foldmethod=marker