Skip to content

Commit 50aa9bf

Browse files
authored
[plugins] Allow multiple plugins per repo (#1285)
## Summary This makes 2 changes: * Allow multiple plugins per repo using fragment syntax (similar to nixpkgs) * Rename default from `devbox.json` to `default.json` ## How was it tested? example tests
1 parent 778c8d2 commit 50aa9bf

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

examples/plugins/github-with-revision/devbox.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
}
1212
},
1313
"include": [
14-
"github:jetpack-io/devbox-plugin-example/6ea0ef9e12ab58dbbb145ca8f3a465611cb603a9"
14+
"github:jetpack-io/devbox-plugin-example/6ea0ef9e12ab58dbbb145ca8f3a465611cb603a9#devbox"
1515
]
1616
}

examples/plugins/github/devbox.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
}
1212
},
1313
"include": [
14-
"github:jetpack-io/devbox-plugin-example"
14+
"github:jetpack-io/devbox-plugin-example",
15+
"github:jetpack-io/devbox-plugin-example#custom-name"
1516
]
1617
}

examples/plugins/github/test.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#!/bin/bash
22

33
expected="I AM SET (new value)"
4-
if [ "$MY_ENV_VAR" == "$expected" ]; then
4+
custom_expected="I AM SET TO CUSTOM (new value)"
5+
if [ "$MY_ENV_VAR" == "$expected" ] && [ "$MY_ENV_VAR_CUSTOM" == "$MY_ENV_VAR_CUSTOM" ]; then
56
echo "Success! MY_ENV_VAR is set to '$MY_ENV_VAR'"
7+
echo "Success! MY_ENV_VAR_CUSTOM is set to '$MY_ENV_VAR_CUSTOM'"
68
else
79
echo "MY_ENV_VAR environment variable is not set to '$expected'"
10+
echo "MY_ENV_VAR MY_ENV_VAR_CUSTOM variable is not set to '$MY_ENV_VAR_CUSTOM'"
811
exit 1
912
fi

internal/plugin/files.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func getConfigIfAny(pkg Includable, projectDir string) (*config, error) {
1818
case *devpkg.Package:
1919
return getBuiltinPluginConfig(pkg, projectDir)
2020
case *githubPlugin:
21-
return getConfigFromGithub(pkg, projectDir)
21+
return pkg.buildConfig(projectDir)
2222
case *localPlugin:
2323
content, err := os.ReadFile(pkg.path)
2424
if err != nil && !os.IsNotExist(err) {
@@ -60,11 +60,3 @@ func getBuiltinPluginConfig(pkg Includable, projectDir string) (*config, error)
6060
}
6161
return nil, nil
6262
}
63-
64-
func getConfigFromGithub(pkg *githubPlugin, projectDir string) (*config, error) {
65-
content, err := pkg.FileContent("devbox.json")
66-
if err != nil {
67-
return nil, errors.WithStack(err)
68-
}
69-
return buildConfig(pkg, projectDir, string(content))
70-
}

internal/plugin/github.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"net/url"
77
"strings"
88

9+
"github.com/pkg/errors"
10+
"github.com/samber/lo"
911
"go.jetpack.io/devbox/internal/boxcli/usererr"
1012
"go.jetpack.io/devbox/internal/cuecfg"
1113
)
@@ -15,13 +17,17 @@ type githubPlugin struct {
1517
org string
1618
repo string
1719
revision string
20+
fragment string
1821
}
1922

2023
// newGithubPlugin returns a plugin that is hosted on github.
21-
// url is of the form org/repo
22-
// The repo must have a devbox.json file in the root of the repo.
24+
// url is of the form org/repo#name
25+
// The repo must have a [name].json in the root of the repo. If fragment is
26+
// not set, it defaults to "default"
2327
func newGithubPlugin(url string) (*githubPlugin, error) {
24-
parts := strings.Split(url, "/")
28+
path, fragment, _ := strings.Cut(url, "#")
29+
30+
parts := strings.Split(path, "/")
2531

2632
if len(parts) < 2 || len(parts) > 3 {
2733
return nil, usererr.New(
@@ -35,6 +41,7 @@ func newGithubPlugin(url string) (*githubPlugin, error) {
3541
org: parts[0],
3642
repo: parts[1],
3743
revision: "master",
44+
fragment: fragment,
3845
}
3946

4047
if len(parts) == 3 {
@@ -75,10 +82,19 @@ func (p *githubPlugin) FileContent(subpath string) ([]byte, error) {
7582
if res.StatusCode != http.StatusOK {
7683
return nil, usererr.New(
7784
"failed to get plugin github:%s (Status code %d). \nPlease make sure a "+
78-
"devbox.json file exists in the root of the repo.",
85+
"[name].json or default.json file exists in the root of the repo.",
7986
p.raw,
8087
res.StatusCode,
8188
)
8289
}
8390
return io.ReadAll(res.Body)
8491
}
92+
93+
func (p *githubPlugin) buildConfig(projectDir string) (*config, error) {
94+
configName, _ := lo.Coalesce(p.fragment, "default")
95+
content, err := p.FileContent(configName + ".json")
96+
if err != nil {
97+
return nil, errors.WithStack(err)
98+
}
99+
return buildConfig(p, projectDir, string(content))
100+
}

0 commit comments

Comments
 (0)