Skip to content

Commit

Permalink
set env variables to execute commands
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
lstocchi committed Feb 11, 2025
1 parent 5b7cb5d commit 7dd77b3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/macadam/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/cfergeau/macadam/cmd/macadam/registry"
"github.com/cfergeau/macadam/pkg/cmdline"
"github.com/cfergeau/macadam/pkg/env"
"github.com/containers/podman/v5/libpod/define"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -52,6 +53,7 @@ var (
TraverseChildren: true,
Version: cmdline.Version(),
DisableFlagsInUseLine: true,
PersistentPreRunE: machinePreRunE,
}

defaultLogLevel = "warn"
Expand Down Expand Up @@ -82,3 +84,7 @@ func Execute() {

os.Exit(registry.GetExitCode())
}

func machinePreRunE(c *cobra.Command, args []string) error {
return env.SetupEnvironment()
}
41 changes: 41 additions & 0 deletions pkg/env/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package env

import (
"os"
"path/filepath"

"github.com/containers/storage/pkg/homedir"
)

const connectionsFile = "macadam-connections.json"

func SetupEnvironment() error {
path, err := homedir.GetConfigHome()
if err != nil {
return err
}

connsFile := filepath.Join(filepath.Dir(path), "macadam", connectionsFile)
// set the path used for storing connection of macadam vms
err = os.Setenv("PODMAN_CONNECTIONS_CONF", connsFile)
if err != nil {
return err
}

// set the directory used when calculating the data and config paths
// config -> <configHome>/containers/macadam/machine (configHome changes based on the OS used e.g. configHome == /home/user/.config)
// data -> <dataHome>/containers/macadam/machine (dataHome changes based on the OS used e.g. dataHome == /home/user/.local/share)
err = os.Setenv("PODMAN_DATA_DIR", filepath.Join("macadam", "machine"))
if err != nil {
return err
}

// set the directory to be used when calculating runtime path
// run -> <runHome>/macadam (runHome changes based on the OS used e.g. runHome == /run)
err = os.Setenv("PODMAN_RUNTIME_DIR", "macadam")
if err != nil {
return err
}

return nil
}

0 comments on commit 7dd77b3

Please sign in to comment.