aboutsummaryrefslogtreecommitdiff
path: root/internal/config/config.go
blob: a37cef1870627590c606f2ee397481bad8a15fee (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
package config

import (
	"os"
	"os/user"
	"runtime"
	"strings"
)

type Map map[string]interface{}

type GlobalOptions struct {
	Timeout      int      `yaml:"timeout"`
	DefaultEmail string   `yaml:"default-email"`
	Target       string   `yaml:"target"`
	Parts        []string `yaml:"parts"`
}

var DefaultGlobalOptions = GlobalOptions{
	Timeout:      30,
	DefaultEmail: username() + "@" + hostname(),
	Target:       "",
	Parts:        []string{"text", "html"},
}

type Config struct {
	GlobalOptions
	GlobalConfig Map
}

type Options struct {
	MinFreq    int   `yaml:"min-frequency"`
	InclImages *bool `yaml:"include-images"`
}

func (c *Config) WithPartText() bool {
	for _, part := range c.Parts {
		if part == "text" {
			return true
		}
	}

	return false
}

func (c *Config) WithPartHtml() bool {
	for _, part := range c.Parts {
		if part == "html" {
			return true
		}
	}

	return false
}

func hostname() (hostname string) {
	hostname, err := os.Hostname()
	if err != nil {
		hostname = "localhost"
	}
	return
}

func username() string {
	u, err := user.Current()
	switch {
	case err != nil:
		return "user"
	case runtime.GOOS == "windows":
		// the domain is attached -- remove it again
		split := strings.Split(u.Username, "\\")
		return split[len(split)-1]
	default:
		return u.Username
	}
}