-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
68 lines (53 loc) · 1.54 KB
/
config.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
package generator
import (
"fmt"
"path/filepath"
"sync"
log "github.com/sirupsen/logrus"
"github.com/catusax/pgen/generator/custom_func"
"gopkg.in/yaml.v2"
)
// Config represents the configuration
type Config struct {
// OnceFiles only generate once when creating new project
OnceFiles []string `yaml:"once_files"`
// SoftFiles will be generate every time when using generate command
SoftFiles []string `yaml:"soft_files"`
// HardFiles will be generate every time when using generate command with --hard flag
HardFiles []string `yaml:"hard_files"`
// DefaultENVs is default environment variables when creating new project.
// on existing project, Makefile variables will replace these variables.
DefaultENVs map[string]any `yaml:"default_envs"`
WasmFuncs []custom_func.Libs `yaml:"wasm_funcs"`
confDir string
}
var (
c *Config
configOnce sync.Once
)
func Conf() *Config {
configOnce.Do(initConfig)
return c
}
func initConfig() {
var err error
c, err = readConfig()
if err != nil {
panic(err)
}
}
func readConfig() (*Config, error) {
config := Config{}
fileBytes, readPath, err := ReadFile(filepath.Join(".template", ".pgen_config.yaml"))
if err != nil {
return nil, fmt.Errorf("failed to read config: %w", err)
}
log.Infoln("loaded config from file: ",
filepath.Join(readPath, ".template", ".pgen_config.yaml"))
err = yaml.Unmarshal(fileBytes, &config)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal config: %w", err)
}
config.confDir = filepath.Join(readPath, ".template")
return &config, nil
}