Skip to content

Commit b3ee57a

Browse files
committed
Avoid panic when parsing a variables file with zero length; fixes #201
1 parent 93f0a90 commit b3ee57a

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

pkg/deployclient/deployer.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import (
77
"strings"
88
"time"
99

10-
"github.com/nais/deploy/pkg/hookd/logproxy"
11-
"github.com/nais/deploy/pkg/pb"
1210
log "github.com/sirupsen/logrus"
1311
"google.golang.org/grpc/codes"
12+
13+
"github.com/nais/deploy/pkg/hookd/logproxy"
14+
"github.com/nais/deploy/pkg/pb"
1415
)
1516

1617
type TemplateVariables map[string]interface{}

pkg/deployclient/template.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import (
1414
)
1515

1616
func MultiDocumentFileAsJSON(path string, ctx TemplateVariables) ([]json.RawMessage, error) {
17-
file, err := os.ReadFile(path)
17+
fileContents, err := os.ReadFile(path)
1818
if err != nil {
1919
return nil, fmt.Errorf("%s: open file: %s", path, err)
2020
}
2121

22-
templated, err := templatedFile(file, ctx)
22+
templated, err := templatedFile(fileContents, ctx)
2323
if err != nil {
2424
errMsg := strings.ReplaceAll(err.Error(), "\n", ": ")
2525
return nil, fmt.Errorf("%s: %s", path, errMsg)
@@ -118,6 +118,11 @@ func templateVariablesFromFile(path string) (TemplateVariables, error) {
118118
vars := TemplateVariables{}
119119
err = yaml.Unmarshal(file, &vars)
120120

121+
// This function MUST return a non-nil map to avoid panics later on.
122+
if vars == nil {
123+
vars = TemplateVariables{}
124+
}
125+
121126
return vars, err
122127
}
123128

0 commit comments

Comments
 (0)