Skip to content

Commit 8a1825d

Browse files
committed
Closes #13
The problem was due to improperly handling files on the command line. Splitting the file-references from the two types of arguments makes it easier to manage. Also fixes a bug with a prior push that was checking lowercase when it should check original case.
1 parent fa4d325 commit 8a1825d

File tree

5 files changed

+50
-31
lines changed

5 files changed

+50
-31
lines changed

main.go

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ type Settings struct {
3636
Config map[string]interface{}
3737
//
3838
Arguments []string
39+
//
40+
Templates []string
3941
// Add the environment map to the variables.
4042
Environment string
4143
//
@@ -59,6 +61,7 @@ var settings = Settings{
5961
Config: map[string]interface{}{},
6062
ConfigFiles: []string{},
6163
Arguments: []string{},
64+
Templates: []string{},
6265
}
6366

6467
//
@@ -128,21 +131,7 @@ func main() {
128131

129132
settings.Arguments = append(settings.Arguments, ctx.Args()...)
130133

131-
mainName, mainTemplate, noTemplate := "", "", true
132-
if largs := len(settings.Arguments); largs > 0 {
133-
i, found := largs-1, -1
134-
for ; i > 0; i-- {
135-
if !strings.HasPrefix(settings.Arguments[i], "--") {
136-
found = i
137-
}
138-
}
139-
if found >= 0 {
140-
noTemplate = false
141-
mainTemplate = settings.Arguments[found]
142-
}
143-
}
144-
145-
if mainTemplate == "" && !settings.Stdin {
134+
if len(settings.Templates) == 0 && !settings.Stdin {
146135
// Try default the template name
147136
folderName, err := os.Getwd()
148137
if err != nil {
@@ -152,23 +141,32 @@ func main() {
152141
folderName = filepath.Base(folderName)
153142
}
154143

155-
for _, ext := range []string{".tmpl", ""} {
156-
for _, try := range []string{"yaml", "json", "html", "txt", "xml", ""} {
157-
name := fmt.Sprintf("%s.%s%s", folderName, try, ext)
158-
if _, err := os.Stat(name); err == nil {
159-
if settings.Verbose {
160-
log.Printf("using template: %+v", name)
144+
name := func() string {
145+
for _, base := range []string{folderName, "renderizer"} {
146+
for _, ext := range []string{".tmpl", ""} {
147+
for _, try := range []string{"yaml", "json", "html", "txt", "xml", ""} {
148+
name := fmt.Sprintf("%s.%s%s", base, try, ext)
149+
if _, err := os.Stat(name); err == nil {
150+
if settings.Verbose {
151+
log.Printf("using template: %+v", name)
152+
}
153+
return name
154+
}
161155
}
162-
mainTemplate = name
163156
}
164157
}
158+
return ""
159+
}()
160+
if name != "" {
161+
settings.Templates = append(settings.Templates, name)
165162
}
166163
}
167164

168-
if noTemplate {
169-
settings.Arguments = append(settings.Arguments, mainTemplate)
165+
if len(settings.Templates) == 0 {
166+
return cli.NewExitError("missing template name", 1)
170167
}
171-
mainName = strings.Split(strings.TrimLeft(filepath.Base(mainTemplate), "."), ".")[0]
168+
169+
mainName := strings.Split(strings.TrimLeft(filepath.Base(settings.Templates[0]), "."), ".")[0]
172170

173171
switch settings.MissingKey {
174172
case "zero", "error", "default", "invalid":
@@ -248,13 +246,22 @@ func main() {
248246
continue
249247
}
250248
} else if strings.HasPrefix(larg, "-") {
251-
switch larg[1:] {
249+
switch arg[1:] {
252250
case "C":
251+
case "S":
252+
if !strings.Contains(arg, "=") {
253+
next = true
254+
}
255+
fallthrough
253256
default:
254257
args = append(args, arg)
255258
continue
256259
}
260+
} else {
261+
settings.Templates = append(settings.Templates, arg)
262+
continue
257263
}
264+
258265
settings.Arguments = append(settings.Arguments, arg)
259266
}
260267
}

renderizer.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@ import (
2020
func renderizer(_ *cli.Context) error {
2121

2222
globalContext := map[string]interface{}{}
23-
args := []string{}
2423

2524
// Iterate the remaining arguments for variable overrides and file names.
2625

2726
for a, arg := range settings.Arguments {
2827
if len(arg) == 0 {
2928
continue
3029
} else if arg[0] != '-' {
31-
args = append(args, arg)
3230
continue
3331
}
3432

@@ -85,8 +83,8 @@ func renderizer(_ *cli.Context) error {
8583
globalContext = retyper(globalContext, retypeSingleElementSlice)
8684

8785
// If there's no files, read from stdin.
88-
files := args
89-
if len(args) == 0 {
86+
files := settings.Templates
87+
if len(files) == 0 {
9088
if settings.Stdin && settings.Verbose {
9189
log.Println("source: stdin")
9290
}
@@ -96,7 +94,7 @@ func renderizer(_ *cli.Context) error {
9694
// Copy any loaded keys into the globalContext unless they already exist, i.e. they were provided on the command line.
9795
mergo.Merge(&globalContext, settings.Config)
9896

99-
if settings.Environment != "" || len(args) == 0 {
97+
if settings.Environment != "" || len(files) == 0 {
10098
v := make(map[string]string)
10199
for _, item := range os.Environ() {
102100
splits := strings.Split(item, "=")

test/issue-0013.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
3+
parameters=(
4+
# issue 13
5+
"-S=issue-0013.yaml"
6+
)

test/issue-0013.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
OSLIST:
2+
- UBUNTU: '16.04'
3+
OSID: 'ubu1604'
4+
- UBUNTU: '18.04'
5+
OSID: 'ubu1804'

test/issue-0013.yaml.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{ range .OSLIST }}
2+
echo "{{ .UBUNTU }} aka {{ .OSID }}"
3+
{{ end }}

0 commit comments

Comments
 (0)