Last active
August 29, 2015 14:06
-
-
Save rafrombrc/2696621c9bce7d735e65 to your computer and use it in GitHub Desktop.
env var substitution prototype
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[hekad] | |
maxprocs = 4 | |
base_dir = "/home/rob/go/heka/var/cache/hekad" | |
#max_pack_idle = 2 | |
#cpuprof = "cpu.prof" | |
memprof = "mem.prof" | |
[TcpInput] | |
address = "127.0.0.1:5565" | |
parser_type = "message.proto" | |
decoder = "ProtobufDecoder" | |
something = "%ENV[GOPATH]" | |
[CounterFilter] | |
message_matcher = "Type != 'heka.counter-output'" | |
ticker_interval = 1 | |
bogus = "%this is a test%" | |
double_bogus = "%ENjustkidding%ENV" | |
triple_bogus = "%EN%ENV[GOPATH]" | |
[PayloadEncoder] | |
prefix_ts = false | |
path = "%ENV[PATH]" | |
[LogOutput] | |
open_only = "%ENV[no_ending" | |
message_matcher = "Type == 'heka.counter-output'" | |
encoder = "PayloadEncoder" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"bytes" | |
"io" | |
"io/ioutil" | |
"log" | |
"os" | |
) | |
func main() { | |
f, err := os.Open("infile.toml") | |
bufIn := bufio.NewReader(f) | |
bufOut := new(bytes.Buffer) | |
for { | |
chunk, err := bufIn.ReadBytes(byte('%')) | |
if err != nil { | |
if err == io.EOF { | |
// We're done. | |
bufOut.Write(chunk) | |
break | |
} | |
log.Fatalln("ReadBytes error: ", err) | |
} | |
bufOut.Write(chunk[:len(chunk)-1]) | |
tmp := make([]byte, 4) | |
tmp, err = bufIn.Peek(4) | |
if err != nil { | |
if err == io.EOF { | |
// End of file, write the last few bytes out and exit. | |
bufOut.WriteRune('%') | |
bufOut.Write(tmp) | |
break | |
} | |
log.Fatalln("Peek error: ", err) | |
} | |
if string(tmp) == "ENV[" { | |
// Found opening delimiter, advance the read cursor and look for | |
// closing delimiter. | |
tmp, err = bufIn.ReadBytes(byte('[')) | |
if err != nil { | |
// This shouldn't happen, since the Peek succeeded. | |
log.Fatalln("Read error: ", err) | |
} | |
chunk, err = bufIn.ReadBytes(byte(']')) | |
if err != nil { | |
if err == io.EOF { | |
// No closing delimiter, write everything we've collected | |
// and be done. | |
bufOut.WriteRune('%') | |
bufOut.Write(tmp) | |
bufOut.Write(chunk) | |
break | |
} | |
log.Fatalln("ReadBytes error (2): ", err) | |
} | |
// `chunk` is now holding var name + closing delimiter. | |
varName := string(chunk[:len(chunk)-1]) | |
varVal := os.Getenv(varName) | |
bufOut.WriteString(varVal) | |
} else { | |
// Just a random '%', not an opening delimiter, write it out and | |
// keep going. | |
bufOut.WriteRune('%') | |
} | |
} | |
// All substituted. | |
err = ioutil.WriteFile("outfile.toml", bufOut.Bytes(), 0664) | |
if err != nil { | |
log.Fatalln("WriteFile error: ", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment