-
Notifications
You must be signed in to change notification settings - Fork 8
/
generic.go
65 lines (54 loc) · 1.44 KB
/
generic.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
package platformifier
import (
"bytes"
"context"
"fmt"
"io"
"io/fs"
"strings"
"text/template"
"github.com/platformsh/platformify/vendorization"
"github.com/Masterminds/sprig/v3"
)
func newGenericPlatformifier(templates fs.FS, fileSystem FS) *genericPlatformifier {
return &genericPlatformifier{
templates: templates,
fileSystem: fileSystem,
}
}
// genericPlatformifier contains the configuration for the application to Platformify
type genericPlatformifier struct {
templates fs.FS
fileSystem FS
}
// Platformify will generate the needed configuration files in the current directory.
func (p *genericPlatformifier) Platformify(ctx context.Context, input *UserInput) error {
assets, _ := vendorization.FromContext(ctx)
err := fs.WalkDir(p.templates, ".", func(name string, d fs.DirEntry, walkErr error) error {
if d.IsDir() {
return nil
}
tpl := template.Must(template.New(d.Name()).Funcs(sprig.FuncMap()).ParseFS(p.templates, name))
contents := &bytes.Buffer{}
if err := tpl.Execute(contents, templateData{input, assets}); err != nil {
return err
}
// Skip empty files
if strings.TrimSpace(contents.String()) == "" {
return nil
}
f, writeErr := p.fileSystem.Create(name)
if writeErr != nil {
return fmt.Errorf("could not write template: %w", writeErr)
}
defer f.Close()
if _, err := io.Copy(f, contents); err != nil {
return err
}
return nil
})
if err != nil {
return err
}
return nil
}