-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
source.go
51 lines (40 loc) · 1.12 KB
/
source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package json
import (
"errors"
"fmt"
"strings"
"github.com/sirupsen/logrus"
"github.com/updatecli/updatecli/pkg/core/result"
)
func (j *Json) Source(workingDir string) (string, error) {
if len(j.contents) > 1 {
return "", errors.New("source only supports one file")
}
sourceOutput := ""
for i := range j.contents {
if err := j.contents[i].Read(workingDir); err != nil {
return "", err
}
queryResult, err := j.contents[i].DaselNode.Query(j.spec.Key)
if err != nil {
// Catch error message returned by Dasel, if it couldn't find the node
// This is approach is not very robust
// https://github.com/TomWright/dasel/blob/master/node_query.go#L58
if strings.HasPrefix(err.Error(), "could not find value:") {
err := fmt.Errorf("%s cannot find value for path %q from file %q",
result.FAILURE,
j.spec.Key,
j.contents[i].FilePath)
return "", err
}
return "", err
}
logrus.Infof("%s Value %q, found in file %q, for key %q'",
result.SUCCESS,
queryResult.String(),
j.contents[i].FilePath,
j.spec.Key)
sourceOutput = queryResult.String()
}
return sourceOutput, nil
}