forked from portapps/portapps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
60 lines (51 loc) · 1.36 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
package portapps
import (
"fmt"
"io/ioutil"
"os"
"github.com/portapps/portapps/pkg/utl"
"gopkg.in/yaml.v2"
)
// Config holds portapp configuration details
type Config struct {
Common Common `yaml:"common" mapstructure:"common"`
App interface{} `yaml:"app,omitempty" mapstructure:"app"`
}
// Common holds common configuration data
type Common struct {
Args []string `yaml:"args" mapstructure:"cmd_switches"`
Env map[string]string `yaml:"env" mapstructure:"env"`
AppPath string `yaml:"app_path" mapstructure:"app_path"`
}
// loadConfig load common and app configuration
func (app *App) loadConfig(appcfg interface{}) (err error) {
cfgPath := utl.PathJoin(app.RootPath, fmt.Sprintf("%s.yml", app.ID))
app.config = &Config{
Common: Common{
Args: []string{},
Env: map[string]string{},
AppPath: "",
},
App: appcfg,
}
// Write sample config
raw, err := yaml.Marshal(app.config)
if err != nil {
return err
}
err = ioutil.WriteFile(utl.PathJoin(app.RootPath, fmt.Sprintf("%s.sample.yml", app.ID)), raw, 0644)
if err != nil {
return err
}
// Skip if config file not found
if _, err := os.Stat(cfgPath); err != nil {
return nil
}
// Read config
raw, err = ioutil.ReadFile(cfgPath)
if err != nil {
return err
}
Log.Info().Msgf("read config:\n%s", string(raw))
return yaml.Unmarshal(raw, &app.config)
}