~hristoast/wem

wem/cfg/main.go -rw-r--r-- 3.3 KiB
e4496baaHristos N. Triantafillou Changelog for the DNS flag 18 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Package cfg represents the overall wem application configuration.
package cfg

import (
	"errors"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"

	"github.com/pelletier/go-toml"
)

const WemName = "wem"

var WemVersion string = "0.0.0--DEV"

type WemConfig struct {
	CacheDir     string
	GeWineDir    string
	WineBuildDir string
	WineEnvDir   string
	WineSrcDir   string
}

func (w *WemConfig) ApplyArgs(cacheDir, protonDir, wineBuildDir, wineEnvDir, wineSrcDir string) {
	if cacheDir != "" {
		w.CacheDir = cacheDir
	}
	if protonDir != "" {
		w.GeWineDir = protonDir
	}
	if wineBuildDir != "" {
		w.WineBuildDir = wineBuildDir
	}
	if wineEnvDir != "" {
		w.WineEnvDir = wineEnvDir
	}
	if wineSrcDir != "" {
		w.WineSrcDir = wineSrcDir
	}
}

func (w *WemConfig) AsToml() (string, error) {
	b, err := toml.Marshal(w)
	if err != nil {
		return "", err
	}
	return string(b), nil
}

// EnsureDirs ensures that all dirs defined in
// the config exist, creating them as needed.
func (w *WemConfig) EnsureDirs() error {
	for _, dirPath := range []string{w.CacheDir, w.GeWineDir, w.WineBuildDir, w.WineEnvDir, w.WineSrcDir} {
		_, err := os.Stat(dirPath)
		if err != nil {
			err = os.MkdirAll(dirPath, 0755)
			if err != nil {
				return err
			}
		}
	}

	return nil
}

// Validate indicates if there are any missing config options.
func (w *WemConfig) Validate() error {
	if w.CacheDir == "" {
		return errors.New("CacheDir is not set")
	}
	if w.GeWineDir == "" {
		return errors.New("GeWineDir is not set - do you need to change ProtonDir to GeWineDir in ~/.config/wem/wem.cfg?") //TODO: remove this after a few releases (new in 0.9.2)
	}
	if w.WineBuildDir == "" {
		return errors.New("WineBuildDir is not set")
	}
	if w.WineEnvDir == "" {
		return errors.New("WineEnvDir is not set")
	}
	if w.WineSrcDir == "" {
		return errors.New("WineSrcDir is not set")
	}
	return nil
}

func defaultWemConfig() string {
	home := os.Getenv("HOME")
	c := fmt.Sprintf(`# Full path to the directory where archives should be cached to.
CacheDir = "%s/.local/share/wem/cache"

# Full path to the directory where GE Wine installations should go.
GeWineDir = "%s/.local/share/wem/ge-wine"

# Full path to the directory where wem-built wine installations should go.
WineBuildDir = "%s/.local/share/wem/builds"

# Full path to the directory where wem envs will be created by default.
WineEnvDir = "%s/.local/share/wem/envs"

# Full path to the directory where wine source code is kept.
WineSrcDir = "%s/.local/share/wem/src"
`, home, home, home, home, home)
	return c
}

func CfgDir() string {
	return filepath.Join(os.Getenv("HOME"), ".config", "wem")
}

func GetConfig() (*WemConfig, error) {
	cfgPath := filepath.Join(CfgDir(), "wem.cfg")
	_, err := os.Stat(cfgPath)

	if err != nil {
		err = os.MkdirAll(filepath.Dir(cfgPath), 0755)
		if err != nil {
			return nil, err
		}
		f, err := os.Create(cfgPath)
		if err != nil {
			return nil, err
		}
		defer f.Close()

		// We do this instead of toml.Marshal()ing a struct because
		// it's nice to provide comments in the config file.
		f.WriteString(defaultWemConfig())
	}

	c, err := ioutil.ReadFile(cfgPath)
	if err != nil {
		return nil, err
	}

	cfgStr := os.ExpandEnv(string(c))
	wc := &WemConfig{}
	toml.Unmarshal([]byte(cfgStr), wc)

	err = wc.Validate()
	if err == nil {
		return wc, nil
	}
	return nil, err
}