aboutsummaryrefslogtreecommitdiff
path: root/config/functions.go
blob: 58ab717df044786c0b8fffcedd11f156dc3e5c3d (plain)
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
package config

import (
	"os"
	"path/filepath"
	"strings"

	"dove/utils/toml"
)

func isDevelopmentMode() bool {
	executablePath, pathError := os.Executable()
	if pathError != nil {
		return false
	}

	return strings.Contains(executablePath, GO_BUILD_PATH_INDICATOR) ||
		strings.Contains(executablePath, GO_BUILD_ALT_INDICATOR)
}

func resolveOSConfigDirectory() string {
	configDirectory, directoryError := os.UserConfigDir()
	if directoryError != nil {
		return CURRENT_DIRECTORY
	}

	return filepath.Join(configDirectory, APPLICATION_DIRECTORY)
}

func resolveConfigFilePath(developmentMode bool, osConfigDirectory string) string {
	switch developmentMode {
	case true:
		return filepath.Join(CURRENT_DIRECTORY, CONFIG_FILE_NAME)
	default:
		return filepath.Join(osConfigDirectory, CONFIG_FILE_NAME)
	}
}

func resolveDataDirectory(developmentMode bool, osConfigDirectory string) string {
	switch developmentMode {
	case true:
		return CURRENT_DIRECTORY
	default:
		return osConfigDirectory
	}
}

func loadConfigFile(configFilePath string) error {
	if _, statError := os.Stat(configFilePath); statError != nil {
		return nil
	}

	return toml.LoadFile(configFilePath)
}

func parseSection(target any) error {
	return toml.Parse(target)
}