File tree Expand file tree Collapse file tree 4 files changed +93
-0
lines changed Expand file tree Collapse file tree 4 files changed +93
-0
lines changed Original file line number Diff line number Diff line change 8
8
9
9
"github.com/cfergeau/macadam/cmd/macadam/registry"
10
10
"github.com/cfergeau/macadam/pkg/cmdline"
11
+ "github.com/cfergeau/macadam/pkg/env"
11
12
"github.com/containers/podman/v5/libpod/define"
12
13
"github.com/spf13/cobra"
13
14
)
52
53
TraverseChildren : true ,
53
54
Version : cmdline .Version (),
54
55
DisableFlagsInUseLine : true ,
56
+ PersistentPreRunE : machinePreRunE ,
55
57
}
56
58
57
59
defaultLogLevel = "warn"
@@ -82,3 +84,7 @@ func Execute() {
82
84
83
85
os .Exit (registry .GetExitCode ())
84
86
}
87
+
88
+ func machinePreRunE (c * cobra.Command , args []string ) error {
89
+ return env .SetupEnvironment ()
90
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments