Don't instantiate a global shared Env in cmd/compute.go

Signed-off-by: Solomon Hykes <sh.github.6811@hykes.org>
This commit is contained in:
Solomon Hykes 2021-02-04 22:28:35 +00:00
parent 0fe2ef4fe4
commit 0202f4447b
4 changed files with 17 additions and 21 deletions

View File

@ -13,7 +13,9 @@ import (
) )
var ( var (
env *dagger.Env // FIXME: global shared cue compiler is a workaround to limitation in the cue API
// This can be made cleaner by moving InputValue (or equivalent) under Env.
cc = &dagger.Compiler{}
input *dagger.InputValue input *dagger.InputValue
updater *dagger.InputValue updater *dagger.InputValue
) )
@ -33,6 +35,10 @@ var computeCmd = &cobra.Command{
lg := logger.New() lg := logger.New()
ctx := lg.WithContext(appcontext.Context()) ctx := lg.WithContext(appcontext.Context())
env, err := dagger.NewEnv(cc)
if err != nil {
lg.Fatal().Err(err).Msg("unable to initialize environment")
}
if err := updater.SourceFlag().Set(args[0]); err != nil { if err := updater.SourceFlag().Set(args[0]); err != nil {
lg.Fatal().Err(err).Msg("invalid local source") lg.Fatal().Err(err).Msg("invalid local source")
} }
@ -60,21 +66,9 @@ var computeCmd = &cobra.Command{
} }
func init() { func init() {
// Why is this stuff here?
// 1. input must be global for flag parsing
// 2. updater must be global for flag parsing
// 3. env must have same compiler as input & updater,
// therefore it must be global too.
//
// FIXME: roll up InputValue into Env?
var err error var err error
env, err = dagger.NewEnv()
if err != nil {
panic(err)
}
// Setup --input-* flags // Setup --input-* flags
input, err = dagger.NewInputValue(env.Compiler(), "{}") input, err = dagger.NewInputValue(cc, "{}")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -84,7 +78,7 @@ func init() {
computeCmd.Flags().Var(input.CueFlag(), "input-cue", "CUE") computeCmd.Flags().Var(input.CueFlag(), "input-cue", "CUE")
// Setup (future) --from-* flags // Setup (future) --from-* flags
updater, err = dagger.NewInputValue(env.Compiler(), "[...{do:string, ...}]") updater, err = dagger.NewInputValue(cc, "[...{do:string, ...}]")
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -68,8 +68,10 @@ func (env *Env) SetUpdater(u interface{}) error {
return nil return nil
} }
func NewEnv() (*Env, error) { func NewEnv(cc *Compiler) (*Env, error) {
cc := &Compiler{} if cc == nil {
cc = &Compiler{}
}
empty, err := cc.EmptyStruct() empty, err := cc.EmptyStruct()
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -6,7 +6,7 @@ import (
) )
func TestSimpleEnvSet(t *testing.T) { func TestSimpleEnvSet(t *testing.T) {
env, err := NewEnv() env, err := NewEnv(nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -23,7 +23,7 @@ func TestSimpleEnvSet(t *testing.T) {
} }
func TestSimpleEnvSetFromInputValue(t *testing.T) { func TestSimpleEnvSetFromInputValue(t *testing.T) {
env, err := NewEnv() env, err := NewEnv(nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -45,7 +45,7 @@ func TestSimpleEnvSetFromInputValue(t *testing.T) {
} }
func TestEnvInputComponent(t *testing.T) { func TestEnvInputComponent(t *testing.T) {
env, err := NewEnv() env, err := NewEnv(nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -6,7 +6,7 @@ import (
) )
func TestEnvInputFlag(t *testing.T) { func TestEnvInputFlag(t *testing.T) {
env, err := NewEnv() env, err := NewEnv(nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }