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 6, 2025
1 parent 5b7cb5d commit 5351bb3
Show file tree
Hide file tree
Showing 4 changed files with 93 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()
}
29 changes: 29 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build !windows

package config

import (
"os"
"path/filepath"

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

// _configPath is the path to the macadam/machines.conf
// inside a given config directory.
const _configPath = "macadam/machines.conf"

// userConfigPath returns the path to the users local config that is
// not shared with other users. It uses $XDG_CONFIG_HOME/containers...
// if set or $HOME/.config/containers... if not.
func UserConfigPath() (string, error) {
if configHome := os.Getenv("XDG_CONFIG_HOME"); configHome != "" {
return filepath.Join(configHome, _configPath), nil
}
home, err := unshare.HomeDir()
if err != nil {
return "", err
}

return filepath.Join(home, _configPath), nil
}
17 changes: 17 additions & 0 deletions pkg/config/config_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:build windows

package config

import "os"

const (
// _configPath is the path to the macadam/machines.conf
// inside a given config directory.
_configPath = "\\macadam\\machines.conf"
)

// userConfigPath returns the path to the users local config that is
// not shared with other users. It uses $APPDATA/containers...
func UserConfigPath() (string, error) {
return os.Getenv("APPDATA") + _configPath, nil
}
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/cfergeau/macadam/pkg/config"
)

const connectionsFile = "macadam-connections.json"

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

connsFile := filepath.Join(filepath.Dir(path), 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 5351bb3

Please sign in to comment.