aboutsummaryrefslogtreecommitdiff
path: root/src/conf/conf.go
blob: 7a6dcbf4fe1020597f57ae151006a5ca594f2e2b (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
59
60
61
62
63
64
65
66
67
68
69
70
71
package conf

import (
	"log"
	"os"

	"github.com/BurntSushi/toml"
)

type (
	Conf struct {
		cwd    string
		path   string
		Db     Db     `toml:"database"`
		Server Server `toml:"server"`
	}

	Db struct {
		Adapter  string `toml:"adapter"`
		User     string `toml:"user"`
		Password string `toml:"pass"`
	}

	Server struct {
		Hostname string `toml:"hostname"`
		Port     string `toml:"port"`
		UiPath   string `toml:"ui_path"`
	}
)

func NewConf() *Conf {

	cwd, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}
	filepath := os.Getenv("NEWSSTAND_CONFIG_PATH")
	if filepath == "" {
		filepath = cwd + "/.newsstandrc.toml"
	}
	log.Printf("Config file path: %s", filepath)

	c := Conf{
		cwd,
		filepath,
		Db{},
		Server{
			UiPath: cwd + "/ui",
		},
	}
	_, err = toml.DecodeFile(filepath, &c)
	if err != nil {
		log.Fatalln(err)
	}
	log.Printf("Config loaded: %s", c)

	return &c
}

func (c *Conf) GetCwd() string {
	return c.cwd
}

func (c *Conf) GetPath() string {
	return c.path
}

func resolvePath(paths ...string) string {
	// TODO resolve file paths to cwd + partial or provided path
	return ""
}