vaguerent/internal/cli/base_init.go
Chris Roberts e958c6183a Adds initial HCP config support
Adds initial basic support for HCP based configuration in vagrant-go.
The initalization process has been updated to remove Vagrantfile parsing
from the client, moving it to the runner using init jobs for the basis
and the project (if there is one). Detection is done on the file based
on extension for Ruby based parsing or HCP based parsing.

Current HCP parsing is extremely simple and currently just a base to
build off. Config components will be able to implement an `Init`
function to handle receiving configuration data from a non-native source
file. This will be extended to include a default approach for injecting
defined data in the future.

Some cleanup was done in the state around validations. Some logging
adjustments were applied on the Ruby side for better behavior
consistency.

VirtualBox provider now caches locale detection to prevent multiple
checks every time the driver is initialized.
2023-09-07 17:26:10 -07:00

71 lines
1.9 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package cli
import (
"fmt"
"path/filepath"
"github.com/hashicorp/vagrant-plugin-sdk/helper/path"
clientpkg "github.com/hashicorp/vagrant/internal/client"
configpkg "github.com/hashicorp/vagrant/internal/config"
)
// This file contains the various methods that are used to perform
// the Init call on baseCommand. They are broken down into individual
// smaller methods for readability but more importantly to power the
// "init" subcommand. This allows us to share as much logic as possible
// between Init and "init" to help ensure that "init" succeeding means that
// other commands will succeed as well.
// initConfig initializes the configuration.
func (c *baseCommand) initConfig(optional bool) (*configpkg.Config, error) {
path, err := c.initConfigPath()
if err != nil {
if optional {
return nil, nil
}
return nil, err
}
return c.initConfigLoad(path)
}
// initConfigPath returns the configuration path to load.
func (c *baseCommand) initConfigPath() (string, error) {
// This configuarion is for the Vagrant process, not the same as a Vagrantfile
path, err := configpkg.FindPath(path.NewPath(c.basis.GetPath()), "vagrant-config.hcl")
if err != nil {
return "", fmt.Errorf("error looking for a Vagrant configuration: %s", err)
}
return path.String(), nil
}
// initConfigLoad loads the configuration at the given path.
func (c *baseCommand) initConfigLoad(path string) (*configpkg.Config, error) {
cfg, err := configpkg.Load(path, filepath.Dir(path))
if err != nil {
return nil, err
}
// Validate
if err := cfg.Validate(); err != nil {
return nil, err
}
return cfg, nil
}
// initClient initializes the client.
func (c *baseCommand) initClient() (*clientpkg.Client, error) {
// Start building our client options
opts := []clientpkg.Option{
clientpkg.WithConfig(c.cfg),
}
// Create our client
return clientpkg.New(c.Ctx, opts...)
}