/
project.go
165 lines (134 loc) · 3.25 KB
/
project.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package engine
import (
"context"
"os"
"path"
"path/filepath"
"strings"
"sync"
"github.com/k0sproject/rig"
"github.com/octohelm/cuekit/pkg/cuecontext"
"github.com/octohelm/piper/cuepkg"
"github.com/octohelm/piper/pkg/cueflow"
"github.com/octohelm/piper/pkg/engine/task"
"github.com/octohelm/piper/pkg/wd"
_ "github.com/octohelm/piper/pkg/engine/task/archive"
_ "github.com/octohelm/piper/pkg/engine/task/client"
_ "github.com/octohelm/piper/pkg/engine/task/container"
_ "github.com/octohelm/piper/pkg/engine/task/encoding"
_ "github.com/octohelm/piper/pkg/engine/task/exec"
_ "github.com/octohelm/piper/pkg/engine/task/file"
_ "github.com/octohelm/piper/pkg/engine/task/http"
_ "github.com/octohelm/piper/pkg/engine/task/wd"
)
func init() {
if err := cuepkg.RegisterAsMemModule(); err != nil {
panic(err)
}
}
type Project interface {
Run(ctx context.Context, action ...string) error
}
type option struct {
cwd string
entry string
cacheDir string
}
type OptFunc = func(o *option)
func WithProject(root string) OptFunc {
return func(o *option) {
o.entry = root
}
}
func WithCacheDir(root string) OptFunc {
return func(o *option) {
o.cacheDir = root
}
}
func New(ctx context.Context, opts ...OptFunc) (Project, error) {
c := &project{}
cacheDir, err := os.UserCacheDir()
if err != nil {
return nil, err
}
c.opt.cacheDir = filepath.Join(cacheDir, "piper")
for i := range opts {
opts[i](&c.opt)
}
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
c.opt.cwd = cwd
c.client, err = fromOpt(cwd, &c.opt)
if err != nil {
return nil, err
}
return c, nil
}
type project struct {
opt option
*client
}
func (p *project) Run(ctx context.Context, action ...string) error {
runner := cueflow.NewRunner(func() (cueflow.Value, error) {
buildConfig, err := cuecontext.NewConfig(cuecontext.WithRoot(p.opt.cwd))
if err != nil {
return nil, err
}
val, err := cuecontext.Build(buildConfig, p.opt.entry)
if err != nil {
return nil, err
}
return cueflow.WrapValue(val), nil
})
ctx = cueflow.TaskRunnerFactoryContext.Inject(ctx, task.Factory)
ctx = task.ClientContext.Inject(ctx, p)
return runner.Run(ctx, action)
}
func fromOpt(cwd string, opt *option) (*client, error) {
return &client{
cacheDir: sync.OnceValues(func() (wd.WorkDir, error) {
cacheDir := opt.cacheDir
if strings.HasPrefix(cacheDir, "~") {
cacheDir = path.Join(os.Getenv("HOME"), "."+cacheDir[1:])
}
return wd.Wrap(
&rig.Connection{
Localhost: &rig.Localhost{
Enabled: true,
},
},
wd.WithDir(cacheDir),
)
}),
sourceDir: sync.OnceValues(func() (wd.WorkDir, error) {
root := path.Join(cwd, opt.entry)
if strings.HasSuffix(root, ".cue") {
root = path.Dir(root)
}
return wd.Wrap(
&rig.Connection{
Localhost: &rig.Localhost{
Enabled: true,
},
},
wd.WithDir(root),
)
}),
}, nil
}
type client struct {
sourceDir func() (wd.WorkDir, error)
cacheDir func() (wd.WorkDir, error)
}
func (c *client) SourceDir(ctx context.Context) (wd.WorkDir, error) {
return c.sourceDir()
}
func (c *client) CacheDir(ctx context.Context, typ string) (wd.WorkDir, error) {
w, err := c.cacheDir()
if err != nil {
return nil, err
}
return wd.With(w, wd.WithDir(typ))
}