forked from drone/drone-runtime
/
main.go
110 lines (93 loc) · 2.03 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"time"
"github.com/mattn/go-isatty"
"github.com/drone/drone-runtime/engine"
"github.com/drone/drone-runtime/engine/docker"
"github.com/drone/drone-runtime/engine/plugin"
"github.com/drone/drone-runtime/runtime"
"github.com/drone/drone-runtime/runtime/chroot"
"github.com/drone/drone-runtime/runtime/term"
"github.com/drone/drone-runtime/version"
"github.com/drone/signal"
)
var tty = isatty.IsTerminal(os.Stdout.Fd())
var (
b = flag.String("chroot", "", "")
p = flag.String("plugin", "", "")
t = flag.Duration("timeout", time.Hour, "")
v = flag.Bool("version", false, "")
h = flag.Bool("help", false, "")
)
func main() {
flag.BoolVar(h, "h", false, "")
flag.BoolVar(v, "v", false, "")
flag.Usage = usage
flag.Parse()
if *h {
flag.Usage()
os.Exit(0)
}
if *v {
fmt.Println(version.Version)
os.Exit(0)
}
var source string
if flag.NArg() > 0 {
source = flag.Args()[0]
}
config, err := engine.ParseFile(source)
if err != nil {
log.Fatalln(err)
}
var engine engine.Engine
if *p == "" {
engine, err = docker.NewEnv()
if err != nil {
log.Fatalln(err)
}
} else {
engine, err = plugin.Open(*p)
if err != nil {
log.Fatalln(err)
}
}
hooks := new(runtime.Hook)
hooks.GotLine = term.WriteLine(os.Stdout)
if tty {
hooks.GotLine = term.WriteLinePretty(os.Stdout)
}
var fs runtime.FileSystem
if *b != "" {
fs, err = chroot.New(*b)
if err != nil {
log.Fatalln(err)
}
}
r := runtime.New(
runtime.WithFileSystem(fs),
runtime.WithEngine(engine),
runtime.WithConfig(config),
runtime.WithHooks(hooks),
)
c, cancel := context.WithTimeout(context.Background(), *t)
c = signal.WithContext(c)
defer cancel()
err = r.Run(c)
if err != nil {
log.Fatalln(err)
}
}
func usage() {
fmt.Println(`Usage: drone-runtime [OPTION]... [SOURCE]
--plugin loads a runtime engine from a .so file
--timeout sets an execution timeout
-v, --version display the version exit
-h, --help display this help and exit
`)
}