Create basis from factory and update runner to use factory
This commit is contained in:
parent
8f4bb571a5
commit
32710b5d79
91
internal/core/factory.go
Normal file
91
internal/core/factory.go
Normal file
@ -0,0 +1,91 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"github.com/hashicorp/go-hclog"
|
||||
"github.com/hashicorp/vagrant-plugin-sdk/terminal"
|
||||
"github.com/hashicorp/vagrant/internal/plugin"
|
||||
"github.com/hashicorp/vagrant/internal/serverclient"
|
||||
)
|
||||
|
||||
type Factory struct {
|
||||
ctx context.Context
|
||||
client *serverclient.VagrantClient
|
||||
logger hclog.Logger
|
||||
m sync.Mutex
|
||||
plugins *plugin.Manager
|
||||
registered map[string]*Basis
|
||||
ui terminal.UI
|
||||
}
|
||||
|
||||
func NewFactory(
|
||||
ctx context.Context,
|
||||
client *serverclient.VagrantClient,
|
||||
logger hclog.Logger,
|
||||
plugins *plugin.Manager,
|
||||
ui terminal.UI,
|
||||
) *Factory {
|
||||
return &Factory{
|
||||
ctx: ctx,
|
||||
client: client,
|
||||
logger: logger,
|
||||
plugins: plugins,
|
||||
ui: ui,
|
||||
registered: map[string]*Basis{},
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Factory) New(name string, opts ...BasisOption) (*Basis, error) {
|
||||
f.m.Lock()
|
||||
defer f.m.Unlock()
|
||||
|
||||
// If we have a name, check if it's registered and return
|
||||
// the existing basis if available
|
||||
if name != "" {
|
||||
if b, ok := f.registered[name]; ok {
|
||||
return b, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Update the options to include this factory and
|
||||
// our settings when creating the new basis
|
||||
opts = append(opts,
|
||||
WithFactory(f),
|
||||
FromBasis(
|
||||
&Basis{
|
||||
ctx: f.ctx,
|
||||
client: f.client,
|
||||
logger: f.logger,
|
||||
plugins: f.plugins,
|
||||
ui: f.ui,
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
b, err := NewBasis(f.ctx, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Now there's a chance we already have this basis
|
||||
// registered if the name was not provided for
|
||||
// an initial lookup. If it is registered, close
|
||||
// this new basis, discard, and return the
|
||||
// registered one
|
||||
if existingB, ok := f.registered[b.Name()]; ok {
|
||||
b.Close()
|
||||
return existingB, nil
|
||||
}
|
||||
|
||||
f.registered[b.Name()] = b
|
||||
b.Closer(func() error {
|
||||
f.m.Lock()
|
||||
defer f.m.Unlock()
|
||||
delete(f.registered, b.Name())
|
||||
return nil
|
||||
})
|
||||
|
||||
return b, nil
|
||||
}
|
||||
@ -84,7 +84,7 @@ func (r *Runner) executeJob(
|
||||
opts = append(opts, core.WithBasisRef(ref))
|
||||
|
||||
// Load our basis
|
||||
b, err := core.NewBasis(ctx, opts...)
|
||||
b, err := r.factory.New("", opts...)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@ -13,6 +13,7 @@ import (
|
||||
|
||||
"github.com/hashicorp/vagrant-plugin-sdk/terminal"
|
||||
intcfg "github.com/hashicorp/vagrant/internal/config"
|
||||
"github.com/hashicorp/vagrant/internal/core"
|
||||
"github.com/hashicorp/vagrant/internal/plugin"
|
||||
|
||||
"github.com/hashicorp/vagrant/internal/server"
|
||||
@ -44,6 +45,7 @@ var ErrClosed = errors.New("runner is closed")
|
||||
//
|
||||
type Runner struct {
|
||||
id string
|
||||
factory *core.Factory
|
||||
logger hclog.Logger
|
||||
client *serverclient.VagrantClient
|
||||
vagrantRubyRuntime plg.ClientProtocol
|
||||
@ -116,7 +118,8 @@ func New(opts ...Option) (*Runner, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := runner.plugins.LoadLegacyPlugins(runner.vagrantRubyClient, runner.vagrantRubyRuntime); err != nil {
|
||||
if err := runner.plugins.LoadLegacyPlugins(
|
||||
runner.vagrantRubyClient, runner.vagrantRubyRuntime); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@ -132,6 +135,15 @@ func New(opts ...Option) (*Runner, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Add a core factory
|
||||
runner.factory = core.NewFactory(
|
||||
runner.ctx,
|
||||
runner.client,
|
||||
runner.logger,
|
||||
runner.plugins,
|
||||
runner.ui,
|
||||
)
|
||||
|
||||
return runner, nil
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user