2021-05-07 23:45:15 +02:00
|
|
|
package state
|
2021-04-07 02:43:12 +02:00
|
|
|
|
2021-07-01 19:42:52 +02:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"path"
|
2021-08-20 15:52:58 +02:00
|
|
|
|
|
|
|
"cuelang.org/go/cue"
|
|
|
|
"go.dagger.io/dagger/compiler"
|
2021-07-01 19:42:52 +02:00
|
|
|
)
|
|
|
|
|
2021-04-27 20:59:04 +02:00
|
|
|
// Contents of an environment serialized to a file
|
2021-05-07 23:45:15 +02:00
|
|
|
type State struct {
|
|
|
|
// State path
|
|
|
|
Path string `yaml:"-"`
|
2021-04-07 02:43:12 +02:00
|
|
|
|
2021-05-19 04:15:17 +02:00
|
|
|
// Workspace path
|
|
|
|
Workspace string `yaml:"-"`
|
|
|
|
|
2021-06-15 18:49:57 +02:00
|
|
|
// Plan
|
2021-07-02 18:28:03 +02:00
|
|
|
Plan Plan `yaml:"plan,omitempty"`
|
2021-05-26 00:14:39 +02:00
|
|
|
|
2021-04-27 20:59:04 +02:00
|
|
|
// Human-friendly environment name.
|
|
|
|
// A environment may have more than one name.
|
2021-04-07 02:43:12 +02:00
|
|
|
// FIXME: store multiple names?
|
2021-05-07 23:45:15 +02:00
|
|
|
Name string `yaml:"name,omitempty"`
|
2021-04-07 02:43:12 +02:00
|
|
|
|
|
|
|
// User Inputs
|
2021-05-17 20:12:46 +02:00
|
|
|
Inputs map[string]Input `yaml:"inputs,omitempty"`
|
2021-04-07 02:43:12 +02:00
|
|
|
|
|
|
|
// Computed values
|
2021-05-07 23:45:15 +02:00
|
|
|
Computed string `yaml:"-"`
|
2021-04-07 02:43:12 +02:00
|
|
|
}
|
|
|
|
|
2021-07-02 18:28:03 +02:00
|
|
|
// Cue module containing the environment plan
|
2021-08-20 15:52:58 +02:00
|
|
|
func (s *State) CompilePlan(ctx context.Context) (*compiler.Value, error) {
|
2021-07-02 18:28:03 +02:00
|
|
|
w := s.Workspace
|
|
|
|
// FIXME: backward compatibility
|
|
|
|
if mod := s.Plan.Module; mod != "" {
|
2021-07-01 19:42:52 +02:00
|
|
|
w = path.Join(w, mod)
|
2021-07-02 18:28:03 +02:00
|
|
|
}
|
2021-08-20 15:52:58 +02:00
|
|
|
|
|
|
|
// FIXME: universe vendoring
|
|
|
|
// This is already done on `dagger init` and shouldn't be done here too.
|
|
|
|
// However:
|
|
|
|
// 1) As of right now, there's no way to update universe through the
|
|
|
|
// CLI, so we are lazily updating on `dagger up` using the embedded `universe`
|
|
|
|
// 2) For backward compatibility: if the workspace was `dagger
|
|
|
|
// init`-ed before we added support for vendoring universe, it might not
|
|
|
|
// contain a `cue.mod`.
|
|
|
|
if err := vendorUniverse(ctx, w); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if pkg := s.Plan.Package; pkg != "" {
|
|
|
|
args = append(args, pkg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return compiler.Build(w, nil, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) CompileInputs() (*compiler.Value, error) {
|
|
|
|
v := compiler.NewValue()
|
|
|
|
|
|
|
|
// Prepare inputs
|
|
|
|
for key, input := range s.Inputs {
|
|
|
|
i, err := input.Compile(key, s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if key == "" {
|
|
|
|
err = v.FillPath(cue.MakePath(), i)
|
|
|
|
} else {
|
|
|
|
err = v.FillPath(cue.ParsePath(key), i)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return v, nil
|
2021-07-02 18:28:03 +02:00
|
|
|
}
|
|
|
|
|
2021-07-01 19:42:52 +02:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2021-06-15 18:49:57 +02:00
|
|
|
type Plan struct {
|
|
|
|
Module string `yaml:"module,omitempty"`
|
|
|
|
Package string `yaml:"package,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-05-07 23:45:15 +02:00
|
|
|
func (s *State) SetInput(key string, value Input) error {
|
2021-05-17 20:12:46 +02:00
|
|
|
if s.Inputs == nil {
|
|
|
|
s.Inputs = make(map[string]Input)
|
2021-04-07 02:43:12 +02:00
|
|
|
}
|
2021-05-17 20:12:46 +02:00
|
|
|
s.Inputs[key] = value
|
2021-04-07 02:43:12 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all inputs at the given key, including sub-keys.
|
|
|
|
// For example RemoveInputs("foo.bar") will remove all inputs
|
|
|
|
// at foo.bar, foo.bar.baz, etc.
|
2021-05-07 23:45:15 +02:00
|
|
|
func (s *State) RemoveInputs(key string) error {
|
2021-05-17 20:12:46 +02:00
|
|
|
delete(s.Inputs, key)
|
2021-04-07 02:43:12 +02:00
|
|
|
return nil
|
|
|
|
}
|