Update client to use cleanup helper

This commit is contained in:
Chris Roberts 2022-06-20 12:54:00 -07:00
parent 21b17e1907
commit c698980afd

View File

@ -16,6 +16,7 @@ import (
vconfig "github.com/hashicorp/vagrant-plugin-sdk/config" vconfig "github.com/hashicorp/vagrant-plugin-sdk/config"
"github.com/hashicorp/vagrant-plugin-sdk/helper/path" "github.com/hashicorp/vagrant-plugin-sdk/helper/path"
"github.com/hashicorp/vagrant-plugin-sdk/helper/paths" "github.com/hashicorp/vagrant-plugin-sdk/helper/paths"
"github.com/hashicorp/vagrant-plugin-sdk/internal-shared/cleanup"
"github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk" "github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk"
"github.com/hashicorp/vagrant-plugin-sdk/terminal" "github.com/hashicorp/vagrant-plugin-sdk/terminal"
"github.com/hashicorp/vagrant/internal/config" "github.com/hashicorp/vagrant/internal/config"
@ -30,7 +31,7 @@ var (
type Client struct { type Client struct {
config *config.Config config *config.Config
cleanupFns []func() error cleanup cleanup.Cleanup
client *serverclient.VagrantClient client *serverclient.VagrantClient
ctx context.Context ctx context.Context
localRunner bool localRunner bool
@ -44,6 +45,7 @@ type Client struct {
func New(ctx context.Context, opts ...Option) (c *Client, err error) { func New(ctx context.Context, opts ...Option) (c *Client, err error) {
c = &Client{ c = &Client{
cleanup: cleanup.New(),
ctx: ctx, ctx: ctx,
logger: hclog.L().Named("vagrant.client"), logger: hclog.L().Named("vagrant.client"),
runnerRef: &vagrant_server.Ref_Runner{ runnerRef: &vagrant_server.Ref_Runner{
@ -184,17 +186,11 @@ func (c *Client) LoadBasis(n string) (*Basis, error) {
// Close the client and call any cleanup functions // Close the client and call any cleanup functions
// that have been defined // that have been defined
func (c *Client) Close() (err error) { func (c *Client) Close() (err error) {
for _, f := range c.cleanupFns { return c.cleanup.Close()
if e := f(); e != nil {
err = multierror.Append(err, e)
}
} }
return func (c *Client) Cleanup(fn cleanup.CleanupFn) {
} c.cleanup.Do(fn)
func (c *Client) Cleanup(f ...func() error) {
c.cleanupFns = append(c.cleanupFns, f...)
} }
func (c *Client) UI() terminal.UI { func (c *Client) UI() terminal.UI {