-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen.go
92 lines (79 loc) · 2 KB
/
gen.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
// Package embed provides yaml discovery with embed actions definition.
package embed
import (
"bytes"
"fmt"
"os"
"path/filepath"
"text/template"
"github.com/launchrctl/launchr/internal/launchr"
)
const pluginTemplate = `
{{- print "// GENERATED. DO NOT EDIT." }}
package gen
import (
_ "embed"
"github.com/launchrctl/launchr"
yamlembed "github.com/launchrctl/launchr/pkg/plugins/yamldiscovery/embed"
)
//go:embed {{.ActionsTarPath}}
var tarFsBytes []byte
type {{.StructName}} struct {
app launchr.App
}
// PluginInfo implements launchr.Plugin interface.
func (p *{{.StructName}}) PluginInfo() launchr.PluginInfo {
return launchr.PluginInfo{}
}
// OnAppInit implements launchr.Plugin interface.
func (p *{{.StructName}}) OnAppInit(app launchr.App) error {
fs, err := yamlembed.UntarFsBytes(tarFsBytes)
if err == nil {
// @fixme
app.SetFS(fs)
}
return err
}
`
type pluginVars struct {
ID string
ActionsTarPath string
StructName string
}
// Generate implements launchr.GeneratePlugin interface.
func (p *Plugin) Generate(buildPath string, workDir string) (*launchr.PluginGeneratedData, error) {
// Generate actions tar.
fmt.Println("[INFO] Discovering actions")
tarName, actions, err := createActionTar(os.DirFS(workDir), buildPath)
if err != nil {
return nil, err
}
fmt.Println("[INFO] Discovered:")
for i, a := range actions {
fmt.Printf("%d. %s\n", i+1, a.ID)
}
var id = "actions.yamldiscovery.gen"
tpl, err := template.New(id).Parse(pluginTemplate)
if err != nil {
return nil, err
}
fmt.Println("[INFO] Generating embed actions go file")
var buf bytes.Buffer
structName := "ActionsYamlDiscoveryGen"
err = tpl.Execute(&buf, &pluginVars{
ID: id,
StructName: structName,
ActionsTarPath: tarName,
})
if err != nil {
return nil, err
}
target := filepath.Join(buildPath, id+".go")
err = os.WriteFile(target, buf.Bytes(), 0600)
if err != nil {
return nil, err
}
return &launchr.PluginGeneratedData{
Plugins: []string{structName},
}, nil
}