@@ -16,65 +16,67 @@ import (
1616
1717 "github.com/drone/plugin/cache"
1818 "github.com/drone/plugin/plugin/internal/file"
19+ "github.com/drone/plugin/utils"
1920 "golang.org/x/exp/slog"
2021)
2122
2223// Execer executes a harness plugin.
2324type Execer struct {
24- Ref string // Git ref for source code
25- Source string // plugin source code directory
26- Workdir string // pipeline working directory (aka workspace)
27- DownloadOnly bool
28- Environ []string
29- Stdout io.Writer
30- Stderr io.Writer
25+ Ref string // Git ref for source code
26+ Source string // plugin source code directory
27+ Workdir string // pipeline working directory (aka workspace)
28+ DownloadOnly bool
29+ binarySources utils.CustomStringSliceFlag
30+ disableClone bool
31+ Environ []string
32+ Stdout io.Writer
33+ Stderr io.Writer
3134}
3235
3336// Exec executes a bitrise plugin.
3437func (e * Execer ) Exec (ctx context.Context ) error {
35- // parse the bitrise plugin yaml
36- out , err := parseFile (filepath .Join (e .Source , "plugin.yml" ))
37- if err != nil {
38- return err
39- }
38+ if ! e .disableClone {
39+ // parse the bitrise plugin yaml
40+ out , err := parseFile (filepath .Join (e .Source , "plugin.yml" ))
41+ if err != nil {
42+ return err
43+ }
4044
41- // install dependencies
42- if runtime .GOOS == "linux" {
43- e .installAptDeps (ctx , out .Deps .Apt .Packages , out .Deps .Apt .Sources )
44- } else if runtime .GOOS == "darwin" {
45- e .installBrewDeps (ctx , out .Deps .Brew )
46- } else if runtime .GOOS == "windows" {
47- e .installChocoDeps (ctx , out .Deps .Choco )
48- }
45+ // install dependencies
46+ if runtime .GOOS == "linux" {
47+ e .installAptDeps (ctx , out .Deps .Apt .Packages , out .Deps .Apt .Sources )
48+ } else if runtime .GOOS == "darwin" {
49+ e .installBrewDeps (ctx , out .Deps .Brew )
50+ } else if runtime .GOOS == "windows" {
51+ e .installChocoDeps (ctx , out .Deps .Choco )
52+ }
4953
50- if len (out .Deps .Run ) != 0 {
51- e .installRunScripts (ctx , out .Deps .Run )
52- }
54+ if len (out .Deps .Run ) != 0 {
55+ e .installRunScripts (ctx , out .Deps .Run )
56+ }
5357
54- // execute the plugin. thxe execution logic differs
55- // based on programming language.
56- if source := out .Run .Binary .Source ; source != "" {
57- return e .runSourceExecutable (ctx , out .Run .Binary .Source , out .Run .Binary .FallbackSource )
58- } else if module := out .Run .Go .Module ; module != "" {
59- return e .runGoExecutable (ctx , module )
58+ // execute the plugin. thxe execution logic differs
59+ // based on programming language.
60+ sources := e .getBinarySources (out .Run .Binary .Source , out .Run .Binary .FallbackSource )
61+ if len (sources ) > 0 {
62+ return e .runSourceExecutable (ctx , sources )
63+ } else if module := out .Run .Go .Module ; module != "" {
64+ return e .runGoExecutable (ctx , module )
65+ } else {
66+ return e .runShellExecutable (ctx , out )
67+ }
68+ } else if len (e .binarySources .GetValue ()) > 0 {
69+ return e .runSourceExecutable (ctx , e .binarySources .GetValue ())
6070 } else {
61- return e .runShellExecutable (ctx , out )
71+ slog .Error ("clone is disabled and binary sources are empty. Aborting" )
72+ return nil
6273 }
6374}
6475
65- func (e * Execer ) runSourceExecutable (ctx context.Context , source string , fallback string ) error {
66- binpath , err := e .downloadBinary ( source )
76+ func (e * Execer ) runSourceExecutable (ctx context.Context , sources [] string ) error {
77+ binpath , err := e .downloadBinaryFromSources ( sources )
6778 if err != nil {
68- slog .Info ("Primary source download failed. Retrying with fallback..." )
69-
70- if fallback != "" {
71- binpath , err = e .downloadBinary (fallback )
72- if err != nil {
73- return err
74- }
75- } else {
76- return err
77- }
79+ return err
7880 }
7981
8082 if e .DownloadOnly {
@@ -90,6 +92,22 @@ func (e *Execer) runSourceExecutable(ctx context.Context, source string, fallbac
9092 return runCmds (ctx , cmds , e .Environ , e .Workdir , e .Stdout , e .Stderr )
9193}
9294
95+ func (e * Execer ) downloadBinaryFromSources (sources []string ) (string , error ) {
96+ var err error
97+ var binpath string
98+ for _ , source := range sources {
99+ if source != "" {
100+ binpath , err = e .downloadBinary (source )
101+ if err == nil {
102+ return binpath , nil
103+ } else {
104+ slog .Info ("binary download failed moving on to next source" )
105+ }
106+ }
107+ }
108+ return "" , err
109+ }
110+
93111func (e * Execer ) downloadBinary (source string ) (string , error ) {
94112 parsedURL , err := NewMetadata (source , e .Ref ).Generate ()
95113 if err != nil {
@@ -266,6 +284,20 @@ func runCmds(ctx context.Context, cmds []*exec.Cmd, env []string, workdir string
266284 return nil
267285}
268286
287+ func (e * Execer ) getBinarySources (source string , fallback string ) []string {
288+ var sources []string
289+ if len (e .binarySources .GetValue ()) > 0 {
290+ sources = append (sources , e .binarySources .GetValue ()... )
291+ }
292+ if source != "" {
293+ sources = append (sources , source )
294+ }
295+ if fallback != "" {
296+ sources = append (sources , fallback )
297+ }
298+ return sources
299+ }
300+
269301// trace writes each command to stdout with the command wrapped in an xml
270302// tag so that it can be extracted and displayed in the logs.
271303func trace (ctx context.Context , cmd * exec.Cmd ) {
0 commit comments