~robertgzr/yamlfs

cba22b0be1e7fe993eb1e7d08e63da6d67adcb2d — Robert Günzler 1 year, 9 months ago 080a931
mark optional variables as such, when listing actiond
2 files changed, 52 insertions(+), 29 deletions(-)

M cmd/yamlfs/helpers.go
M cmd/yamlfs/main.go
M cmd/yamlfs/helpers.go => cmd/yamlfs/helpers.go +24 -17
@@ 33,25 33,28 @@ func checkVariables(tpls *template.Template, defined map[string]string) map[stri
	return variables
}

func checkNodes(nodes []parse.Node, variables map[string]string, inv bool) {
func checkNodes(nodes []parse.Node, variables map[string]string, optional bool) {
	for _, n := range nodes {
		checkNode(n, variables, inv)
		checkNode(n, variables, optional)
	}
}

func checkNode(node parse.Node, variables map[string]string, inv bool) {
func checkNode(node parse.Node, variables map[string]string, optional bool) {
	switch node.Type() {
	case parse.NodeField:
		node := node.(*parse.FieldNode)
		for _, field := range node.Ident {
			val, ok := variables[field]
			if !ok && !inv {
				log.Printf("adding %q to %v", field, variables)
				variables[field] = ""
			if ok && val != "" && val != "<optional>" {
				log.Printf("found %q = %q in %v", field, val, variables)
			}
			if ok && val == "" && inv {
				log.Printf("removing %q from %v", field, variables)
				delete(variables, field)
			if !ok {
				log.Printf("adding %q to %v", field, variables)
				if optional {
					variables[field] = "<optional>"
				} else {
					variables[field] = ""
				}
			}
		}



@@ 71,37 74,41 @@ func checkNode(node parse.Node, variables map[string]string, inv bool) {
					}
				}
			}
			checkNodes(c.Args, variables, inv)
			checkNodes(c.Args, variables, optional)
		}

	case parse.NodeIf:
		node := node.(*parse.IfNode).BranchNode
		if node.List != nil {
			checkNodes(node.List.Nodes, variables, inv)
			checkNodes(node.List.Nodes, variables, optional)
		}
		if node.ElseList != nil {
			checkNodes(node.ElseList.Nodes, variables, inv)
			checkNodes(node.ElseList.Nodes, variables, optional)
		}
		checkPipeNode(node.Pipe, variables, true)

	case parse.NodeWith:
		node := node.(*parse.WithNode).BranchNode
		if node.List != nil {
			checkNodes(node.List.Nodes, variables, inv)
			checkNodes(node.List.Nodes, variables, optional)
		}
		if node.ElseList != nil {
			checkNodes(node.ElseList.Nodes, variables, inv)
			checkNodes(node.ElseList.Nodes, variables, optional)
		}
		checkPipeNode(node.Pipe, variables, true)

	case parse.NodePipe:
		node := node.(*parse.PipeNode)
		checkPipeNode(node, variables, optional)
	}
}

func checkPipeNode(node *parse.PipeNode, variables map[string]string, inv bool) {
func checkPipeNode(node *parse.PipeNode, variables map[string]string, optional bool) {
	for _, c := range node.Cmds {
		checkNodes(c.Args, variables, inv)
		checkNodes(c.Args, variables, optional)
	}
	for _, decl := range node.Decl {
		checkNode(decl, variables, inv)
		checkNode(decl, variables, optional)
	}
}


M cmd/yamlfs/main.go => cmd/yamlfs/main.go +28 -12
@@ 7,6 7,7 @@ import (
	"log"
	"os"
	"os/exec"
	"strconv"

	flag "github.com/spf13/pflag"



@@ 81,19 82,34 @@ func main() {
		os.Exit(1)
	}

	undefinedVariables := checkVariables(tpl, tplLiteral)
	variables := make(map[string]interface{})
	undefinedVariables := make(map[string]struct{})

	variables_ := checkVariables(tpl, tplLiteral)
	// FIXME this should consider `limits`
	for key, val := range undefinedVariables {
		log.Printf("%q: %q", key, val)
		defined := false
		if val != "" && val != "-" {
			defined = true
			delete(undefinedVariables, key)
	for key, val := range variables_ {
		if val == "" || val == "-" {
			undefinedVariables[key] = struct{}{}
			delete(variables_, key)
		}
		if val == "<optional>" {
			delete(variables_, key)
		}
	}
	for key, val := range variables_ {
		if boolean, err := strconv.ParseBool(val); err == nil {
			variables[key] = boolean
		} else if number, err := strconv.ParseInt(val, 10, 64); err == nil {
			variables[key] = number
		} else {
			variables[key] = val
		}
		if defined {
			fmt.Fprintf(os.Stderr, "[%s] %s (%q)\n", boolToAscii(defined), key, val)

		_, undefined := undefinedVariables[key]
		if undefined {
			fmt.Fprintf(os.Stderr, "[%s] %s\n", boolToAscii(false), key)
		} else {
			fmt.Fprintf(os.Stderr, "[%s] %s\n", boolToAscii(defined), key)
			fmt.Fprintf(os.Stderr, "[%s] %s (%#v)\n", boolToAscii(true), key, variables[key])
		}
	}
	if len(undefinedVariables) > 0 {


@@ 102,8 118,8 @@ func main() {
	}
	var buf bytes.Buffer
	for _, t := range tpl.Templates() {
		log.Printf("%v", tplLiteral)
		if err := t.Execute(&buf, tplLiteral); err != nil {
		log.Printf("%v", variables)
		if err := t.Execute(&buf, variables); err != nil {
			fmt.Fprintf(os.Stderr, "rendering template failed: %s\n", err)
			os.Exit(1)
		}