-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
153 lines (136 loc) · 4.07 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2022 Harness Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"flag"
"os"
"golang.org/x/exp/slog"
"github.com/drone/plugin/cloner"
"github.com/drone/plugin/plugin/bitrise"
"github.com/drone/plugin/plugin/github"
"github.com/drone/plugin/plugin/harness"
)
var (
name string // plugin name
repo string // plugin repository
ref string // plugin repository reference
sha string // plugin repository commit
kind string // plugin kind (action, bitrise, harness)
downloadOnly bool // plugin won't be executed on setting this flag. Only source will be downloaded. Used for caching the plugin dependencies
)
func main() {
ctx := context.Background()
level := slog.LevelInfo
if os.Getenv("DRONE_DEBUG") == "true" {
level = slog.LevelDebug
}
logger := slog.New(slog.HandlerOptions{Level: level}.NewTextHandler(os.Stdout))
slog.SetDefault(logger)
// parse the input parameters
flag.StringVar(&name, "name", "", "plugin name")
flag.StringVar(&repo, "repo", "", "plugin repository")
flag.StringVar(&ref, "ref", "", "plugin reference")
flag.StringVar(&sha, "sha", "", "plugin commit")
flag.StringVar(&kind, "kind", "", "plugin kind")
flag.BoolVar(&downloadOnly, "download-only", false, "plugin downloadOnly")
flag.Parse()
// the user may specific the action plugin alias instead
// of the git repository. We are able to lookup the plugin
// by alias to find the corresponding repository and ref.
if repo == "" && kind == "action" {
repo_, ref_, ok := github.ParseLookup(name)
if ok {
repo = repo_
ref = ref_
}
}
// the user may specific the harness plugin alias instead
// of the git repository. We are able to lookup the plugin
// by alias to find the corresponding repository and commit.
if repo == "" && kind == "harness" {
repo_, ref_, sha_, ok := harness.ParseLookup(name)
if ok {
repo = repo_
ref = ref_
sha = sha_
}
}
// the user may specific the bitrise plugin alias instead
// of the git repository. We are able to lookup the plugin
// by alias to find the corresponding repository and commit.
if repo == "" && kind == "bitrise" {
repo_, sha_, ok := bitrise.ParseLookup(name)
if ok {
repo = repo_
sha = sha_
}
}
// current working directory (workspace)
workdir, err := os.Getwd()
if err != nil {
slog.Error("cannot get workdir", "error", err)
os.Exit(1)
}
// clone the plugin repository
clone := cloner.NewCache(cloner.NewDefault())
codedir, err := clone.Clone(ctx, repo, ref, sha)
if err != nil {
slog.Error("cannot clone the plugin", "error", err)
os.Exit(1)
}
outputFile := os.Getenv("DRONE_OUTPUT")
switch {
// execute harness plugin
case kind == "harness" || (kind == "" && harness.Is(codedir)):
slog.Info("detected harness plugin.yml")
execer := harness.Execer{
Source: codedir,
Workdir: workdir,
Ref: ref,
Environ: os.Environ(),
Stdout: os.Stdout,
Stderr: os.Stderr,
DownloadOnly: downloadOnly,
}
if err := execer.Exec(ctx); err != nil {
slog.Error("step failed", "error", err)
os.Exit(1)
}
// execute bitrise plugin
case kind == "bitrise" || (kind == "" && bitrise.Is(codedir)):
slog.Info("detected bitrise step.yml")
execer := bitrise.Execer{
Source: codedir,
Workdir: workdir,
Stdout: os.Stdout,
Stderr: os.Stderr,
Environ: bitrise.Environ(
os.Environ(),
),
OutputFile: outputFile,
}
if err := execer.Exec(ctx); err != nil {
slog.Error("step failed", "error", err)
os.Exit(1)
}
case github.Is(codedir) || kind == "action":
slog.Info("detected github action action.yml")
execer := github.Execer{
Name: name,
Source: codedir,
Stdout: os.Stdout,
Stderr: os.Stderr,
Environ: github.Environ(os.Environ()),
OutputFile: outputFile,
}
if err := execer.Exec(ctx); err != nil {
slog.Error("action step failed", "error", err)
os.Exit(1)
}
default:
slog.Info("unknown plugin type")
os.Exit(1)
}
}