File: base_init.go

package info (click to toggle)
vagrant 2.3.7%2Bgit20230731.5fc64cde%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 17,616 kB
  • sloc: ruby: 111,820; sh: 462; makefile: 123; ansic: 34; lisp: 1
file content (66 lines) | stat: -rw-r--r-- 1,817 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package cli

import (
	"fmt"
	"path/filepath"

	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(c.basis.Path(), "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...)
}