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-09-23 20:02:57 +02:00
|
|
|
// Project path
|
|
|
|
Project string `yaml:"-"`
|
2021-05-19 04:15:17 +02:00
|
|
|
|
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
|
|
|
|
2021-11-05 16:21:21 +01:00
|
|
|
// Platform execution
|
|
|
|
Platform string `yaml:"platform,omitempty"`
|
2021-10-22 20:52:25 +02:00
|
|
|
|
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-09-23 20:02:57 +02:00
|
|
|
w := s.Project
|
2021-07-02 18:28:03 +02:00
|
|
|
// FIXME: backward compatibility
|
2021-10-13 23:32:06 +02:00
|
|
|
if planModule := s.Plan.Module; planModule != "" {
|
|
|
|
w = path.Join(w, planModule)
|
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`
|
2021-09-23 20:02:57 +02:00
|
|
|
// 2) For backward compatibility: if the project was `dagger
|
2021-08-20 15:52:58 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-10-13 23:32:06 +02:00
|
|
|
var args []string
|
2021-08-20 15:52:58 +02:00
|
|
|
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-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
|
|
|
|
}
|