Skip to content

Commit 5351bb3

Browse files
committed
set env variables to execute commands
before executing any action we need to update the environment variables PODMAN_CONNECTIONS_CONF, PODMAN_DATA_DIR and PODMAN_RUNTIME_DIR to define a specific location where to store macadam data. This way we prevent to write macadam stuff within podman-related folders Signed-off-by: lstocchi <[email protected]>
1 parent 5b7cb5d commit 5351bb3

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

cmd/macadam/root.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/cfergeau/macadam/cmd/macadam/registry"
1010
"github.com/cfergeau/macadam/pkg/cmdline"
11+
"github.com/cfergeau/macadam/pkg/env"
1112
"github.com/containers/podman/v5/libpod/define"
1213
"github.com/spf13/cobra"
1314
)
@@ -52,6 +53,7 @@ var (
5253
TraverseChildren: true,
5354
Version: cmdline.Version(),
5455
DisableFlagsInUseLine: true,
56+
PersistentPreRunE: machinePreRunE,
5557
}
5658

5759
defaultLogLevel = "warn"
@@ -82,3 +84,7 @@ func Execute() {
8284

8385
os.Exit(registry.GetExitCode())
8486
}
87+
88+
func machinePreRunE(c *cobra.Command, args []string) error {
89+
return env.SetupEnvironment()
90+
}

pkg/config/config.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//go:build !windows
2+
3+
package config
4+
5+
import (
6+
"os"
7+
"path/filepath"
8+
9+
"github.com/containers/storage/pkg/unshare"
10+
)
11+
12+
// _configPath is the path to the macadam/machines.conf
13+
// inside a given config directory.
14+
const _configPath = "macadam/machines.conf"
15+
16+
// userConfigPath returns the path to the users local config that is
17+
// not shared with other users. It uses $XDG_CONFIG_HOME/containers...
18+
// if set or $HOME/.config/containers... if not.
19+
func UserConfigPath() (string, error) {
20+
if configHome := os.Getenv("XDG_CONFIG_HOME"); configHome != "" {
21+
return filepath.Join(configHome, _configPath), nil
22+
}
23+
home, err := unshare.HomeDir()
24+
if err != nil {
25+
return "", err
26+
}
27+
28+
return filepath.Join(home, _configPath), nil
29+
}

pkg/config/config_windows.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//go:build windows
2+
3+
package config
4+
5+
import "os"
6+
7+
const (
8+
// _configPath is the path to the macadam/machines.conf
9+
// inside a given config directory.
10+
_configPath = "\\macadam\\machines.conf"
11+
)
12+
13+
// userConfigPath returns the path to the users local config that is
14+
// not shared with other users. It uses $APPDATA/containers...
15+
func UserConfigPath() (string, error) {
16+
return os.Getenv("APPDATA") + _configPath, nil
17+
}

pkg/env/env.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package env
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
7+
"github.com/cfergeau/macadam/pkg/config"
8+
)
9+
10+
const connectionsFile = "macadam-connections.json"
11+
12+
func SetupEnvironment() error {
13+
path, err := config.UserConfigPath()
14+
if err != nil {
15+
return err
16+
}
17+
18+
connsFile := filepath.Join(filepath.Dir(path), connectionsFile)
19+
// set the path used for storing connection of macadam vms
20+
err = os.Setenv("PODMAN_CONNECTIONS_CONF", connsFile)
21+
if err != nil {
22+
return err
23+
}
24+
25+
// set the directory used when calculating the data and config paths
26+
// config -> <configHome>/containers/macadam/machine (configHome changes based on the OS used e.g. configHome == /home/user/.config)
27+
// data -> <dataHome>/containers/macadam/machine (dataHome changes based on the OS used e.g. dataHome == /home/user/.local/share)
28+
err = os.Setenv("PODMAN_DATA_DIR", filepath.Join("macadam", "machine"))
29+
if err != nil {
30+
return err
31+
}
32+
33+
// set the directory to be used when calculating runtime path
34+
// run -> <runHome>/macadam (runHome changes based on the OS used e.g. runHome == /run)
35+
err = os.Setenv("PODMAN_RUNTIME_DIR", "macadam")
36+
if err != nil {
37+
return err
38+
}
39+
40+
return nil
41+
}

0 commit comments

Comments
 (0)