Skip to content

Commit b2b49f9

Browse files
authored
[feat]: [CI-17150]: plugin changes to support binary sources as argum… (#42)
* [feat]: [CI-17150]: plugin changes to support binary sources as arguments * [feat]: [CI-17150]: spacing * [feat]: [CI-17150]: fix * [feat]: [CI-17150]: issue fix * [feat]: [CI-17150]: uts * [feat]: [CI-17150]: uts
1 parent 325bc01 commit b2b49f9

File tree

6 files changed

+461
-61
lines changed

6 files changed

+461
-61
lines changed

main.go

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@ import (
1515
"github.com/drone/plugin/plugin/bitrise"
1616
"github.com/drone/plugin/plugin/github"
1717
"github.com/drone/plugin/plugin/harness"
18+
"github.com/drone/plugin/utils"
1819
)
1920

2021
var (
21-
name string // plugin name
22-
repo string // plugin repository
23-
ref string // plugin repository reference
24-
sha string // plugin repository commit
25-
kind string // plugin kind (action, bitrise, harness)
26-
downloadOnly bool // plugin won't be executed on setting this flag. Only source will be downloaded. Used for caching the plugin dependencies
22+
name string // plugin name
23+
repo string // plugin repository
24+
ref string // plugin repository reference
25+
sha string // plugin repository commit
26+
kind string // plugin kind (action, bitrise, harness)
27+
downloadOnly bool // plugin won't be executed on setting this flag. Only source will be downloaded. Used for caching the plugin dependencies
28+
disableClone bool // plugin does not clone when this flag is enabled
29+
binarySources utils.CustomStringSliceFlag // plugin uses these binary source urls in the same order to download the binaires
2730
)
2831

2932
func main() {
@@ -43,6 +46,8 @@ func main() {
4346
flag.StringVar(&sha, "sha", "", "plugin commit")
4447
flag.StringVar(&kind, "kind", "", "plugin kind")
4548
flag.BoolVar(&downloadOnly, "download-only", false, "plugin downloadOnly")
49+
flag.BoolVar(&disableClone, "disable-clone", false, "disable clone functionality")
50+
flag.Var(&binarySources, "sources", "source urls to download binaries")
4651
flag.Parse()
4752

4853
// the user may specific the action plugin alias instead
@@ -87,27 +92,36 @@ func main() {
8792
}
8893

8994
// clone the plugin repository
90-
clone := cloner.NewCache(cloner.NewDefault())
91-
codedir, err := clone.Clone(ctx, repo, ref, sha)
92-
if err != nil {
93-
slog.Error("cannot clone the plugin", "error", err)
94-
os.Exit(1)
95+
var codedir string
96+
if !disableClone {
97+
clone := cloner.NewCache(cloner.NewDefault())
98+
codedir, err = clone.Clone(ctx, repo, ref, sha)
99+
if err != nil {
100+
slog.Error("cannot clone the plugin", "error", err)
101+
os.Exit(1)
102+
}
95103
}
96104

97105
outputFile := os.Getenv("DRONE_OUTPUT")
98106

99107
switch {
100108
// execute harness plugin
101109
case kind == "harness" || (kind == "" && harness.Is(codedir)):
102-
slog.Info("detected harness plugin.yml")
110+
if !disableClone {
111+
slog.Info("detected harness plugin.yml")
112+
} else {
113+
slog.Info("clone is disabled for harness plugin")
114+
}
103115
execer := harness.Execer{
104-
Source: codedir,
105-
Workdir: workdir,
106-
Ref: ref,
107-
Environ: os.Environ(),
108-
Stdout: os.Stdout,
109-
Stderr: os.Stderr,
110-
DownloadOnly: downloadOnly,
116+
Source: codedir,
117+
Workdir: workdir,
118+
Ref: ref,
119+
Environ: os.Environ(),
120+
Stdout: os.Stdout,
121+
Stderr: os.Stderr,
122+
BinarySources: binarySources,
123+
DisableClone: disableClone,
124+
DownloadOnly: downloadOnly,
111125
}
112126
if err := execer.Exec(ctx); err != nil {
113127
slog.Error("step failed", "error", err)

main_test.go

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"os"
6+
"testing"
7+
8+
"github.com/drone/plugin/utils"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
// Save original command line arguments and flags
13+
func saveOriginalFlags() []string {
14+
oldArgs := make([]string, len(os.Args))
15+
copy(oldArgs, os.Args)
16+
return oldArgs
17+
}
18+
19+
// Restore original command line arguments and flags
20+
func restoreOriginalFlags(oldArgs []string) {
21+
os.Args = make([]string, len(oldArgs))
22+
copy(os.Args, oldArgs)
23+
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
24+
}
25+
26+
func TestFlagParsing(t *testing.T) {
27+
// Save original flags
28+
oldArgs := saveOriginalFlags()
29+
defer restoreOriginalFlags(oldArgs)
30+
31+
tests := []struct {
32+
name string
33+
args []string
34+
expectedFlags struct {
35+
name string
36+
repo string
37+
ref string
38+
sha string
39+
kind string
40+
downloadOnly bool
41+
disableClone bool
42+
binarySources []string
43+
}
44+
}{
45+
{
46+
name: "all flags set",
47+
args: []string{
48+
"cmd",
49+
"-name", "test-plugin",
50+
"-repo", "github.com/test/repo",
51+
"-ref", "main",
52+
"-sha", "abc123",
53+
"-kind", "harness",
54+
"-download-only",
55+
"-disable-clone",
56+
"-sources", "source1;source2;source3",
57+
},
58+
expectedFlags: struct {
59+
name string
60+
repo string
61+
ref string
62+
sha string
63+
kind string
64+
downloadOnly bool
65+
disableClone bool
66+
binarySources []string
67+
}{
68+
name: "test-plugin",
69+
repo: "github.com/test/repo",
70+
ref: "main",
71+
sha: "abc123",
72+
kind: "harness",
73+
downloadOnly: true,
74+
disableClone: true,
75+
binarySources: []string{"source1", "source2", "source3"},
76+
},
77+
},
78+
{
79+
name: "minimal flags",
80+
args: []string{
81+
"cmd",
82+
"-name", "minimal-plugin",
83+
"-repo", "github.com/test/minimal",
84+
},
85+
expectedFlags: struct {
86+
name string
87+
repo string
88+
ref string
89+
sha string
90+
kind string
91+
downloadOnly bool
92+
disableClone bool
93+
binarySources []string
94+
}{
95+
name: "minimal-plugin",
96+
repo: "github.com/test/minimal",
97+
ref: "",
98+
sha: "",
99+
kind: "",
100+
downloadOnly: false,
101+
disableClone: false,
102+
binarySources: []string{},
103+
},
104+
},
105+
{
106+
name: "multiple sources",
107+
args: []string{
108+
"cmd",
109+
"-name", "source-plugin",
110+
"-sources", "source1;source2",
111+
"-sources", "source3;source4",
112+
},
113+
expectedFlags: struct {
114+
name string
115+
repo string
116+
ref string
117+
sha string
118+
kind string
119+
downloadOnly bool
120+
disableClone bool
121+
binarySources []string
122+
}{
123+
name: "source-plugin",
124+
repo: "",
125+
ref: "",
126+
sha: "",
127+
kind: "",
128+
downloadOnly: false,
129+
disableClone: false,
130+
binarySources: []string{"source1", "source2", "source3", "source4"},
131+
},
132+
},
133+
}
134+
135+
for _, tt := range tests {
136+
t.Run(tt.name, func(t *testing.T) {
137+
// Reset flags for each test
138+
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
139+
os.Args = tt.args
140+
141+
// Reset global variables
142+
name = ""
143+
repo = ""
144+
ref = ""
145+
sha = ""
146+
kind = ""
147+
downloadOnly = false
148+
disableClone = false
149+
binarySources = utils.CustomStringSliceFlag{}
150+
151+
// Parse flags
152+
flag.StringVar(&name, "name", "", "plugin name")
153+
flag.StringVar(&repo, "repo", "", "plugin repository")
154+
flag.StringVar(&ref, "ref", "", "plugin reference")
155+
flag.StringVar(&sha, "sha", "", "plugin commit")
156+
flag.StringVar(&kind, "kind", "", "plugin kind")
157+
flag.BoolVar(&downloadOnly, "download-only", false, "plugin downloadOnly")
158+
flag.BoolVar(&disableClone, "disable-clone", false, "disable clone functionality")
159+
flag.Var(&binarySources, "sources", "source urls to download binaries")
160+
flag.Parse()
161+
162+
// Assert flag values
163+
assert.Equal(t, tt.expectedFlags.name, name)
164+
assert.Equal(t, tt.expectedFlags.repo, repo)
165+
assert.Equal(t, tt.expectedFlags.ref, ref)
166+
assert.Equal(t, tt.expectedFlags.sha, sha)
167+
assert.Equal(t, tt.expectedFlags.kind, kind)
168+
assert.Equal(t, tt.expectedFlags.downloadOnly, downloadOnly)
169+
assert.Equal(t, tt.expectedFlags.disableClone, disableClone)
170+
assert.Equal(t, tt.expectedFlags.binarySources, binarySources.GetValue())
171+
})
172+
}
173+
}

0 commit comments

Comments
 (0)