/
run.go
91 lines (70 loc) · 1.87 KB
/
run.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
/*
winch - Universal Build and Release Tool
Copyright (C) 2021 Ketch Kloud, Inc.
This program is free software: you can redistribute it and/or modify it under the terms of the GNU
General Public License as published by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General Public License along with this program. If not,
see <https://www.gnu.org/licenses/>.
*/
package pkg
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"strings"
"github.com/winchci/winch/pkg/config"
)
func Run(ctx context.Context, cmd *config.RunConfig, cfg *config.Config) error {
if !cmd.IsEnabled() {
return nil
}
if !CheckFilters(ctx, cmd.Branches, cmd.Tags) {
return nil
}
c := exec.CommandContext(ctx, "sh", "-c", cmd.Command)
env := make(map[string]string)
for _, e := range os.Environ() {
parts := strings.SplitN(e, "=", 2)
env[parts[0]] = parts[1]
}
for k, v := range cfg.Environment {
env[k] = v
}
for k, v := range cmd.Environment {
env[k] = v
}
for replacementsRequired := true; replacementsRequired; {
replacementsRequired = false
for k, v := range env {
env[k] = os.Expand(v, func(s string) string {
return env[s]
})
if strings.Contains(env[k], "$") {
replacementsRequired = true
}
}
}
for k, v := range env {
c.Env = append(c.Env, fmt.Sprintf("%s=%s", k, v))
}
if len(cmd.Input) > 0 {
c.Stdin = bytes.NewBufferString(cmd.Input)
}
if cfg.Verbose {
c.Stdout = os.Stdout
c.Stderr = os.Stderr
}
if len(cmd.Name) > 0 {
fmt.Println(cmd.Name)
}
if !cfg.Quiet {
fmt.Println(cmd.Command)
}
return c.Run()
}