1
0
Fork 0
mirror of https://github.com/Luzifer/korvike.git synced 2024-11-09 16:00:09 +00:00

add initial version

This commit is contained in:
Knut Ahlers 2016-07-31 21:13:01 +02:00
parent d6112c11c0
commit a101eeb060
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
3 changed files with 113 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
korvike

96
app.go Normal file
View file

@ -0,0 +1,96 @@
package main
import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"sync"
"text/template"
"github.com/Luzifer/go_helpers/env"
"github.com/Luzifer/rconfig"
)
var (
cfg = struct {
Input string `flag:"in,i" default:"-" description:"Source to read the template from ('-' = stdin)"`
KeyPairs []string `flag:"key-value,v" default:"" description:"Key-value pairs to use in templating (-v key=value)"`
Output string `flag:"out,o" default:"-" description:"Destination to write the output to ('-' = stdout)"`
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
}{}
templateFunctions = template.FuncMap{}
templateFunctionsLock sync.Mutex
version = "dev"
)
func registerFunction(name string, f interface{}) error {
templateFunctionsLock.Lock()
defer templateFunctionsLock.Unlock()
if _, ok := templateFunctions[name]; ok {
return errors.New("Duplicate function for name " + name)
}
templateFunctions[name] = f
return nil
}
func init() {
if err := rconfig.Parse(&cfg); err != nil {
log.Fatalf("Unable to parse commandline options: %s", err)
}
if cfg.VersionAndExit {
fmt.Printf("korvike %s\n", version)
os.Exit(0)
}
}
func openInput(f string) (io.Reader, error) {
if f == "-" {
return os.Stdin, nil
}
if _, err := os.Stat(f); err != nil {
return nil, errors.New("Could not find file " + f)
}
return os.Open(f)
}
func openOutput(f string) (io.Writer, error) {
if f == "-" {
return os.Stdout, nil
}
return os.Create(f)
}
func main() {
in, err := openInput(cfg.Input)
if err != nil {
log.Fatalf("Unable to open input: %s", err)
}
out, err := openOutput(cfg.Output)
if err != nil {
log.Fatalf("Unable to open output: %s", err)
}
rawTpl, err := ioutil.ReadAll(in)
if err != nil {
log.Fatalf("Unable to read from input: %s", err)
}
tpl, err := template.New("in").Funcs(templateFunctions).Parse(string(rawTpl))
if err != nil {
log.Fatalf("Unable to parse template: %s", err)
}
if err := tpl.Execute(out, env.ListToMap(cfg.KeyPairs)); err != nil {
log.Fatalf("Unable to execute template: %s", err)
}
}

16
func_env.go Normal file
View file

@ -0,0 +1,16 @@
package main
import "os"
func init() {
registerFunction("env", func(name string, v ...string) string {
defaultValue := ""
if len(v) > 0 {
defaultValue = v[0]
}
if value, ok := os.LookupEnv(name); ok {
return value
}
return defaultValue
})
}