diff --git a/internal/boxcli/global.go b/internal/boxcli/global.go index 432908996ae..ecb941f98b0 100644 --- a/internal/boxcli/global.go +++ b/internal/boxcli/global.go @@ -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 } diff --git a/internal/boxcli/init.go b/internal/boxcli/init.go index a203217c375..f2065562c3a 100644 --- a/internal/boxcli/init.go +++ b/internal/boxcli/init.go @@ -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" ) @@ -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 }, } diff --git a/internal/devbox/devbox.go b/internal/devbox/devbox.go index 557f6de7ebe..d36d74c5036 100644 --- a/internal/devbox/devbox.go +++ b/internal/devbox/devbox.go @@ -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 diff --git a/internal/devbox/util.go b/internal/devbox/util.go index c01a85812f7..1a59f00b235 100644 --- a/internal/devbox/util.go +++ b/internal/devbox/util.go @@ -55,7 +55,7 @@ func ensureDevboxUtilityConfig() (string, error) { return "", err } - err = InitConfig(path) + err = EnsureConfig(path) if err != nil { return "", err } diff --git a/internal/devconfig/init.go b/internal/devconfig/init.go index df7cc0cdfd1..24dd70fa67c 100644 --- a/internal/devconfig/init.go +++ b/internal/devconfig/init.go @@ -4,7 +4,6 @@ package devconfig import ( - "errors" "os" "path/filepath" @@ -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 } diff --git a/testscripts/init/empty.test.txt b/testscripts/init/empty.test.txt index 706ce8443bf..74f9b0ce00a 100644 --- a/testscripts/init/empty.test.txt +++ b/testscripts/init/empty.test.txt @@ -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 -- {