This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
dagger/state/state.go
Andrea Luzzardi a61e8dcb62 prepare the transition to #Plan.context
This change helps the transition between `dagger input` and `#Plan.context`.

In summary, the codebase now relies on a *context* for execution with mapping to *IDs*.
In the future, *context* will come from a `#Plan.context`.
In the meantime, a bridge converts `dagger input` to a plan context. This allows both *old* and *new* style configurations to co-exist with the same underlying engine.

- Implement `plancontext`. Context holds the execution context for a plan. Currently this includes the platform, local directories, secrets and services (e.g. unix/npipe).
- Contextual data can be registered at any point. In the future, this will be done by `#Plan.context`
- Migrated the `dagger input` codebase to register inputs in a `plancontext`
- Migrated low-level types/operations to the *Context ID* pattern.
  - `dagger.#Stream` now only includes an `id` (instead of `unix` path)
  - `dagger.#Secret` still includes only an ID, but now it's based off `plancontext`
  - `op.#Local` now only includes an `id` (instead of `path`, `include`, `exclude`.

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
2021-11-19 11:29:38 -08:00

112 lines
2.5 KiB
Go

package state
import (
"context"
"path"
"cuelang.org/go/cue"
"go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/plancontext"
)
// Contents of an environment serialized to a file
type State struct {
// Plan Context.
// FIXME: this is used as a bridge and is temporary.
Context *plancontext.Context `yaml:"-"`
// State path
Path string `yaml:"-"`
// Project path
Project string `yaml:"-"`
// Plan
Plan Plan `yaml:"plan,omitempty"`
// Human-friendly environment name.
// A environment may have more than one name.
// FIXME: store multiple names?
Name string `yaml:"name,omitempty"`
// Platform execution
Platform string `yaml:"platform,omitempty"`
// User Inputs
Inputs map[string]Input `yaml:"inputs,omitempty"`
// Computed values
Computed string `yaml:"-"`
}
// Cue module containing the environment plan
func (s *State) CompilePlan(ctx context.Context) (*compiler.Value, error) {
w := s.Project
// FIXME: backward compatibility
if planModule := s.Plan.Module; planModule != "" {
w = path.Join(w, planModule)
}
// 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 project 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
}
var 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(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
}
type Plan struct {
Module string `yaml:"module,omitempty"`
Package string `yaml:"package,omitempty"`
}
func (s *State) SetInput(key string, value Input) error {
if s.Inputs == nil {
s.Inputs = make(map[string]Input)
}
s.Inputs[key] = value
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.
func (s *State) RemoveInputs(key string) error {
delete(s.Inputs, key)
return nil
}