This repository has been archived by the owner on Jan 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pkgcfg.go
113 lines (95 loc) · 2.36 KB
/
pkgcfg.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
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
104
105
106
107
108
109
110
111
112
113
package pkgcfg
import (
"encoding/json"
"os"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/starofservice/vconf"
"github.com/starofservice/carbon/pkg/schema/pkgcfg/latest"
"github.com/starofservice/carbon/pkg/util/command"
"github.com/starofservice/carbon/pkg/util/tojson"
)
var schemaVersions = map[string]func() vconf.ConfigInterface{
latest.Version: latest.NewCarbonConfig,
}
const (
HookPreBuild = "pre-build"
HookPostBuild = "post-build"
)
func GetCurrentVersion(data []byte) (string, error) {
type VersionStruct struct {
APIVersion string `json:"apiVersion"`
}
version := &VersionStruct{}
if err := json.Unmarshal(data, version); err != nil {
return "", errors.Wrap(err, "parsing api version")
}
return version.APIVersion, nil
}
type CarbonConfig struct {
Cwd string
Data latest.CarbonConfig
}
func ParseConfig(dir string, data []byte, argName, argVPref, argVSuf string) (*CarbonConfig, error) {
log.Debug("Processing Carbon config")
jsonData, err := tojson.ToJSON(data)
if err != nil {
return nil, err
}
current, err := GetCurrentVersion(jsonData)
if err != nil {
return nil, err
}
sh := vconf.NewSchemaHandler(latest.Version)
for k, v := range schemaVersions {
sh.RegVersion(k, v)
}
cfg, err := sh.GetLatestConfig(current, jsonData)
if err != nil {
return nil, err
}
parsedCfg := cfg.(*latest.CarbonConfig)
if argName != "" {
parsedCfg.Name = argName
}
parsedCfg.Version = argVPref + parsedCfg.Version + argVSuf
pc := &CarbonConfig{
Cwd: dir,
Data: *parsedCfg,
}
return pc, nil
}
func (self *CarbonConfig) HookDefined(hookType string) bool {
switch hookType {
case HookPreBuild:
if len(self.Data.Hooks.PreBuild) > 0 {
return true
}
return false
case HookPostBuild:
if len(self.Data.Hooks.PostBuild) > 0 {
return true
}
return false
default:
return false
}
}
func (self *CarbonConfig) RunHook(hookType string) error {
var cmds []string
switch hookType {
case HookPreBuild:
cmds = self.Data.Hooks.PreBuild
case HookPostBuild:
cmds = self.Data.Hooks.PostBuild
default:
return errors.Errorf("Unsupported hook type: %s", hookType)
}
for _, i := range cmds {
err := command.Run(i, self.Cwd, os.Stdout, os.Stderr)
if err != nil {
return err
}
}
return nil
}