Skip to content

Commit

Permalink
function mkdir
Browse files Browse the repository at this point in the history
  • Loading branch information
mandelsoft committed May 6, 2020
1 parent 8c0bae5 commit 30bdddb
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 2 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ Contents:
- [(( write("file.yml", data) ))](#-writefileyml-data-)
- [(( tempfile("file.yml", data) ))](#-tempfilefileyml-data-)
- [(( lookup_file("file.yml", data) ))](#-lookup_filefileyml-list-)
- [(( mkdir("dir", 0755) ))](#-mkdirdir-0755-)
- [(( list_files(".") ))](#-list_files-)
- [(( archive(files, "tar") ))](#-archivefiles-tar-)
- [X509 Functions](#x509-functions)
Expand Down Expand Up @@ -2972,6 +2973,14 @@ If no existing files can be found the empty list is returned.
It is possible to pass multiple list or string arguments to compose the
search path.

#### `(( mkdir("dir", 0755) ))`

Create a directory and all its intermediate directories if they do not
exist yet.

The permission part is optional (default 0755). The path of the directory
might be given by atring like value or as a list of path components.

#### `(( list_files(".") ))`

List files in a directory. The result is a list of existing
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var cfgFile string
var rootCmd = &cobra.Command{
Use: "spiff",
Short: "YAML in-domain templating processor",
Version: "v1.4.0",
Version: "v1.4.1-dev",
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down
64 changes: 64 additions & 0 deletions dynaml/mkdir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package dynaml

import (
"os"
"path/filepath"

"github.com/mandelsoft/spiff/yaml"
)

func init() {
RegisterFunction("mkdir", func_mkdir)
}

func func_mkdir(arguments []interface{}, binding Binding) (interface{}, EvaluationInfo, bool) {
var err error
info := DefaultInfo()

if len(arguments) < 1 || len(arguments) > 2 {
return info.Error("mkdir requires one or two arguments")
}
path, _, ok := getArg(0, arguments[0], false)
if !ok {
comps, ok := arguments[0].([]yaml.Node)
if !ok {
return info.Error("path argument must be a non-empty string")
}
for i, a := range comps {
comp, _, ok := getArg(i, a.Value(), false)
if !ok || comp == "" {
return info.Error("path component %d must be a non-empty string", i)
}
path = filepath.Join(path, comp)
}
}
if path == "" {
return info.Error("path argument must not be empty")
}

permissions := int64(0755)
binary := false
if len(arguments) == 2 {
switch v := arguments[1].(type) {
case string:
permissions, binary, err = getWriteOptions(v, 0644)
if err != nil {
return info.Error("%s", err)
}
if binary {
return info.Error("binary option not supported for mkdir")
}
case int64:
permissions = v
default:
return info.Error("permissions must be given as int or int string")
}
}

err = os.MkdirAll(path, os.FileMode(permissions))
if err != nil {
return info.Error("cannot create directory %q: %s", path, err)
}

return path, info, true
}
2 changes: 1 addition & 1 deletion dynaml/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func func_write(arguments []interface{}, binding Binding) (interface{}, Evaluati
info := DefaultInfo()

if len(arguments) < 2 || len(arguments) > 3 {
return info.Error("write requires two arguments")
return info.Error("write requires two or three arguments")
}
file, _, ok := getArg(0, arguments[0], false)
if !ok || file == "" {
Expand Down
5 changes: 5 additions & 0 deletions flow/cascade.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func Apply(outer dynaml.Binding, template yaml.Node, prepared []yaml.Node) (yaml
result, err := NestedFlow(outer, template, prepared...)
if err == nil {
result = Cleanup(result, discardTemporary)
result = Cleanup(result, unescapeDynaml)
}
return result, err
}
Expand All @@ -41,6 +42,10 @@ func discardTemporary(node yaml.Node) (yaml.Node, CleanupFunction) {
return node, discardTemporary
}

func unescapeDynaml(node yaml.Node) (yaml.Node, CleanupFunction) {
return yaml.UnescapeDynaml(node), unescapeDynaml
}

func discardLocal(node yaml.Node) (yaml.Node, CleanupFunction) {
if node.Local() {
return nil, discardLocal
Expand Down
15 changes: 15 additions & 0 deletions yaml/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,18 @@ func EmbeddedDynaml(root Node) *string {
}
return nil
}

func UnescapeDynaml(root Node) Node {
rootString, ok := root.Value().(string)
if !ok {
return root
}
if strings.HasPrefix(rootString, "((") &&
strings.HasSuffix(rootString, "))") {
sub := rootString[2 : len(rootString)-2]
if strings.HasPrefix(sub, "!") {
return NewNode("((" + sub[1:]+"))", root.SourceName() )
}
}
return root
}

0 comments on commit 30bdddb

Please sign in to comment.