summaryrefslogtreecommitdiff
path: root/config.Save.go
blob: 28d5b4b243ad1767d08a6586b322cf43807cc01e (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
92
93
94
95
96
97
98
99
100
101
102
103
package config

import (
	"os"
	"strings"
	"sync"

	"go.wit.com/log"
)

// an experiment to see if this is useful
var saveMu sync.RWMutex

// returns true if config is working okay
func InitValid() bool {
	saveMu.Lock()
	defer saveMu.Unlock()
	if configPB == nil {
		// todo: try to re-init it here
		return false
	}
	return true
}

// saves your applications config file
func Save() error {
	/*
		basedir, _ := filepath.Split(configPB.Filename)
		if err := os.MkdirAll(basedir, os.ModePerm); err != nil {
			return err
		}
		saveENV()
		saveMu.Lock()
		defer saveMu.Unlock()
		err := SavePB(configPB)
	*/
	return saveENV()
}

func saveENV() error {
	filename, err := getConfigFilenameENV()
	if err != nil {
		return err
	}
	saveMu.Lock()
	defer saveMu.Unlock()
	outENV, err := formatENV()
	if err == nil {
		log.Info("SAVEENV IS RUNNING")
		log.Info("SAVEENV IS RUNNING")
		log.Info("SAVEENV IS RUNNING")
		log.Info(outENV)
	}
	return os.WriteFile(filename, []byte(outENV), 0644)
}

func Get(flag string) string {
	saveMu.Lock()
	defer saveMu.Unlock()
	if configPB == nil {
		return ""
	}
	c := findByLower(flag)
	if c == nil {
		return ""
	}

	return c.Value
}

func True(flag string) bool {
	saveMu.Lock()
	defer saveMu.Unlock()
	if configPB == nil {
		return false
	}
	found := configPB.findByKey(flag)
	if found == nil {
		return false
	}
	if strings.ToLower(found.Value) == "true" {
		return true
	}
	return false
}

func Set(key string, newValue string) error {
	saveMu.Lock()
	defer saveMu.Unlock()
	if configPB == nil {
		return NotInitialized
	}
	found := configPB.findByKey(key)
	if found != nil {
		found.Value = newValue
	}

	newvar := new(Config)
	newvar.Key = key
	newvar.Value = newValue
	configPB.Append(newvar)
	return nil
}