├── LICENSE ├── README.mkd └── etcdenv.go /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Yasuhiro Matsumoto, http://mattn.kaoriya.net 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.mkd: -------------------------------------------------------------------------------- 1 | # etcdenv 2 | 3 | etcd + env = awesome! 4 | 5 | env command integrated with [etcd](http://coreos.com/docs/etcd/) server 6 | 7 | ## Usage 8 | 9 | $ curl http://127.0.0.1:4001/v1/keys/app/db -d value="newdb" 10 | $ curl http://127.0.0.1:4001/v1/keys/app/cache -d value="new cache" 11 | $ curl http://127.0.0.1:4001/v1/keys/app2/db -d value="otherdb" 12 | 13 | $ curl http://localhost:4001/v1/keys/app 14 | [{"action":"GET","key":"/app/db","value":"newdb","index":4},{"action":"GET","key":"/app/cache","value":"new cache","index":4}] 15 | 16 | $ etcdenv -key=/app/ 17 | DB=newdb 18 | CACHE=new cache 19 | 20 | $ etcdenv -key=/app/ ruby web.rb 21 | 22 | $ etcdenv -key=/ -r 23 | DB=newdb,otherdb 24 | CACHE=new cache 25 | 26 | ### Shebang 27 | 28 | $ cat myapp.sh 29 | #!/path/to/etcdenv -key=/myapp ./myapp.sh 30 | 31 | If you are using OSs which the shell doesn't parse arguments separated with spaces, try to use `-s` option. 32 | 33 | $ cat myapp.sh 34 | #!/path/to/etcdenv -s -key=/myapp ./myapp.sh 35 | 36 | ### Foreman/Goreman 37 | 38 | $ cat Procfile 39 | web: etcdenv -key=/myapp/ ruby web.rb 40 | 41 | ## License 42 | 43 | MIT: http://mattn.mit-license.org/2013 44 | -------------------------------------------------------------------------------- /etcdenv.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "os" 7 | "os/exec" 8 | "strings" 9 | "syscall" 10 | 11 | "github.com/coreos/go-etcd/etcd" 12 | ) 13 | 14 | var sep = flag.Bool("s", false, "separate arguments with spaces") 15 | var rec = flag.Bool("r", false, "recursively fetch child values") 16 | var key = flag.String("key", "", "etcd key") 17 | var host = flag.String("host", "", "etcd host") 18 | var hosts []string 19 | var envs []string 20 | 21 | func prefixInSlice(list []string, s string) int { 22 | for i, entry := range list { 23 | if strings.HasPrefix(entry, s) { 24 | return i 25 | } 26 | } 27 | return -1 28 | } 29 | 30 | func handleNode(n *etcd.Node) { 31 | if !n.Dir { 32 | key := strings.Split(n.Key, "/") 33 | k, v := strings.ToUpper(key[len(key)-1]), n.Value 34 | // if k already exists and in recursive mode, append v with comma 35 | if i := prefixInSlice(envs, k+"="); i != -1 && *rec { 36 | envs[i] = fmt.Sprint(envs[i], ",", v) 37 | } else { 38 | envs = append(envs, k+"="+v) 39 | } 40 | } else if *rec { 41 | for _, n2 := range n.Nodes { 42 | handleNode(n2) 43 | } 44 | } 45 | } 46 | 47 | func main() { 48 | if len(os.Args) > 1 && strings.HasPrefix(os.Args[1], "-s ") { 49 | args := []string{} 50 | for _, arg := range os.Args { 51 | args = append(args, strings.Split(arg, " ")...) 52 | } 53 | os.Args = args 54 | } 55 | flag.Parse() 56 | 57 | if *key == "" { 58 | *key = os.Getenv("ETCDENV_KEY") 59 | } 60 | if *host == "" { 61 | *host = os.Getenv("ETCDENV_HOST") 62 | } 63 | if *host != "" { 64 | hosts = strings.Split(*host, ",") 65 | } 66 | client := etcd.NewClient(hosts) 67 | res, err := client.Get(*key, true, true) 68 | if err != nil { 69 | fmt.Fprintf(os.Stderr, "etcdenv: %s\n", err) 70 | os.Exit(1) 71 | } 72 | 73 | envs = os.Environ() 74 | for _, n := range res.Node.Nodes { 75 | handleNode(n) 76 | } 77 | 78 | if flag.NArg() == 0 { 79 | for _, env := range envs { 80 | line := fmt.Sprintf("%q", env) 81 | fmt.Println(line[1 : len(line)-1]) 82 | } 83 | os.Exit(0) 84 | } 85 | 86 | cmd := exec.Command(flag.Args()[0], flag.Args()[1:]...) 87 | cmd.Env = envs 88 | cmd.Stderr = os.Stderr 89 | cmd.Stdout = os.Stdout 90 | cmd.Stdin = os.Stdin 91 | err = cmd.Run() 92 | if cmd.Process == nil { 93 | fmt.Fprintf(os.Stderr, "etcdenv: %s\n", err) 94 | os.Exit(1) 95 | } 96 | os.Exit(cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()) 97 | } 98 | --------------------------------------------------------------------------------