all repos — nirvash @ 119d66cd2726ad7bd8058a96dbe162d27da56a61

modular CMS using the quartzgun library

archetype/config.go (raw)

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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package archetype

import (
	"fmt"
	"os"
	"path/filepath"
	"runtime"
	"strings"
)

type Config struct {
	Adapter          Adapter // adapter for this instance
	Root             string  // root of the site data
	StaticRoot       string  // root of static files for StaticFileManager
	StaticShowHidden bool    // whether to show hidden files in the StaticFileManager
	StaticShowHtml   bool    // whether to show html files in the StaticFileManager
	AssetRoot        string  // root of Nirvash dist files (CSS, images)
	Plugins          map[string]interface{}
}

func GetConfigLocation() string {
	home := os.Getenv("HOME")
	appdata := os.Getenv("APPDATA")
	switch runtime.GOOS {
	case "windows":
		return filepath.Join(appdata, "nirvash")
	case "darwin":
		return filepath.Join(home, "Library", "Application Support", "nirvash")
	case "plan9":
		return filepath.Join(home, "lib", "nirvash")
	default:
		return filepath.Join(home, ".config", "nirvash")
	}
}

func ensureConfigLocationExists() {
	fileInfo, err := os.Stat(GetConfigLocation())

	if os.IsNotExist(err) {
		os.MkdirAll(GetConfigLocation(), os.ModePerm)
	} else if !fileInfo.IsDir() {
		panic("Config location is not a directory!")
	}
}

func ReadConfig() *Config {
	ensureConfigLocationExists()
	return parseConfig(filepath.Join(GetConfigLocation(), "nirvash.conf"))
}

func (self *Config) Write() error {
	ensureConfigLocationExists()
	return writeConfig(self, filepath.Join(GetConfigLocation(), "nirvash.conf"))
}

func (self *Config) SetAdapter(adapter string) {
	switch adapter {
	case "eureka":
		self.Adapter = &EurekaAdapter{}
	default:
		panic("Unsupported adapter! Try one of [ eureka ]")
	}
}

func (self *Config) IsNull() bool {
	return self.Adapter == nil || len(self.Root) == 0 || len(self.StaticRoot) == 0 || len(self.AssetRoot) == 0
}

func (self *Config) RunWizard() {
	fmt.Printf("All options are required.\n")
	defer func(cfg *Config) {
		if r := recover(); r != nil {
			fmt.Printf("Invalid selection, starting over...")
			cfg.RunWizard()
		}
	}(self)
	inputBuf := ""
	fmt.Printf("adapter? (eureka) [eureka] ")
	fmt.Scanln(&inputBuf)
	if len(strings.TrimSpace(inputBuf)) == 0 {
		inputBuf = "eureka"
	}
	self.SetAdapter(inputBuf)

	inputBuf = ""
	fmt.Printf("site data root? ")
	ensureNonEmptyOption(&inputBuf)
	self.Root = inputBuf

	inputBuf = ""

	fmt.Printf("static file root? ")
	ensureNonEmptyOption(&inputBuf)
	self.StaticRoot = inputBuf

	inputBuf = ""
	fmt.Printf("nirvash asset root? ")
	ensureNonEmptyOption(&inputBuf)
	self.AssetRoot = inputBuf

	inputBuf = ""
	fmt.Printf("plugins? (not implemented yet) ")
	ensureNonEmptyOption(&inputBuf)
	//self.Plugins = processPlugins(inputBuf)

	fmt.Printf("Configuration complete!\n")
	self.Write()
}

func ensureNonEmptyOption(buffer *string) {
	for {
		fmt.Scanln(buffer)
		if len(strings.TrimSpace(*buffer)) != 0 {
			break
		}
	}
}

func writeConfig(cfg *Config, configFile string) error {
	f, err := os.Create(configFile)
	if err != nil {
		return err
	}

	defer f.Close()

	f.WriteString("root=" + cfg.Root + "\n")
	f.WriteString("staticRoot=" + cfg.StaticRoot + "\n")
	f.WriteString("assetRoot=" + cfg.AssetRoot + "\n")
	f.WriteString("adapter=" + cfg.Adapter.Name() + "\n")
	f.WriteString("plugins=\n")
	return nil
}

func parseConfig(configFile string) *Config {
	f, err := os.ReadFile(configFile)
	cfg := &Config{}
	if err != nil {
		return cfg
	}

	fileData := string(f[:])

	lines := strings.Split(fileData, "\n")

	for _, l := range lines {
		if len(l) == 0 {
			continue
		}
		if !strings.Contains(l, "=") {
			panic("Malformed config not in INI format")
		}

		kvp := strings.Split(l, "=")
		k := strings.TrimSpace(kvp[0])
		v := strings.TrimSpace(kvp[1])
		switch k {
		case "root":
			cfg.Root = v
		case "staticRoot":
			cfg.StaticRoot = v
		case "assetRoot":
			cfg.AssetRoot = v
		case "plugins":
			// not implemented
		case "adapter":
			cfg.SetAdapter(v)
		default:
			panic("Unrecognized config option: " + k)
		}
	}
	return cfg
}