Skip to content

Commit 7dd77b3

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 7dd77b3

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-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/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/containers/storage/pkg/homedir"
8+
)
9+
10+
const connectionsFile = "macadam-connections.json"
11+
12+
func SetupEnvironment() error {
13+
path, err := homedir.GetConfigHome()
14+
if err != nil {
15+
return err
16+
}
17+
18+
connsFile := filepath.Join(filepath.Dir(path), "macadam", 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)