2022-01-11 21:40:02 +01:00
|
|
|
package pkg
|
2021-02-17 02:54:43 +01:00
|
|
|
|
|
|
|
import (
|
2021-07-01 19:42:52 +02:00
|
|
|
"context"
|
2021-02-17 02:54:43 +01:00
|
|
|
"embed"
|
2022-01-12 01:36:23 +01:00
|
|
|
"errors"
|
2021-07-01 19:42:52 +02:00
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
2021-02-17 02:54:43 +01:00
|
|
|
"path"
|
2021-07-01 19:42:52 +02:00
|
|
|
"path/filepath"
|
2022-01-12 01:36:23 +01:00
|
|
|
|
|
|
|
"github.com/gofrs/flock"
|
|
|
|
"github.com/rs/zerolog/log"
|
2021-02-17 02:54:43 +01:00
|
|
|
)
|
|
|
|
|
2021-03-12 22:00:11 +01:00
|
|
|
var (
|
|
|
|
// FS contains the filesystem of the stdlib.
|
2022-01-12 01:36:23 +01:00
|
|
|
//go:embed alpha.dagger.io/**/*.cue alpha.dagger.io/**/*/*.cue dagger.io/**/*.cue dagger.io/**/*/*.cue
|
2021-03-12 22:00:11 +01:00
|
|
|
FS embed.FS
|
2022-01-12 01:36:23 +01:00
|
|
|
)
|
2021-02-17 02:54:43 +01:00
|
|
|
|
2022-01-12 01:36:23 +01:00
|
|
|
var (
|
2022-01-11 21:40:02 +01:00
|
|
|
AlphaModule = "alpha.dagger.io"
|
2022-01-12 01:36:23 +01:00
|
|
|
DaggerModule = "dagger.io"
|
|
|
|
EnginePackage = fmt.Sprintf("%s/dagger/engine", DaggerModule)
|
|
|
|
|
|
|
|
modules = []string{
|
|
|
|
AlphaModule,
|
|
|
|
DaggerModule,
|
|
|
|
}
|
|
|
|
|
|
|
|
lockFilePath = "dagger.lock"
|
2021-02-17 02:54:43 +01:00
|
|
|
)
|
2021-07-01 19:42:52 +02:00
|
|
|
|
2022-01-12 01:36:23 +01:00
|
|
|
func Vendor(ctx context.Context, p string) error {
|
|
|
|
if p == "" {
|
|
|
|
p = getCueModParent()
|
|
|
|
}
|
|
|
|
|
|
|
|
cuePkgDir := path.Join(p, "cue.mod", "pkg")
|
|
|
|
if err := os.MkdirAll(cuePkgDir, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock this function so no more than 1 process can run it at once.
|
|
|
|
lockFile := path.Join(cuePkgDir, lockFilePath)
|
|
|
|
l := flock.New(lockFile)
|
|
|
|
if err := l.Lock(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
l.Unlock()
|
|
|
|
os.Remove(lockFile)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// ensure cue module is initialized
|
|
|
|
if err := cueModInit(ctx, p); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate `.gitignore`
|
|
|
|
if err := os.WriteFile(
|
|
|
|
path.Join(cuePkgDir, ".gitignore"),
|
|
|
|
[]byte(fmt.Sprintf("# generated by dagger\n%s\n%s\ndagger.lock\n", AlphaModule, DaggerModule)),
|
|
|
|
0600,
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Ctx(ctx).Debug().Str("mod", p).Msg("vendoring packages")
|
|
|
|
|
|
|
|
// Unpack modules in a temporary directory
|
|
|
|
unpackDir, err := os.MkdirTemp(cuePkgDir, "vendor-*")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(unpackDir)
|
|
|
|
|
|
|
|
if err := extractModules(unpackDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, module := range modules {
|
|
|
|
// Semi-atomic swap of the module
|
|
|
|
//
|
|
|
|
// The following basically does:
|
|
|
|
// $ rm -rf cue.mod/pkg/MODULE.old
|
|
|
|
// $ mv cue.mod/pkg/MODULE cue.mod/pkg/MODULE.old
|
|
|
|
// $ mv VENDOR/MODULE cue.mod/pkg/MODULE
|
|
|
|
// $ rm -rf cue.mod/pkg/MODULE.old
|
|
|
|
|
|
|
|
moduleDir := path.Join(cuePkgDir, module)
|
|
|
|
backupModuleDir := moduleDir + ".old"
|
|
|
|
if err := os.RemoveAll(backupModuleDir); err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Rename(moduleDir, backupModuleDir); err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(backupModuleDir)
|
|
|
|
|
|
|
|
if err := os.Rename(path.Join(unpackDir, module), moduleDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractModules(dest string) error {
|
2021-07-01 19:42:52 +02:00
|
|
|
return fs.WalkDir(FS, ".", func(p string, entry fs.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !entry.Type().IsRegular() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if filepath.Ext(entry.Name()) != ".cue" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
contents, err := fs.ReadFile(FS, p)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %w", p, err)
|
|
|
|
}
|
|
|
|
|
2021-12-23 17:48:28 +01:00
|
|
|
overlayPath := path.Join(dest, p)
|
2021-07-01 19:42:52 +02:00
|
|
|
|
|
|
|
if err := os.MkdirAll(filepath.Dir(overlayPath), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.WriteFile(overlayPath, contents, 0600)
|
|
|
|
})
|
|
|
|
}
|
2022-01-12 01:36:23 +01:00
|
|
|
|
|
|
|
// getCueModParent traverses the directory tree up through ancestors looking for a cue.mod folder
|
|
|
|
func getCueModParent() string {
|
|
|
|
cwd, _ := os.Getwd()
|
|
|
|
parentDir := cwd
|
|
|
|
|
|
|
|
for {
|
|
|
|
if _, err := os.Stat(path.Join(parentDir, "cue.mod")); !errors.Is(err, os.ErrNotExist) {
|
|
|
|
break // found it!
|
|
|
|
}
|
|
|
|
|
|
|
|
parentDir = filepath.Dir(parentDir)
|
|
|
|
|
|
|
|
if parentDir == string(os.PathSeparator) {
|
|
|
|
// reached the root
|
|
|
|
parentDir = cwd // reset to working directory
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parentDir
|
|
|
|
}
|
|
|
|
|
|
|
|
func cueModInit(ctx context.Context, parentDir string) error {
|
|
|
|
lg := log.Ctx(ctx)
|
|
|
|
|
|
|
|
modDir := path.Join(parentDir, "cue.mod")
|
|
|
|
modFile := path.Join(modDir, "module.cue")
|
|
|
|
if _, err := os.Stat(modFile); err != nil {
|
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
lg.Debug().Str("mod", parentDir).Msg("initializing cue.mod")
|
|
|
|
|
|
|
|
if err := os.WriteFile(modFile, []byte("module: \"\"\n"), 0600); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Mkdir(path.Join(modDir, "usr"), 0755); err != nil {
|
|
|
|
if !errors.Is(err, os.ErrExist) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := os.Mkdir(path.Join(modDir, "pkg"), 0755); err != nil {
|
|
|
|
if !errors.Is(err, os.ErrExist) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|