Skip to content

Commit

Permalink
Add default config object and update README
Browse files Browse the repository at this point in the history
  • Loading branch information
albertrdixon committed Mar 13, 2015
1 parent 044bc45 commit ed534ab
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 20 deletions.
30 changes: 26 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<version>/tnator-linux-amd64-<version>.tar.gz |\
RUN curl -#kL https://github.com/albertrdixon/tmplnator/releases/download/<version>/tnator-linux-amd64.tar.gz |\
tar xvz -C /usr/local/bin
```

Expand All @@ -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 }}
Expand All @@ -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 <key>` if key not found will look in ENV

`dir "/path/to/destination/dir"`: Describe destination directory
`dir "/path/to/destination/dir" <args...>`: 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" <args...>`: Describe name of generated file. Accepts printf style formatting of name string.

`mode <file_mode>`: Describe filemode for generated file

`dir_mode <file_mode>`: Describe mode for any generated directories

`user <uid>`: Describe uid for generated file

`group <gid>`: Describe gid for generated file
Expand All @@ -80,14 +100,16 @@ Access etcd values with `.Var <key>` if key not found will look in ENV

`last <slice>`: Return the last element of the slice

`file_exists <filename.`: True if file exists
`file_exists <filename>`: True if file exists

`parseURL <string>`: Return url.URL object of given url string

`has_key <map> <key>`: True if key exists in map

`default <value> <default_value>`: Output default_value if value is nil or empty string, otherwise output value

`fmt <format> <args...>`: fmt.Sprintf

`split <string>`: strings.Split

`join <slice>`: strings.Join
Expand Down
18 changes: 2 additions & 16 deletions cmd/t2/t2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package config

import (
"fmt"
"os"
"path/filepath"
)

// Config is the config struct
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
}
}

0 comments on commit ed534ab

Please sign in to comment.