Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/boxcli/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func ensureGlobalConfig() (string, error) {
if err != nil {
return "", err
}
err = devbox.InitConfig(globalConfigPath)
err = devbox.EnsureConfig(globalConfigPath)
if err != nil {
return "", err
}
Expand Down
12 changes: 11 additions & 1 deletion internal/boxcli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ package boxcli

import (
"fmt"
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"go.jetpack.io/devbox/internal/boxcli/usererr"
"go.jetpack.io/devbox/internal/devbox"
"go.jetpack.io/devbox/pkg/autodetect"
)
Expand All @@ -28,7 +30,15 @@ func initCmd() *cobra.Command {
"You can then add packages using `devbox add`",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runInitCmd(cmd, args, flags)
err := runInitCmd(cmd, args, flags)
if errors.Is(err, os.ErrExist) {
path := pathArg(args)
if path == "" || path == "." {
path, _ = os.Getwd()
}
return usererr.New("devbox.json already exists in %q.", path)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we make switch this to a warning emitted instead and make it a non-error?
I don't think its that bad to run devbox init if a devbox.json already exists. Its useful for scripts.

}
return err
},
}

Expand Down
8 changes: 8 additions & 0 deletions internal/devbox/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ func InitConfig(dir string) error {
return err
}

func EnsureConfig(dir string) error {
err := InitConfig(dir)
if err != nil && !errors.Is(err, os.ErrExist) {
return err
}
return nil
}

func Open(opts *devopt.Opts) (*Devbox, error) {
var cfg *devconfig.Config
var err error
Expand Down
2 changes: 1 addition & 1 deletion internal/devbox/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func ensureDevboxUtilityConfig() (string, error) {
return "", err
}

err = InitConfig(path)
err = EnsureConfig(path)
if err != nil {
return "", err
}
Expand Down
6 changes: 0 additions & 6 deletions internal/devconfig/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package devconfig

import (
"errors"
"os"
"path/filepath"

Expand All @@ -17,11 +16,6 @@ func Init(dir string) (*Config, error) {
os.O_RDWR|os.O_CREATE|os.O_EXCL,
0o644,
)
if errors.Is(err, os.ErrExist) {
// TODO: Should we return an error here?
// If we do, it breaks a bunch of tests, but it's likely the correct behavior
return nil, nil
}
if err != nil {
return nil, err
}
Expand Down
6 changes: 0 additions & 6 deletions testscripts/add/add.test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ exec rg --version
exec vim --version
json.superset devbox.json expected_devbox2.json

-- devbox.json --
{
"packages": [
]
}

-- expected_devbox1.json --
{
"packages": [
Expand Down
6 changes: 0 additions & 6 deletions testscripts/add/add_outputs.test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ exec devbox run -- prometheus --version



-- devbox.json --
{
"packages": [
]
}

-- expected_devbox.json --
{
"packages": {
Expand Down
2 changes: 0 additions & 2 deletions testscripts/add/add_replace.test.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Testscript for exercising adding packages

exec devbox init

exec devbox add [email protected]
devboxjson.packages.contains devbox.json [email protected]
! devboxjson.packages.contains devbox.json [email protected]
Expand Down
6 changes: 0 additions & 6 deletions testscripts/add/global_add.test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,3 @@ exec devbox global shellenv --recompute
source.path
exec rg --version
exec vim --version

-- devbox.json --
{
"packages": [
]
}
2 changes: 0 additions & 2 deletions testscripts/info/info.test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ exec devbox init
exec devbox info hello
stdout 'hello '

exec devbox init
exec devbox info hello@latest
stdout 'hello '

exec devbox init
! exec devbox info notapackage
stderr 'Package "notapackage" not found'
5 changes: 3 additions & 2 deletions testscripts/init/empty.test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ exists devbox.json

json.superset devbox.json expected.json

# Second init should be a no-op.
exec devbox init
# Second init should be an error.
! exec devbox init
stderr 'devbox.json already exists in'

-- expected.json --
{
Expand Down