universe vendoring
Rather than injecting universe at runtime, this change will vendor alpha.dagger.io in `cue.mod` directly. Fixes #700 Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path"
|
||||
)
|
||||
|
||||
// Contents of an environment serialized to a file
|
||||
type State struct {
|
||||
// State path
|
||||
@@ -28,11 +33,26 @@ func (s *State) Source() Input {
|
||||
w := s.Workspace
|
||||
// FIXME: backward compatibility
|
||||
if mod := s.Plan.Module; mod != "" {
|
||||
w = mod
|
||||
w = path.Join(w, mod)
|
||||
}
|
||||
return DirInput(w, []string{}, []string{})
|
||||
}
|
||||
|
||||
// VendorUniverse vendors the latest (built-in) version of the universe into the
|
||||
// environment's `cue.mod`.
|
||||
// FIXME: This has nothing to do in `State` and should be tied to a `Workspace`.
|
||||
// However, since environments could point to different modules before, we have
|
||||
// to handle vendoring on a per environment basis.
|
||||
func (s *State) VendorUniverse(ctx context.Context) error {
|
||||
w := s.Workspace
|
||||
// FIXME: backward compatibility
|
||||
if mod := s.Plan.Module; mod != "" {
|
||||
w = path.Join(w, mod)
|
||||
}
|
||||
|
||||
return vendorUniverse(ctx, w)
|
||||
}
|
||||
|
||||
type Plan struct {
|
||||
Module string `yaml:"module,omitempty"`
|
||||
Package string `yaml:"package,omitempty"`
|
||||
|
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"go.dagger.io/dagger/keychain"
|
||||
"go.dagger.io/dagger/stdlib"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
@@ -51,6 +52,11 @@ func Init(ctx context.Context, dir string) (*Workspace, error) {
|
||||
if err := os.Mkdir(path.Join(daggerRoot, envDir), 0755); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := vendorUniverse(ctx, root); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Workspace{
|
||||
Path: root,
|
||||
}, nil
|
||||
@@ -335,3 +341,63 @@ func (w *Workspace) cleanPackageName(ctx context.Context, pkg string) (string, e
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func cueModInit(ctx context.Context, p string) error {
|
||||
lg := log.Ctx(ctx)
|
||||
|
||||
mod := path.Join(p, "cue.mod")
|
||||
if err := os.Mkdir(mod, 0755); err != nil {
|
||||
if !errors.Is(err, os.ErrExist) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
modFile := path.Join(mod, "module.cue")
|
||||
if _, err := os.Stat(modFile); err != nil {
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
|
||||
lg.Debug().Str("mod", p).Msg("initializing cue.mod")
|
||||
|
||||
if err := os.WriteFile(modFile, []byte("module: \"\"\n"), 0600); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := os.Mkdir(path.Join(mod, "usr"), 0755); err != nil {
|
||||
if !errors.Is(err, os.ErrExist) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := os.Mkdir(path.Join(mod, "pkg"), 0755); err != nil {
|
||||
if !errors.Is(err, os.ErrExist) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func vendorUniverse(ctx context.Context, p string) error {
|
||||
// ensure cue module is initialized
|
||||
if err := cueModInit(ctx, p); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// add universe to `.gitignore`
|
||||
if err := os.WriteFile(
|
||||
path.Join(p, "cue.mod", "pkg", ".gitignore"),
|
||||
[]byte(fmt.Sprintf("# dagger universe\n%s\n", stdlib.PackageName)),
|
||||
0600,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Ctx(ctx).Debug().Str("mod", p).Msg("vendoring universe")
|
||||
if err := stdlib.Vendor(ctx, p); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user