Skip to content

Commit

Permalink
[plugins] Allow multiple plugins per repo (#1285)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
mikeland73 authored Jul 19, 2023
1 parent 778c8d2 commit 50aa9bf
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion examples/plugins/github-with-revision/devbox.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
}
},
"include": [
"github:jetpack-io/devbox-plugin-example/6ea0ef9e12ab58dbbb145ca8f3a465611cb603a9"
"github:jetpack-io/devbox-plugin-example/6ea0ef9e12ab58dbbb145ca8f3a465611cb603a9#devbox"
]
}
3 changes: 2 additions & 1 deletion examples/plugins/github/devbox.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
}
},
"include": [
"github:jetpack-io/devbox-plugin-example"
"github:jetpack-io/devbox-plugin-example",
"github:jetpack-io/devbox-plugin-example#custom-name"
]
}
5 changes: 4 additions & 1 deletion examples/plugins/github/test.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#!/bin/bash

expected="I AM SET (new value)"
if [ "$MY_ENV_VAR" == "$expected" ]; then
custom_expected="I AM SET TO CUSTOM (new value)"
if [ "$MY_ENV_VAR" == "$expected" ] && [ "$MY_ENV_VAR_CUSTOM" == "$MY_ENV_VAR_CUSTOM" ]; then
echo "Success! MY_ENV_VAR is set to '$MY_ENV_VAR'"
echo "Success! MY_ENV_VAR_CUSTOM is set to '$MY_ENV_VAR_CUSTOM'"
else
echo "MY_ENV_VAR environment variable is not set to '$expected'"
echo "MY_ENV_VAR MY_ENV_VAR_CUSTOM variable is not set to '$MY_ENV_VAR_CUSTOM'"
exit 1
fi
10 changes: 1 addition & 9 deletions internal/plugin/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func getConfigIfAny(pkg Includable, projectDir string) (*config, error) {
case *devpkg.Package:
return getBuiltinPluginConfig(pkg, projectDir)
case *githubPlugin:
return getConfigFromGithub(pkg, projectDir)
return pkg.buildConfig(projectDir)
case *localPlugin:
content, err := os.ReadFile(pkg.path)
if err != nil && !os.IsNotExist(err) {
Expand Down Expand Up @@ -60,11 +60,3 @@ func getBuiltinPluginConfig(pkg Includable, projectDir string) (*config, error)
}
return nil, nil
}

func getConfigFromGithub(pkg *githubPlugin, projectDir string) (*config, error) {
content, err := pkg.FileContent("devbox.json")
if err != nil {
return nil, errors.WithStack(err)
}
return buildConfig(pkg, projectDir, string(content))
}
24 changes: 20 additions & 4 deletions internal/plugin/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"net/url"
"strings"

"github.com/pkg/errors"
"github.com/samber/lo"
"go.jetpack.io/devbox/internal/boxcli/usererr"
"go.jetpack.io/devbox/internal/cuecfg"
)
Expand All @@ -15,13 +17,17 @@ type githubPlugin struct {
org string
repo string
revision string
fragment string
}

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

parts := strings.Split(path, "/")

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

if len(parts) == 3 {
Expand Down Expand Up @@ -75,10 +82,19 @@ func (p *githubPlugin) FileContent(subpath string) ([]byte, error) {
if res.StatusCode != http.StatusOK {
return nil, usererr.New(
"failed to get plugin github:%s (Status code %d). \nPlease make sure a "+
"devbox.json file exists in the root of the repo.",
"[name].json or default.json file exists in the root of the repo.",
p.raw,
res.StatusCode,
)
}
return io.ReadAll(res.Body)
}

func (p *githubPlugin) buildConfig(projectDir string) (*config, error) {
configName, _ := lo.Coalesce(p.fragment, "default")
content, err := p.FileContent(configName + ".json")
if err != nil {
return nil, errors.WithStack(err)
}
return buildConfig(p, projectDir, string(content))
}

0 comments on commit 50aa9bf

Please sign in to comment.