1
// This Source Code Form is subject to the terms of the Mozilla Public
2
// License, v. 2.0. If a copy of the MPL was not distributed with this
3
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
12
"github.com/siderolabs/talos/pkg/machinery/constants"
15
// Path represents a path to a configuration file.
17
// Path is the filesystem path of the config.
19
// WriteAllowed is true if the path is allowed to be written.
23
// GetTalosDirectory returns path to Talos directory (~/.talos).
24
func GetTalosDirectory() (string, error) {
25
home, err := os.UserHomeDir()
30
return filepath.Join(home, constants.TalosDir), nil
33
// GetDefaultPaths returns the list of config file paths in order of priority.
34
func GetDefaultPaths() ([]Path, error) {
35
talosDir, err := GetTalosDirectory()
40
result := make([]Path, 0, 3)
42
if path, ok := os.LookupEnv(constants.TalosConfigEnvVar); ok {
43
result = append(result, Path{
52
Path: filepath.Join(talosDir, constants.TalosconfigFilename),
56
Path: filepath.Join(constants.ServiceAccountMountPath, constants.TalosconfigFilename),
64
// firstValidPath iterates over the default paths and returns the first one that exists and readable.
65
// If no path is found, it will ensure that the first path that allows writes is created and returned.
66
// If no path is found that is writable, an error is returned.
67
func firstValidPath() (Path, error) {
68
paths, err := GetDefaultPaths()
73
var firstWriteAllowed Path
75
for _, path := range paths {
76
_, err = os.Stat(path.Path)
81
if firstWriteAllowed.Path == "" && path.WriteAllowed {
82
firstWriteAllowed = path
86
if firstWriteAllowed.Path == "" {
87
return Path{}, errors.New("no valid config paths found")
90
err = ensure(firstWriteAllowed.Path)
95
return firstWriteAllowed, nil