Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 12 additions & 1 deletion internal/boxcli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ package boxcli

import (
"fmt"
"os"

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

"go.jetpack.io/devbox/internal/devbox"
"go.jetpack.io/devbox/internal/ux"
"go.jetpack.io/devbox/pkg/autodetect"
)

Expand All @@ -28,7 +30,16 @@ 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()
}
ux.Fwarningf(cmd.ErrOrStderr(), "devbox.json already exists in %q.", path)
err = nil
}
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
3 changes: 2 additions & 1 deletion 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.
# Second init should be a no-op with a warning
exec devbox init
stderr 'devbox.json already exists in'

-- expected.json --
{
Expand Down