HasCapability working

This commit is contained in:
sophia 2021-06-07 12:06:43 -05:00 committed by Paul Hinze
parent 6805d81301
commit e4c0f09925
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
6 changed files with 41 additions and 17 deletions

View File

@ -33,4 +33,5 @@ func (h *AlwaysTrueHost) InitializeCapabilities() (err error) {
var (
_ component.Host = (*AlwaysTrueHost)(nil)
_ sdkcore.Host = (*AlwaysTrueHost)(nil)
)

View File

@ -7,6 +7,7 @@ import (
"github.com/hashicorp/go-argmapper"
"github.com/hashicorp/vagrant-plugin-sdk/component"
plugincore "github.com/hashicorp/vagrant-plugin-sdk/core"
// "github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk"
"github.com/hashicorp/vagrant-plugin-sdk/terminal"
//"google.golang.org/protobuf/types/known/anypb"
@ -158,8 +159,8 @@ func (c *Command) ExecuteOfni(trm terminal.UI) int64 {
func (c *Command) ExecuteUseHostPlugin(trm terminal.UI, host plugincore.Host) int64 {
trm.Output("I'm going to use a the host plugin to do something!")
host.HasCapability("write_hello")
if ok, _ := host.HasCapability("write_hello"); ok {
ok := host.HasCapability("write_hello")
if ok {
trm.Output("Writing to file using `write_hello` capability")
host.Capability("write_hello", argmapper.Typed(trm))
} else {

View File

@ -384,6 +384,31 @@ func (b *Basis) Run(ctx context.Context, task *vagrant_server.Task) (err error)
return
}
func (b *Basis) findHostPlugin(ctx context.Context) (*Component, error) {
f := b.factories[component.HostType]
for _, name := range f.Registered() {
if name != "myplugin" {
continue
}
h, err := componentCreatorMap[component.HostType].Create(ctx, b, name)
if err != nil {
return nil, err
}
detected, err := b.callDynamicFunc(
ctx,
b.logger,
(interface{})(nil),
h,
h.Value.(component.Host).DetectFunc(),
)
if detected != nil && detected.(bool) {
return h, nil
}
// h.Close()
}
return nil, errors.New("host plugin not found")
}
func (b *Basis) component(ctx context.Context, typ component.Type, name string) (*Component, error) {
// If this is a command type component, the plugin is registered
// as only the root command

View File

@ -94,9 +94,3 @@ func FlagsToProtoMapper(input []*option.Option) []*vagrant_server.Job_Flag {
}
return output
}
func HostComponentToProtoMapper(input Component) *vagrant_plugin_sdk.Args_Host {
return &vagrant_plugin_sdk.Args_Host{
ServerAddr: input.Info.ServerAddr,
}
}

View File

@ -97,8 +97,11 @@ func (p *Project) DefaultPrivateKey() (path string, err error) {
}
func (p *Project) Host() (host core.Host, err error) {
// TODO: implement
return
hostComponent, err := p.basis.findHostPlugin(p.ctx)
if err != nil {
return nil, err
}
return hostComponent.Value.(core.Host), nil
}
func (p *Project) MachineNames() (names []string, err error) {
@ -232,12 +235,7 @@ func (p *Project) Components(ctx context.Context) (results []*Component, err err
func (p *Project) Run(ctx context.Context, task *vagrant_server.Task) (err error) {
p.logger.Debug("running new task", "project", p, "task", task)
hostPluginName, err := p.findHostPlugin(ctx)
if hostPluginName != "" {
}
cmd, err := p.basis.component(ctx, component.CommandType, task.Component.Name)
hostPlugin, err := p.basis.component(ctx, component.HostType, hostPluginName)
if err != nil {
return err
@ -248,7 +246,6 @@ func (p *Project) Run(ctx context.Context, task *vagrant_server.Task) (err error
return
}
hostComponentToProtoMapper, _ := argmapper.NewFunc(HostComponentToProtoMapper)
result, err := p.callDynamicFunc(
ctx,
p.logger,

View File

@ -166,13 +166,19 @@ func (t *Target) Run(ctx context.Context, task *vagrant_server.Task) (err error)
return
}
host, _ := t.project.Host()
ok := host.HasCapability("write_hello")
if ok {
}
result, err := t.callDynamicFunc(
ctx,
t.logger,
(interface{})(nil),
cmd,
cmd.Value.(component.Command).ExecuteFunc(strings.Split(task.CommandName, " ")),
argmapper.Typed(task.CliArgs, t.jobInfo, t.dir),
argmapper.Typed(task.CliArgs, t.jobInfo, t.dir, host),
)
if err != nil || result == nil || result.(int64) != 0 {