Skip to content

Commit

Permalink
Merge pull request #49 from UKHomeOffice/split
Browse files Browse the repository at this point in the history
A couple of features
  • Loading branch information
vaijab authored Oct 18, 2017
2 parents 389e67e + d5acc40 commit 0fb1de4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,48 @@ $ kd --context=mykube --namespace=testing --file nginx-deployment.yaml
You can fail an ongoing deployment if there's been a new deployment by adding `--fail-superseded` flag.


## Templating

### split

`split` function is go's `strings.Split()`, it returns a `[]string`. A range function
can also be used to iterate over returned list.

```yaml
# split.yaml
---
apiVersion: v1
data:
foo: {{ split .LIST " "}}
kind: ConfigMap
metadata:
name: list
```

```
$ export LIST="one,two,three"
$ ./kd -f split.yaml -- --dry-run -o yaml
[INFO] 2017/10/18 15:08:09 main.go:241: deploying configmap/list
[INFO] 2017/10/18 15:08:09 main.go:248: apiVersion: v1
data:
foo:
- one
- two
- three
kind: ConfigMap
metadata:
name: list
```
## Configuration
Configuration can be provided via cli flags and arguments as well as
environment variables.
It supports end of flags `--` parameter, any flags or arguments that are
specified after `--` will be passed onto kubectl.
```bash
$ kd --help
Expand Down
26 changes: 25 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,11 @@ func run(c *cli.Context) error {
}

func render(tmpl string, vars map[string]string) (string, error) {
t := template.Must(template.New("template").Parse(tmpl))
fm := template.FuncMap{
"split": strings.Split,
}

t := template.Must(template.New("template").Funcs(fm).Parse(tmpl))
t.Option("missingkey=error")
var b bytes.Buffer
if err := t.Execute(&b, vars); err != nil {
Expand Down Expand Up @@ -338,9 +342,29 @@ func newKubeCmd(c *cli.Context, args []string) (*exec.Cmd, error) {
args = append([]string{"--server=" + c.String("kube-server")}, args...)
}

flags, err := extraFlags(c)
if err != nil {
return nil, err
}
args = append(args, flags...)

return exec.Command(kube, args...), nil
}

func extraFlags(c *cli.Context) ([]string, error) {
var a []string

if c.NArg() < 1 {
return a, nil
}

if c.Args()[0] == "--" {
return c.Args()[1:], nil
}

return c.Args(), nil
}

// listDirectory returns a recursive list of all files under a directory, or an error
func listDirectory(path string) ([]string, error) {
var list []string
Expand Down

0 comments on commit 0fb1de4

Please sign in to comment.