Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
73 lines (64 loc) · 2 KB
/
Copy pathmain.go
File metadata and controls
73 lines (64 loc) · 2 KB
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
// Manage Github Actions configurations with Dagger
//
// Daggerizing your CI makes your YAML configurations smaller, but they still exist,
// and they're still a pain to maintain by hand.
//
// This module aims to finish the job, by letting you generate your remaining
// YAML configuration from a Dagger pipeline, written in your favorite language.
package main
import "github.com/dagger/dagger/modules/gha/internal/dagger"
type Gha struct {
Workflows []*Workflow
JobDefaults *Job // +private
WorkflowDefaults *Workflow // +private
}
func New(
jobDefaults *Job, // +optional
workflowDefaults *Workflow, // +optional
) *Gha {
return &Gha{
JobDefaults: jobDefaults,
WorkflowDefaults: workflowDefaults,
}
}
func (gha *Gha) Generate(
// +optional
directory *dagger.Directory,
// +optional
asJSON bool,
// +optional
// +default=".gen.yml"
fileExtension string,
) *dagger.Directory {
if directory == nil {
directory = dag.Directory()
}
directory = directory.With(deleteOldFiles(fileExtension))
for _, p := range gha.Workflows {
directory = directory.WithDirectory(".", p.config(asJSON, fileExtension))
}
directory = directory.With(gitAttributes(fileExtension))
return directory
}
func gitAttributes(fileExtension string) func(*dagger.Directory) *dagger.Directory {
// Need a custom file extension to match generated files in .gitattributes
if ext := fileExtension; ext == ".yml" || ext == ".yaml" {
return nil
}
return func(d *dagger.Directory) *dagger.Directory {
return d.WithNewFile(
".github/workflows/.gitattributes",
"*"+fileExtension+" linguist-generated")
}
}
func deleteOldFiles(fileExtension string) func(*dagger.Directory) *dagger.Directory {
// Need a custom file extension to delete old files
if ext := fileExtension; ext == ".yml" || ext == ".yaml" {
return nil
}
return func(d *dagger.Directory) *dagger.Directory {
return dag.Directory().WithDirectory("", d, dagger.DirectoryWithDirectoryOpts{
Exclude: []string{".github/workflows/*" + fileExtension},
})
}
}