-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
103 lines (81 loc) · 3.02 KB
/
file.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package datasource
import (
"bytes"
"fmt"
"html/template"
"io"
"os"
"path/filepath"
"github.com/Masterminds/sprig/v3"
"github.com/Sirupsen/logrus"
"github.com/spf13/viper"
)
// load a dile type datasource from the viper configuration.
func loadFileDatasource(log *logrus.Entry, recipePath string, filename string, v *viper.Viper, engine Engine, envVar map[string]string) (Datasource, error) {
log.Debugf("Loading %s file datasource", filename)
var ds Datasource
ds.dstype = File
ds.engine = engine
ds.name = filename
ds.file.Inline = v.GetString("inline")
type tmplEnv struct {
Environments map[string]string
}
data := tmplEnv{Environments: envVar}
fileTmpl, err := template.New("file").Funcs(sprig.FuncMap()).Parse(v.GetString("file"))
if err != nil {
return Datasource{}, fmt.Errorf("parsing file provided: %w", err)
}
var file bytes.Buffer
if err = fileTmpl.Execute(&file, data); err != nil {
return Datasource{}, fmt.Errorf("expanding file provided: %w", err)
}
ds.file.FilePath = file.String()
if ds.file.FilePath != "" && ds.file.FilePath != "-" && ds.file.FilePath[0] != '/' {
ds.file.FilePath = filepath.Join(recipePath, ds.file.FilePath)
}
URLTmpl, err := template.New("URL").Funcs(sprig.FuncMap()).Parse(v.GetString("URL"))
if err != nil {
return Datasource{}, fmt.Errorf("parsing URL provided: %w", err)
}
var URL bytes.Buffer
if err = URLTmpl.Execute(&URL, data); err != nil {
return Datasource{}, fmt.Errorf("expanding URL provided: %w", err)
}
ds.file.URL = URL.String()
if ds.file.FilePath == "" && ds.file.URL == "" && ds.file.Inline == "" {
return Datasource{}, fmt.Errorf("no file path or URL provided: %w", err)
}
ds.tags = v.GetStringSlice("tags")
if len(ds.tags) == 0 {
ds.tags = []string{""}
}
ds.file.Zip = v.GetBool("zip")
ds.file.Gzip = v.GetBool("gzip")
ds.file.ZippedExt = EngineToString(engine)
return ds, nil
}
//OpenReadFile open and return a io.ReadCloser corresponding to the datasource to be used by providers.
func (ds *Datasource) OpenReadFile(log *logrus.Entry) (io.ReadCloser, error) {
logFile := log.WithField("engine", EngineToString(ds.engine))
return ds.file.OpenReadFile(logFile)
}
//OpenWriteFile open and return a io.WriteCloser corresponding to the datasource to be used by providers.
func (ds *Datasource) OpenWriteFile(log *logrus.Entry) (io.WriteCloser, error) {
logFile := log.WithField("engine", EngineToString(ds.engine))
return ds.file.OpenWriteFile(logFile)
}
//ResetFile close the file and remove the temporary file.
func (ds *Datasource) ResetFile(log *logrus.Entry) error {
logFile := log.WithField("engine", EngineToString(ds.engine))
return ds.file.ResetFile(logFile)
}
//CloseFile close the file and rename the temporary file to real name (if exists).
func (ds *Datasource) CloseFile(log *logrus.Entry) error {
logFile := log.WithField("engine", EngineToString(ds.engine))
return ds.file.CloseFile(logFile)
}
// Stat returns os.FileInfo on the file of the datasource.
func (ds *Datasource) Stat() (os.FileInfo, error) {
return ds.file.Stat()
}