Repository navigation
Expand file tree
/
Copy pathgithub.go
More file actions
74 lines (62 loc) · 1.55 KB
/
Copy pathgithub.go
File metadata and controls
74 lines (62 loc) · 1.55 KB
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
package github
import (
"bytes"
"errors"
"os/exec"
"strings"
"webtyp.com/deploy"
)
// Actions is the CI id a project declares in config/deploy.go.
const Actions deploy.CIID = "webtyp.com/github.Actions"
// Runner runs an external command in dir, writing stdin to it, and returns
// its stdout. Secret values travel only through stdin.
type Runner interface {
Run(dir, stdin, name string, args ...string) ([]byte, error)
}
// ExecRunner runs commands with os/exec.
type ExecRunner struct{}
func (e ExecRunner) Run(dir, stdin, name string, args ...string) ([]byte, error) {
cmd := exec.Command(name, args...)
cmd.Dir = dir
if stdin != "" {
cmd.Stdin = strings.NewReader(stdin)
}
var stderr bytes.Buffer
cmd.Stderr = &stderr
out, err := cmd.Output()
if err != nil {
if stderr.Len() > 0 {
return nil, errors.New(err.Error() + ": " + strings.TrimSpace(stderr.String()))
}
return nil, err
}
return out, nil
}
// Config configures a CI. Both fields are required.
type Config struct {
RootDir string // the project root (contains .git and go.mod)
Runner Runner
}
// CI implements deploy.CI for GitHub Actions.
type CI struct {
rootDir string
runner Runner
repo string // cached repository name
}
// New returns a CI, or an error if a Config field is missing.
func New(cfg Config) (*CI, error) {
if cfg.RootDir == "" {
return nil, ErrNoRootDir
}
if cfg.Runner == nil {
return nil, ErrNoRunner
}
return &CI{
rootDir: cfg.RootDir,
runner: cfg.Runner,
}, nil
}
func (c *CI) ID() deploy.CIID {
return Actions
}
var _ deploy.CI = (*CI)(nil)