Skip to content

Commit dd41255

Browse files
authored
[plugins] Implements github plugins (#1281)
## Summary This implements github plugins. They use the following syntax in `devbox.json`: ```json "include": [ "github:jetpack-io/devbox-plugin-example/6ea0ef9e12ab58dbbb145ca8f3a465611cb603a9" ] ``` revision is optional and defaults to master/main. Repos must have `devbox.json` at top level. TODO: - [x] Better error message for missing devbox.json and other files. - [ ] Add to lockfile? The are currently locked using optional revision - [ ] Updates: They are currently don't update unless change is made that busts local.lock cache. ## How was it tested? <img width="781" alt="image" src="https://github.com/jetpack-io/devbox/assets/544948/abb258dd-ef78-45c6-8652-e0111635208a">
1 parent de300c3 commit dd41255

File tree

16 files changed

+223
-32
lines changed

16 files changed

+223
-32
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
some data
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"packages": [],
3+
"shell": {
4+
"init_hook": [
5+
"echo 'Welcome to devbox!' > /dev/null"
6+
],
7+
"scripts": {
8+
"run_test": [
9+
"./test.sh"
10+
]
11+
}
12+
},
13+
"include": [
14+
"github:jetpack-io/devbox-plugin-example/6ea0ef9e12ab58dbbb145ca8f3a465611cb603a9"
15+
]
16+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"lockfile_version": "1",
3+
"packages": {}
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
expected="I AM SET"
4+
if [ "$MY_ENV_VAR" == "$expected" ]; then
5+
echo "Success! MY_ENV_VAR is set to '$MY_ENV_VAR'"
6+
else
7+
echo "MY_ENV_VAR environment variable is not set to '$expected'"
8+
exit 1
9+
fi
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
some data
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"packages": [],
3+
"shell": {
4+
"init_hook": [
5+
"echo 'Welcome to devbox!' > /dev/null"
6+
],
7+
"scripts": {
8+
"run_test": [
9+
"./test.sh"
10+
]
11+
}
12+
},
13+
"include": [
14+
"github:jetpack-io/devbox-plugin-example"
15+
]
16+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"lockfile_version": "1",
3+
"packages": {}
4+
}

examples/plugins/github/test.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
expected="I AM SET (new value)"
4+
if [ "$MY_ENV_VAR" == "$expected" ]; then
5+
echo "Success! MY_ENV_VAR is set to '$MY_ENV_VAR'"
6+
else
7+
echo "MY_ENV_VAR environment variable is not set to '$expected'"
8+
exit 1
9+
fi

internal/boxcli/midcobra/debug.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ func (d *DebugMiddleware) postRun(cmd *cobra.Command, args []string, runErr erro
5656
if runErr == nil {
5757
return
5858
}
59-
if usererr.HasUserMessage(runErr) {
60-
if usererr.IsWarning(runErr) {
59+
if userErr, hasUserErr := usererr.Extract(runErr); hasUserErr {
60+
if usererr.IsWarning(userErr) {
6161
ux.Fwarning(cmd.ErrOrStderr(), runErr.Error())
6262
return
6363
}
64-
color.New(color.FgRed).Fprintf(cmd.ErrOrStderr(), "\nError: %s\n\n", runErr.Error())
64+
color.New(color.FgRed).Fprintf(cmd.ErrOrStderr(), "\nError: %s\n\n", userErr.Error())
6565
} else {
6666
color.New(color.FgRed).Fprintf(cmd.ErrOrStderr(), "Error: %v\n\n", runErr)
6767
}

internal/boxcli/usererr/usererr.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func NewWarning(msg string, args ...any) error {
5353
func WithUserMessage(source error, msg string, args ...any) error {
5454
// We don't want to wrap the error if it already has a user message. Doing
5555
// so would obscure the original error message which is likely more useful.
56-
if source == nil || HasUserMessage(source) {
56+
if source == nil || hasUserMessage(source) {
5757
return source
5858
}
5959
return &combined{
@@ -63,7 +63,7 @@ func WithUserMessage(source error, msg string, args ...any) error {
6363
}
6464

6565
func WithLoggedUserMessage(source error, msg string, args ...any) error {
66-
if source == nil || HasUserMessage(source) {
66+
if source == nil || hasUserMessage(source) {
6767
return source
6868
}
6969
return &combined{
@@ -73,9 +73,13 @@ func WithLoggedUserMessage(source error, msg string, args ...any) error {
7373
}
7474
}
7575

76-
func HasUserMessage(err error) bool {
76+
// Extract unwraps and returns the user error if it exists.
77+
func Extract(err error) (error, bool) { // nolint: revive
7778
c := &combined{}
78-
return errors.As(err, &c) // note double pointer
79+
if errors.As(err, &c) {
80+
return c, true
81+
}
82+
return nil, false
7983
}
8084

8185
// ShouldLogError returns true if the it's a logged user error or is a non-user error
@@ -126,3 +130,8 @@ func (c *combined) Format(s fmt.State, verb rune) {
126130
Format(s fmt.State, verb rune)
127131
}).Format(s, verb)
128132
}
133+
134+
func hasUserMessage(err error) bool {
135+
_, hasUserMessage := Extract(err)
136+
return hasUserMessage
137+
}

0 commit comments

Comments
 (0)