generated from cjlapao/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (54 loc) · 1.38 KB
/
main.go
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
package config
import (
"errors"
"github.com/cjlapao/common-go/execution_context"
"github.com/cjlapao/common-go/helper"
"github.com/cjlapao/tls-certificate-generator-go/constants"
"github.com/cjlapao/tls-certificate-generator-go/entities"
"gopkg.in/yaml.v2"
)
var ctx = execution_context.Get()
var config = ctx.Configuration
type Config struct {
Root []*entities.RootCertificate `json:"rootCertificates" yaml:"rootCertificates"`
OutputToFile bool `json:"outputToFile" yaml:"outputToFile"`
}
func Init() *Config {
config := Config{
Root: make([]*entities.RootCertificate, 0),
}
return &config
}
func (c *Config) ReadFromFile() error {
fileName := config.GetString("CONFIG_PATH")
if fileName == "" {
fileName = ".\\config.yml"
}
if !helper.FileExists(fileName) {
return errors.New("file not found")
}
yamlFile, err := helper.ReadFromFile(fileName)
if err != nil {
return err
}
err = yaml.Unmarshal(yamlFile, c)
if c.OutputToFile {
config.UpsertKey(constants.OUTPUT_TO_FILE, c.OutputToFile)
}
return nil
}
func (c *Config) SaveToFile() error {
fileName := config.GetString("CONFIG_PATH")
if fileName == "" {
fileName = ".\\config.yml"
}
if helper.FileExists(fileName) {
helper.DeleteFile(fileName)
}
yamlFile, err := yaml.Marshal(c)
if err != nil {
return err
}
helper.WriteToFile(string(yamlFile), fileName)
return nil
}