Skip to content

Commit

Permalink
newvar
Browse files Browse the repository at this point in the history
  • Loading branch information
kellerza committed Jul 22, 2021
1 parent a14756a commit b349cc1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
14 changes: 5 additions & 9 deletions clab/config/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func RenderAll(nodes map[string]nodes.Node, links map[int]*types.Link) (map[stri
var buf strings.Builder
err := tmpl.ExecuteTemplate(&buf, tmplN, vars)
if err != nil {
res[nodeName].Print(0, true)
res[nodeName].Print(true, true)
return nil, err
}

Expand All @@ -106,18 +106,17 @@ func RenderAll(nodes map[string]nodes.Node, links map[int]*types.Link) (map[stri

// Implement stringer for NodeConfig
func (c *NodeConfig) String() string {

s := fmt.Sprintf("%s: %v", c.TargetNode.ShortName, c.Info)
return s
}

// Print the config
func (c *NodeConfig) Print(printLines int, forceDebug ...bool) {
func (c *NodeConfig) Print(vars, rendered bool) {
var s strings.Builder

s.WriteString(c.TargetNode.ShortName)

if log.IsLevelEnabled(log.DebugLevel) || len(forceDebug) > 0 {
if vars {
s.WriteString(" vars = ")
var saved_nodes Dict
restore := false
Expand Down Expand Up @@ -146,14 +145,11 @@ func (c *NodeConfig) Print(printLines int, forceDebug ...bool) {
}
}

if printLines > 0 {
if rendered {
for idx, conf := range c.Data {
fmt.Fprintf(&s, "\n Template %s for %s = [[", c.Info[idx], c.TargetNode.ShortName)

cl := strings.SplitN(conf, "\n", printLines+1)
if len(cl) > printLines {
cl[printLines] = "..."
}
cl := strings.Split(conf, "\n")
for _, l := range cl {
s.WriteString("\n ")
s.WriteString(l)
Expand Down
23 changes: 18 additions & 5 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/srl-labs/containerlab/nodes"
)

// Only print config locally, don't send to the node
var printLines int
// Dryrun and print config
var check string

// configCmd represents the config command
var configCmd = &cobra.Command{
Expand Down Expand Up @@ -41,9 +41,21 @@ var configCmd = &cobra.Command{
return err
}

if printLines > 0 {
if check != "" {

pv := check == "all" || check == "vars"
pt := check == "all" || check == "template"

if !(pv || pt) {
if c, ok := allConfig[check]; ok {
c.Print(true, true)
return nil
}
log.Warnf("Invalid command line option for check. It needs to be either 'template'(default), 'vars', 'all' or a valid node name")
pt = true
}
for _, c := range allConfig {
c.Print(printLines)
c.Print(pv, pt)
}
return nil
}
Expand Down Expand Up @@ -104,5 +116,6 @@ func init() {
configCmd.Flags().StringSliceVarP(&config.TemplatePaths, "template-path", "p", []string{}, "comma separated list of paths to search for templates")
_ = configCmd.MarkFlagDirname("template-path")
configCmd.Flags().StringSliceVarP(&config.TemplateNames, "template-list", "l", []string{}, "comma separated list of template names to render")
configCmd.Flags().IntVarP(&printLines, "check", "c", 0, "render templates in dry-run mode & print N lines of rendered config")
configCmd.Flags().StringVarP(&check, "check", "c", "", "render templates in dry-run mode & print N lines of rendered config")
configCmd.Flags().Lookup("check").NoOptDefVal = "template"
}

0 comments on commit b349cc1

Please sign in to comment.