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 Jul 6, 2020
1 parent 62b18e4 commit cf5b9da
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions cmd/tk/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import (
"github.com/grafana/tanka/pkg/tanka"
)

// BelRune is a string of the Ascii character BEL which made computers ring in ancient times
// We use it as "magic" char for the subfolder creation as it is a non printable character and thereby will never be
// in a valid filepath by accident. Only when we include it.
const BelRune = string(rune(7))

func exportCmd() *cli.Command {
args := workflowArgs
args.Validator = cli.ValidateExact(2)
Expand Down Expand Up @@ -50,7 +55,11 @@ func exportCmd() *cli.Command {
}

// exit early if the template is bad
tmpl, err := template.New("").Funcs(templateFuncMap).Parse(*format)

// Replace all os.path separators in string with BelRune for creating subfolders
replacedFormat := strings.Replace(*format, string(os.PathSeparator), BelRune, -1)

tmpl, err := template.New("").Funcs(templateFuncMap).Parse(replacedFormat)
if err != nil {
return fmt.Errorf("Parsing name format: %s", err)
}
Expand All @@ -70,11 +79,21 @@ func exportCmd() *cli.Command {
if err := tmpl.Execute(&buf, m); err != nil {
log.Fatalln("executing name template:", err)
}
name := strings.Replace(buf.String(), "/", "-", -1)

// Replace all os.path separators in string in order to not accidentally create subfolders
name := strings.Replace(buf.String(), string(os.PathSeparator), "-", -1)
// Replace the BEL character inserted with a path separator again in order to create a subfolder
name = strings.Replace(name, BelRune, string(os.PathSeparator), -1)

// Create all subfolders in path
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

0 comments on commit cf5b9da

Please sign in to comment.