-P / --skip-parent

This commit is contained in:
Ian Gulliver
2023-07-07 00:23:17 +01:00
parent 5d3c8ba87e
commit f5c4d506c8
9 changed files with 28 additions and 22 deletions
-13
View File
@@ -33,19 +33,6 @@ func New() *Parser {
return &Parser{}
}
// NewFromFile creates a new [Parser] then calls [Parser.MergeFileLayers] with
// the supplied path to merge in the file and its parent layers.
func NewFromFile(path string) (*Parser, error) {
p := New()
err := p.MergeFileLayers(path)
if err != nil {
return nil, err
}
return p, nil
}
// SetDebug enables or disables debug log output to stderr.
func (p *Parser) SetDebug(debug bool) {
p.debug = debug
+13 -6
View File
@@ -11,6 +11,7 @@ import (
type options struct {
OutputPath *string `short:"o" long:"output" description:"output file path"`
OutputFormat *string `short:"f" long:"format" description:"output format"`
SkipParent bool `short:"P" long:"skip-parent" description:"skip loading parent templates"`
Verbose bool `short:"v" long:"verbose" description:"enable verbose logging"`
Positional struct {
@@ -35,14 +36,20 @@ func main() {
}
for _, path := range opts.Positional.InputPaths {
fileP, err := bkl.NewFromFile(path)
fileP := bkl.New()
if opts.SkipParent {
err = fileP.MergeFile(path)
} else {
err = fileP.MergeFileLayers(path)
}
if err != nil {
fatal("%s", err)
fatal(err)
}
err = p.MergeParser(fileP)
if err != nil {
fatal("%s", err)
fatal(err)
}
}
@@ -58,11 +65,11 @@ func main() {
}
if err != nil {
fatal("%s", err)
fatal(err)
}
}
func fatal(format string, v ...any) {
fmt.Fprintf(os.Stderr, format+"\n", v...)
func fatal(err error) {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
+8 -1
View File
@@ -380,13 +380,20 @@ $ <cmd>bkl</cmd> <focus><string>c.d.yaml</string></focus> # a.yaml + a.b.yaml
<p>Inheritance is determined using filenames by default. After stripping the extension, the remaining filename is split on <inlineFocus>.</inlineFocus> and treated as an inheritance hierarchy (e.g. <inlineFocus>a.b.c.yaml</inlineFocus> inherits from <inlineFocus>a.b.&lt;ext&gt;</inlineFocus> inherits from <inlineFocus>a.&lt;ext&gt;</inlineFocus>). Parent templates may have any supported file extension.</p>
<label>Set Parent</label>
<label>Set Parent In File</label>
<code class="labeled"><focus><key>$parent</key>: <string>a.b</string></focus> # inherits from a.b.&lt;ext&gt;, from a.&lt;ext&gt;</code>
<vSpace></vSpace>
<p><inlineFocus>$parent</inlineFocus> can only be used in the document root. bkl will still check for all supported endings of a manually-specified parent file, and will still evaluate template layers of the parent in the normal order.</p>
<label>Set Parent In CLI</label>
<code>$ <cmd>bkl</cmd> <focus>-P <string>a.b.yaml</string> <string>c.d.yaml</string></focus> # a.b.yaml + c.d.yaml</code>
<vSpace></vSpace>
<p>The <inlineFocus>-P</inlineFocus> flag (or <inlineFocus>--skip-parent</inlineFocus>) tells bkl to not load parent templates using filenames or <inlineFocus>$parent</inlineFocus>. Layer order can then be specified with just commandline argument order.</p>
<vSpace></vSpace>
<vSpace></vSpace>
+1 -1
View File
@@ -1 +1 @@
bkl -v -f yaml a.b.yaml
bkl -f yaml a.b.yaml
+1 -1
View File
@@ -1 +1 @@
bkl -v -f yaml a.b.yaml
bkl -f yaml a.b.yaml
+1
View File
@@ -0,0 +1 @@
a: 1
+1
View File
@@ -0,0 +1 @@
b: 2
+1
View File
@@ -0,0 +1 @@
bkl -P -f yaml a.b.yaml c.yaml
+2
View File
@@ -0,0 +1,2 @@
a: 1
b: 2