forked from containers/common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules.go
95 lines (81 loc) · 2.39 KB
/
modules.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package config
import (
"fmt"
"path/filepath"
"github.com/containers/storage/pkg/fileutils"
"github.com/containers/storage/pkg/homedir"
"github.com/containers/storage/pkg/unshare"
"github.com/hashicorp/go-multierror"
)
// The subdirectory for looking up containers.conf modules.
const moduleSubdir = "containers/containers.conf.modules"
// Moving the base paths into variables allows for overriding them in units
// tests.
var (
moduleBaseEtc = "/etc/"
moduleBaseUsr = "/usr/share"
)
// LoadedModules returns absolute paths to loaded containers.conf modules.
func (c *Config) LoadedModules() []string {
// Required for conmon's callback to Podman's cleanup.
// Absolute paths make loading the modules a bit faster.
return c.loadedModules
}
// Find the specified modules in the options. Return an error if a specific
// module cannot be located on the host.
func (o *Options) modules() ([]string, error) {
if len(o.Modules) == 0 {
return nil, nil
}
dirs, err := ModuleDirectories()
if err != nil {
return nil, err
}
configs := make([]string, 0, len(o.Modules))
for _, path := range o.Modules {
resolved, err := resolveModule(path, dirs)
if err != nil {
return nil, fmt.Errorf("could not resolve module %q: %w", path, err)
}
configs = append(configs, resolved)
}
return configs, nil
}
// ModuleDirectories return the directories to load modules from:
// 1) XDG_CONFIG_HOME/HOME if rootless
// 2) /etc/
// 3) /usr/share
func ModuleDirectories() ([]string, error) { // Public API for shell completions in Podman
modules := []string{
filepath.Join(moduleBaseEtc, moduleSubdir),
filepath.Join(moduleBaseUsr, moduleSubdir),
}
if !unshare.IsRootless() {
return modules, nil
}
// Prepend the user modules dir.
configHome, err := homedir.GetConfigHome()
if err != nil {
return nil, err
}
return append([]string{filepath.Join(configHome, moduleSubdir)}, modules...), nil
}
// Resolve the specified path to a module.
func resolveModule(path string, dirs []string) (string, error) {
if filepath.IsAbs(path) {
err := fileutils.Exists(path)
return path, err
}
// Collect all errors to avoid suppressing important errors (e.g.,
// permission errors).
var multiErr error
for _, d := range dirs {
candidate := filepath.Join(d, path)
err := fileutils.Exists(candidate)
if err == nil {
return candidate, nil
}
multiErr = multierror.Append(multiErr, err)
}
return "", multiErr
}