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-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
|
|
|
|
func (s *State) Source() Input {
|
|
|
|
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
|
|
|
}
|
|
|
|
return DirInput(w, []string{}, []string{})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|