Skip to content

Commit

Permalink
alltemplates
Browse files Browse the repository at this point in the history
  • Loading branch information
kellerza committed Jul 11, 2021
1 parent 82a8e31 commit ab704d4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
9 changes: 8 additions & 1 deletion clab/config/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ func RenderAll(nodes map[string]nodes.Node, links map[int]*types.Link) (map[stri
}

if len(TemplateNames) == 0 {
TemplateNames = []string{"base"}
var err error
TemplateNames, err = GetTemplateNamesInDirs(TemplatePaths)
if err != nil {
return nil, err
}
if len(TemplateNames) == 0 {
return nil, fmt.Errorf("no templates files were found by %s path", TemplatePaths)
}
}

tmpl := template.New("").Funcs(jT.Funcs)
Expand Down
26 changes: 24 additions & 2 deletions clab/config/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"fmt"
"path/filepath"
"strconv"
"strings"

Expand Down Expand Up @@ -35,13 +36,13 @@ func PrepareVars(nodes map[string]nodes.Node, links map[int]*types.Link) map[str
for _, node := range nodes {
nodeCfg := node.Config()
name := nodeCfg.ShortName
vars := make(map[string]interface{})
vars := make(Dict)
vars[vkNodeName] = name

// Init array for this node
for key, val := range nodeCfg.Config.Vars {
if key == vkNodes || key == vkNodeName {
log.Warning("the variable %s on %s will be ignored, it hides the other nodes", vkNodes, name)
log.Warningf("the variable %s on %s will be ignored, it hides other nodes", vkNodes, name)
continue
}
vars[key] = val
Expand Down Expand Up @@ -237,3 +238,24 @@ func ipFarEnd(in netaddr.IPPrefix) netaddr.IPPrefix {
}
return netaddr.IPPrefixFrom(n, in.Bits())
}

// GetTemplateNamesInDirs returns a list of template file names found in a dir p
// without traversing nested dirs
// template names are following the pattern <some-name>__<role/kind>.tmpl
func GetTemplateNamesInDirs(paths []string) ([]string, error) {
var tnames []string
for _, p := range paths {
all, err := filepath.Glob(filepath.Join(p, "*__*.tmpl"))
if err != nil {
return nil, err
}
for _, fn := range all {
tn := strings.Split(filepath.Base(fn), "__")[0]
if len(tnames) > 0 && tnames[len(tnames)-1] == tn {
continue
}
tnames = append(tnames, tn)
}
}
return tnames, nil
}
6 changes: 6 additions & 0 deletions lab-examples/vr05/test__vr-sros.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{ range .links }}

Link: {{ $.node }} ({{ $.systemip }}) - {{ .far.node }} ({{ (index $.nodes .far.node).systemip }})
Port: {{ .port }}:{{ .vlan }} - {{ .far.ip }}:{{ .vlan }}
IP: {{ .ip }} - {{ .far.ip }}
{{ end }}

0 comments on commit ab704d4

Please sign in to comment.