Skip to content

Commit

Permalink
Allow file format to create subfolders
Browse files Browse the repository at this point in the history
  • Loading branch information
simonfrey committed Jun 25, 2020
1 parent 62b18e4 commit 44f9e6c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
15 changes: 12 additions & 3 deletions cmd/tk/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func exportCmd() *cli.Command {

vars := workflowFlags(cmd.Flags())
getExtCode := extCodeParser(cmd.Flags())
allowSubFolderCreation := cmd.Flags().Bool("allow-subfolder-creation", false, "Allow export to create subfolders in the out directory if the format contains '/'")
format := cmd.Flags().String("format", "{{.apiVersion}}.{{.kind}}-{{.metadata.name}}", "https://tanka.dev/exporting#filenames")
extension := cmd.Flags().String("extension", "yaml", "File extension")

Expand Down Expand Up @@ -70,11 +71,19 @@ func exportCmd() *cli.Command {
if err := tmpl.Execute(&buf, m); err != nil {
log.Fatalln("executing name template:", err)
}
name := strings.Replace(buf.String(), "/", "-", -1)

name := buf.String()
if allowSubFolderCreation == nil || !*allowSubFolderCreation {
name = strings.Replace(name, "/", "-", -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)
}

data := m.String()
if err := ioutil.WriteFile(filepath.Join(to, name+"."+*extension), []byte(data), 0644); err != nil {
return fmt.Errorf("Writing manifest: %s", err)
if err := ioutil.WriteFile(path, []byte(data), 0644); err != nil {
return fmt.Errorf("writing manifest: %s", err)
}
}

Expand Down
45 changes: 33 additions & 12 deletions docs/docs/exporting.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,39 @@ loki-loki-ConfigMap
loki-ingester-Service
```

You can optionally use the template function `lower` for lower-casing fields, e.g. in the above example
You can optionally use one of the following template functions:

- `lower` for lower-casing fields, e.g. in the above example

```bash
... --format='{{.metadata.labels.app}}-{{.metadata.name}}-{{.kind | lower}}'
```
would yield

```
loki-distributor-deployment
```
etc.
- `replace` for replacing occurrences of a character
```bash
... --format='{{.metadata.labels.app}}-{{.metadata.name}}-{{.kind | replace "e" "_" }}'
```
would yield

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

```bash
... --format='{{.metadata.labels.app}}-{{.metadata.name}}-{{.kind | lower}}'
```

would yield
You can also use a different file extension by providing `--extension='yml'`, for example.

If you want your format to be able to create subfolders you can enable this by providing `--allow-subfolder-creation`.
With this format
```bash
... --format='{{.metadata.labels.app}}/{{.metadata.name}}/{{.kind}}' --allow-subfolder-creation
```
loki-distributor-deployment
```

etc.

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

0 comments on commit 44f9e6c

Please sign in to comment.