aboutsummaryrefslogtreecommitdiff
path: root/src/conf/conf.go
blob: 62ffb57050038158404234f0274e1f09460ff8a9 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package conf

import (
	"fmt"
	"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"`
		Secret   string `toml:"secret"`
		Hostname string `toml:"hostname"`
		Port     string `toml:"port"`
		DbName   string `toml:"dbname"`
		Url      string
	}

	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{
			Hostname: "localhost",
		},
		Server{
			UiPath: cwd + "/ui",
		},
	}
	_, err = toml.DecodeFile(filepath, &c)
	if err != nil {
		log.Fatalln(err)
	}
	c.setDbUrl()
	log.Printf("Config loaded: %s", c)

	return &c
}

func (c *Conf) setDbUrl() {
	c.Db.Url = fmt.Sprintf(
		"%s://%s:%s@%s:%s/%s",
		c.Db.Adapter,
		c.Db.User,
		c.Db.Secret,
		c.Db.Hostname,
		c.Db.Port,
		c.Db.DbName,
	)
}

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 ""
}