Skip to content

Commit

Permalink
Use mkdir template function instead of additional flag for creating a…
Browse files Browse the repository at this point in the history
… subfolder
  • Loading branch information
simonfrey committed Jun 25, 2020
1 parent 75b35fe commit 91b9b81
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 27 deletions.
11 changes: 4 additions & 7 deletions cmd/tk/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@ func exportCmd() *cli.Command {

vars := workflowFlags(cmd.Flags())
getExtCode := extCodeParser(cmd.Flags())
createDirs := cmd.Flags().Bool("dirs", false, "Treat '/' in filenames as directories")
format := cmd.Flags().String("format", "{{.apiVersion}}.{{.kind}}-{{.metadata.name}}", "https://tanka.dev/exporting#filenames")
extension := cmd.Flags().String("extension", "yaml", "File extension")

templateFuncMap := template.FuncMap{
"lower": func(s string) string {
return strings.ToLower(s)
},
"replace": func(old, new string, s string) string {
return strings.Replace(s, old, new, -1)
"mkdir": func() string {
return string(rune(7))
},
}

Expand Down Expand Up @@ -75,10 +74,8 @@ func exportCmd() *cli.Command {
log.Fatalln("executing name template:", err)
}

name := buf.String()
if !*createDirs {
name = strings.Replace(name, "/", "-", -1)
}
name := strings.Replace(buf.String(), "/", "-", -1)
name = strings.Replace(name, string(rune(7)), "/", -1)
path := filepath.Join(to, name+"."+*extension)
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
return fmt.Errorf("creating filepath '%s': %s", filepath.Dir(path), err)
Expand Down
27 changes: 7 additions & 20 deletions docs/docs/exporting.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,13 @@ You can optionally use one of the following template functions:
loki-distributor-deployment
```
etc.
- `replace` for replacing occurrences of a character
- `mkdir` create new subdirectory
```bash
... --format='{{.metadata.labels.app}}-{{.metadata.name}}-{{.kind | replace "e" "_" }}'
... --format='{{.metadata.labels.app}}{{mkdir}}{{.metadata.name}}{{mkdir}}{{.kind}}'
```
would yield

```
loki-distributor-d_ploym_nt
```
etc.

You can also use a different file extension by providing `--extension='yml'`, for example.
would create following directory structure:
- loki
- distributor
- deployment

If you want your format to be able to create subfolders you can enable this by providing `--dirs`.
With this format
```bash
... --format='{{.metadata.labels.app}}/{{.metadata.name}}/{{.kind}}' --dirs
```
you would create following folder structure
- loki
- distributor
- deployment
etc.
You can also use a different file extension by providing `--extension='yml'`, for example.

0 comments on commit 91b9b81

Please sign in to comment.