From f0a7d87f315eba94d6ee7d5b4e93ccddff96bc18 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Mon, 27 Jun 2022 18:10:40 -0700 Subject: [PATCH] Only load target with vagrantfile if config exists --- internal/core/vagrantfile.go | 44 ++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/internal/core/vagrantfile.go b/internal/core/vagrantfile.go index bcc93da3f..40a227a48 100644 --- a/internal/core/vagrantfile.go +++ b/internal/core/vagrantfile.go @@ -452,14 +452,15 @@ func (v *Vagrantfile) Target( return } - // Convert to actual Vagrantfile for target setup - vf := conf.(*Vagrantfile) - - target, err = v.origin.LoadTarget( - WithTargetName(name), - WithTargetVagrantfile(vf), - ) + opts := []TargetOption{WithTargetName(name)} + var vf *Vagrantfile + if conf != nil { + // Convert to actual Vagrantfile for target setup + vf = conf.(*Vagrantfile) + opts = append(opts, WithTargetVagrantfile(vf)) + } + target, err = v.origin.LoadTarget(opts...) if err != nil { return } @@ -467,21 +468,24 @@ func (v *Vagrantfile) Target( // Since the target config gives us a Vagrantfile which is // attached to the project, we need to clone it and attach // it to the target we loaded - rawTarget := target.(*Target) - tvf := v.clone(name, rawTarget) - if err = tvf.Init(); err != nil { - return nil, err - } - rawTarget.vagrantfile = tvf + if vf != nil { + rawTarget := target.(*Target) + tvf := v.clone(name, rawTarget) + if err = tvf.Init(); err != nil { + return nil, err + } + rawTarget.vagrantfile = tvf - if err = vf.Close(); err != nil { - return nil, err + if err = vf.Close(); err != nil { + return nil, err + } } return } // Generate a new Vagrantfile for the given target +// NOTE: This function may return a nil result without an error // TODO(spox): Provider validation is not currently implemented func (v *Vagrantfile) TargetConfig( name, // name of the target @@ -498,11 +502,11 @@ func (v *Vagrantfile) TargetConfig( subvm, err := v.GetValue("vm", "__defined_vms", name) if err != nil { - v.logger.Error("failed to get subvm", + v.logger.Warn("failed to get subvm", "name", name, "error", err, ) - return nil, err + return nil, nil } if subvm == nil { @@ -595,6 +599,11 @@ func (v *Vagrantfile) GetValue( // Since we already used out first path value above // be sure we start our loop from 1 for i := 1; i < len(path); i++ { + v.logger.Warn("loop value lookup", + "iteration", i, + "current-value", result, + "current-key", path[i], + ) // First, attempt to use the current value as a stringed map if sm, ok := result.(map[string]interface{}); ok { if result, ok = sm[path[i]]; ok { @@ -603,6 +612,7 @@ func (v *Vagrantfile) GetValue( v.logger.Warn("get value lookup failed", "keys", path, "current-key", path[i], + "current-value", sm, "type", "map[string]interface{}", ) return nil, err