1删除vendor添加go mod 2配置文件支持环境变量

This commit is contained in:
wangzhngyang 2020-01-03 16:45:49 +08:00
parent 8fe6c9a05d
commit 2ea79134e6
3 changed files with 55 additions and 1 deletions

View File

@ -60,6 +60,16 @@ Our `hooks.json` file will now look like this:
}
]
```
添加对环境变量的支持
```json
[
{
"id": "redeploy-webhook",
"execute-command": "${PATH}/redeploy.sh",
"command-working-directory": "/var/webhook"
}
]
```
You can now run [webhook][w] using
```bash

17
go.mod Normal file
View File

@ -0,0 +1,17 @@
module github.com/adnanh/webhook
go 1.13
require (
github.com/codegangsta/negroni v0.2.1-0.20171009163950-5bc66cf1ad89
github.com/fsnotify/fsnotify v1.4.7 // indirect
github.com/ghodss/yaml v1.0.0
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/mux v1.5.1-0.20171008214913-bdd5a5a1b0b4
github.com/kr/pretty v0.2.0 // indirect
github.com/satori/go.uuid v1.1.1-0.20170321230731-5bf94b69c6b6
golang.org/x/sys v0.0.0-20200102141924-c96a22e43c9c // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/fsnotify.v1 v1.4.2
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7 // indirect
)

View File

@ -648,10 +648,37 @@ func (h *Hooks) LoadFromFile(path string, asTemplate bool) error {
file = buf.Bytes()
}
e = yaml.Unmarshal(file, h)
f, err := h.ReplaceEnv(string(file))
if err != nil {
return err
}
e = yaml.Unmarshal([]byte(f), h)
return e
}
// ReplaceEnv replace env
func (h *Hooks) ReplaceEnv(str string) (string, error) {
expr := `\${[A-Za-z_]*}`
r, err := regexp.Compile(expr)
if err != nil {
return "", fmt.Errorf("regexp.Compile error: %w", err)
}
if r == nil {
return "", fmt.Errorf("r is nil")
}
all := r.FindAll([]byte(str), -1)
for _, v := range all {
envStr := string(v)
env := os.Getenv(envStr[2 : len(envStr)-1])
if env == "" {
return "", errors.New("get env failed:" + envStr)
}
str = strings.ReplaceAll(str, envStr, env)
}
return str, nil
}
// Append appends hooks unless the new hooks contain a hook with an ID that already exists
func (h *Hooks) Append(other *Hooks) error {
for _, hook := range *other {