use the workspace as the plan module
Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
@@ -9,7 +9,7 @@ type State struct {
|
||||
Workspace string `yaml:"-"`
|
||||
|
||||
// Plan
|
||||
Plan Plan `yaml:"plan"`
|
||||
Plan Plan `yaml:"plan,omitempty"`
|
||||
|
||||
// Human-friendly environment name.
|
||||
// A environment may have more than one name.
|
||||
@@ -23,17 +23,21 @@ type State struct {
|
||||
Computed string `yaml:"-"`
|
||||
}
|
||||
|
||||
// Cue module containing the environment plan
|
||||
func (s *State) Source() Input {
|
||||
w := s.Workspace
|
||||
// FIXME: backward compatibility
|
||||
if mod := s.Plan.Module; mod != "" {
|
||||
w = mod
|
||||
}
|
||||
return DirInput(w, []string{}, []string{})
|
||||
}
|
||||
|
||||
type Plan struct {
|
||||
Module string `yaml:"module,omitempty"`
|
||||
Package string `yaml:"package,omitempty"`
|
||||
}
|
||||
|
||||
// Cue module containing the environment plan
|
||||
// The input's top-level artifact is used as a module directory.
|
||||
func (p *Plan) Source() Input {
|
||||
return DirInput(p.Module, []string{}, []string{})
|
||||
}
|
||||
|
||||
func (s *State) SetInput(key string, value Input) error {
|
||||
if s.Inputs == nil {
|
||||
s.Inputs = make(map[string]Input)
|
||||
|
@@ -8,6 +8,7 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"go.dagger.io/dagger/keychain"
|
||||
@@ -161,18 +162,16 @@ func (w *Workspace) Get(ctx context.Context, name string) (*State, error) {
|
||||
return nil, err
|
||||
}
|
||||
st.Path = envPath
|
||||
// Backward compat: if no plan module has been provided,
|
||||
// use `.dagger/env/<name>/plan`
|
||||
// FIXME: Backward compat: Support for old-style `.dagger/env/<name>/plan`
|
||||
if st.Plan.Module == "" {
|
||||
planPath := path.Join(envPath, planDir)
|
||||
if _, err := os.Stat(planPath); err != nil {
|
||||
return nil, fmt.Errorf("missing plan information for %q", name)
|
||||
if _, err := os.Stat(planPath); err == nil {
|
||||
planRelPath, err := filepath.Rel(w.Path, planPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
st.Plan.Module = planRelPath
|
||||
}
|
||||
planRelPath, err := filepath.Rel(w.Path, planPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
st.Plan.Module = planRelPath
|
||||
}
|
||||
st.Workspace = w.Path
|
||||
|
||||
@@ -230,20 +229,22 @@ func (w *Workspace) Save(ctx context.Context, st *State) error {
|
||||
}
|
||||
|
||||
func (w *Workspace) Create(ctx context.Context, name string, plan Plan) (*State, error) {
|
||||
if _, err := w.Get(ctx, name); err == nil {
|
||||
return nil, ErrExist
|
||||
}
|
||||
|
||||
pkg, err := w.cleanPackageName(ctx, plan.Package)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
envPath, err := filepath.Abs(w.envPath(name))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, err := os.Stat(plan.Module); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Environment directory
|
||||
if err := os.MkdirAll(envPath, 0755); err != nil {
|
||||
if errors.Is(err, os.ErrExist) {
|
||||
return nil, ErrExist
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -252,8 +253,10 @@ func (w *Workspace) Create(ctx context.Context, name string, plan Plan) (*State,
|
||||
st := &State{
|
||||
Path: envPath,
|
||||
Workspace: w.Path,
|
||||
Plan: plan,
|
||||
Name: name,
|
||||
Plan: Plan{
|
||||
Package: pkg,
|
||||
},
|
||||
Name: name,
|
||||
}
|
||||
|
||||
data, err := yaml.Marshal(st)
|
||||
@@ -284,6 +287,51 @@ func (w *Workspace) Create(ctx context.Context, name string, plan Plan) (*State,
|
||||
return st, nil
|
||||
}
|
||||
|
||||
func (w *Workspace) DaggerDir() string {
|
||||
return path.Join(w.Path, daggerDir)
|
||||
func (w *Workspace) cleanPackageName(ctx context.Context, pkg string) (string, error) {
|
||||
lg := log.
|
||||
Ctx(ctx).
|
||||
With().
|
||||
Str("package", pkg).
|
||||
Logger()
|
||||
|
||||
if pkg == "" {
|
||||
return pkg, nil
|
||||
}
|
||||
|
||||
// If the package is not a path, then it must be a domain (e.g. foo.bar/mypackage)
|
||||
if _, err := os.Stat(pkg); err != nil {
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Make sure the domain is in the correct form
|
||||
if !strings.Contains(pkg, ".") || !strings.Contains(pkg, "/") {
|
||||
return "", fmt.Errorf("invalid package %q", pkg)
|
||||
}
|
||||
|
||||
return pkg, nil
|
||||
}
|
||||
|
||||
p, err := filepath.Abs(pkg)
|
||||
if err != nil {
|
||||
lg.Error().Err(err).Msg("unable to resolve path")
|
||||
return "", err
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(p, w.Path) {
|
||||
lg.Fatal().Err(err).Msg("package is outside the workspace")
|
||||
return "", err
|
||||
}
|
||||
|
||||
p, err = filepath.Rel(w.Path, p)
|
||||
if err != nil {
|
||||
lg.Fatal().Err(err).Msg("unable to resolve path")
|
||||
return "", err
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(p, ".") {
|
||||
p = "./" + p
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user