From ed534ab61639c88c82de17f18db33c4741789c46 Mon Sep 17 00:00:00 2001 From: albertrdixon Date: Fri, 13 Mar 2015 11:57:11 -0700 Subject: [PATCH] Add default config object and update README --- README.md | 30 ++++++++++++++++++++++++++---- cmd/t2/t2.go | 18 ++---------------- config/config.go | 16 ++++++++++++++++ 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 395f65a..595289c 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Enter tmplnator. Templates describe where they should go, what their file mode s In your Dockerfile do something like: ``` -RUN curl -#kL https://github.com/albertrdixon/tmplnator/releases/download//tnator-linux-amd64-.tar.gz |\ +RUN curl -#kL https://github.com/albertrdixon/tmplnator/releases/download//tnator-linux-amd64.tar.gz |\ tar xvz -C /usr/local/bin ``` @@ -37,9 +37,23 @@ Usage of ./t2: -version="": show version ``` +Defaults: + +```golang + Defaults = Config{ + TmplDir: "/templates", + DefaultDir: filepath.Join(os.TempDir(), "T2"), + Delete: false, + Threads: 4, + BpoolSize: 4, + Verbosity: 1, + ShowVersion: false, + } +``` + Super simple. Use the following methods in the template to set up how the file should be generated from the template: `dir` `mode` `user` `group` -``` +```golang # example supervisor.conf template {{ dir "/etc/supervisor" }} {{ mode 0644 }} @@ -56,16 +70,22 @@ Run tmplnator like so: `t2 -template-dir /templates` And that's it! +*NOTE*: Templates without a described `dir` will use `default-dir` as their output directory. + ## Template Functions Access environment variables in the template with `.Env` like so `.Env.VARIABLE` Access etcd values with `.Var ` if key not found will look in ENV -`dir "/path/to/destination/dir"`: Describe destination directory +`dir "/path/to/destination/dir" `: Describe destination directory. Accepts printf style formatting in path string. *NOTE*: Templates without a described `dir` will use `default-dir` as their output directory. + +`name "name" `: Describe name of generated file. Accepts printf style formatting of name string. `mode `: Describe filemode for generated file +`dir_mode `: Describe mode for any generated directories + `user `: Describe uid for generated file `group `: Describe gid for generated file @@ -80,7 +100,7 @@ Access etcd values with `.Var ` if key not found will look in ENV `last `: Return the last element of the slice -`file_exists `: True if file exists `parseURL `: Return url.URL object of given url string @@ -88,6 +108,8 @@ Access etcd values with `.Var ` if key not found will look in ENV `default `: Output default_value if value is nil or empty string, otherwise output value +`fmt `: fmt.Sprintf + `split `: strings.Split `join `: strings.Join diff --git a/cmd/t2/t2.go b/cmd/t2/t2.go index 8c33629..f0628d7 100644 --- a/cmd/t2/t2.go +++ b/cmd/t2/t2.go @@ -8,19 +8,10 @@ import ( "github.com/ian-kent/gofigure" "io/ioutil" "os" - "path/filepath" ) func main() { - var cfg = config.Config{ - TmplDir: "/templates", - DefaultDir: "", - Delete: false, - Threads: 4, - BpoolSize: 4, - Verbosity: 1, - ShowVersion: false, - } + var cfg = config.Defaults // gofigure.Debug = true err := gofigure.Gofigure(&cfg) @@ -46,15 +37,10 @@ func main() { fmt.Printf("Problems reading dir %q: %v\n", cfg.TmplDir, err) os.Exit(2) } - if cfg.DefaultDir == "" { - d, err := ioutil.TempDir("", "T2") - if err != nil { - d = filepath.Join(os.TempDir(), "T2") - } + if d, err := ioutil.TempDir("", "T2"); err == nil { cfg.DefaultDir = d } - // be := backend.New(cfg.Namespace, cfg.EtcdPeers) g, err := generator.NewGenerator(&cfg) if err != nil { fmt.Printf("ERROR: %v", err) diff --git a/config/config.go b/config/config.go index 774cebc..646e4bd 100644 --- a/config/config.go +++ b/config/config.go @@ -2,6 +2,8 @@ package config import ( "fmt" + "os" + "path/filepath" ) // Config is the config struct @@ -40,6 +42,8 @@ func (b *boolflag) IsBoolFlag() bool { var ( // Build is passed in via ldflags Build string + // Defaults is a config with default values + Defaults Config ) func RuntimeVersion(version string, build string) string { @@ -51,3 +55,15 @@ func RuntimeVersion(version string, build string) string { } return vers } + +func init() { + Defaults = Config{ + TmplDir: "/templates", + DefaultDir: filepath.Join(os.TempDir(), "T2"), + Delete: false, + Threads: 4, + BpoolSize: 4, + Verbosity: 1, + ShowVersion: false, + } +}