Skip to content

Commit ae834c6

Browse files
committed
[init] init config should return error if already exists
1 parent 9cb6297 commit ae834c6

File tree

11 files changed

+26
-34
lines changed

11 files changed

+26
-34
lines changed

internal/boxcli/global.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package boxcli
55

66
import (
77
"fmt"
8+
"os"
89

910
"github.com/pkg/errors"
1011
"github.com/spf13/cobra"
@@ -63,8 +64,8 @@ func ensureGlobalConfig() (string, error) {
6364
if err != nil {
6465
return "", err
6566
}
66-
err = devbox.InitConfig(globalConfigPath)
67-
if err != nil {
67+
err = devbox.EnsureConfig(globalConfigPath)
68+
if err != nil && !errors.Is(err, os.ErrExist) {
6869
return "", err
6970
}
7071
return globalConfigPath, nil

internal/boxcli/init.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ package boxcli
55

66
import (
77
"fmt"
8+
"os"
89

910
"github.com/pkg/errors"
1011
"github.com/spf13/cobra"
1112

13+
"go.jetpack.io/devbox/internal/boxcli/usererr"
1214
"go.jetpack.io/devbox/internal/devbox"
1315
"go.jetpack.io/devbox/pkg/autodetect"
1416
)
@@ -28,7 +30,15 @@ func initCmd() *cobra.Command {
2830
"You can then add packages using `devbox add`",
2931
Args: cobra.MaximumNArgs(1),
3032
RunE: func(cmd *cobra.Command, args []string) error {
31-
return runInitCmd(cmd, args, flags)
33+
err := runInitCmd(cmd, args, flags)
34+
if errors.Is(err, os.ErrExist) {
35+
path := pathArg(args)
36+
if path == "" || path == "." {
37+
path, _ = os.Getwd()
38+
}
39+
return usererr.New("devbox.json already exists in %q.", path)
40+
}
41+
return err
3242
},
3343
}
3444

internal/devbox/devbox.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ func InitConfig(dir string) error {
7676
return err
7777
}
7878

79+
func EnsureConfig(dir string) error {
80+
err := InitConfig(dir)
81+
if err != nil && !errors.Is(err, os.ErrExist) {
82+
return err
83+
}
84+
return nil
85+
}
86+
7987
func Open(opts *devopt.Opts) (*Devbox, error) {
8088
var cfg *devconfig.Config
8189
var err error

internal/devbox/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func ensureDevboxUtilityConfig() (string, error) {
5555
return "", err
5656
}
5757

58-
err = InitConfig(path)
58+
err = EnsureConfig(path)
5959
if err != nil {
6060
return "", err
6161
}

internal/devconfig/init.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package devconfig
55

66
import (
7-
"errors"
87
"os"
98
"path/filepath"
109

@@ -17,11 +16,6 @@ func Init(dir string) (*Config, error) {
1716
os.O_RDWR|os.O_CREATE|os.O_EXCL,
1817
0o644,
1918
)
20-
if errors.Is(err, os.ErrExist) {
21-
// TODO: Should we return an error here?
22-
// If we do, it breaks a bunch of tests, but it's likely the correct behavior
23-
return nil, nil
24-
}
2519
if err != nil {
2620
return nil, err
2721
}

testscripts/add/add.test.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ exec rg --version
2020
exec vim --version
2121
json.superset devbox.json expected_devbox2.json
2222

23-
-- devbox.json --
24-
{
25-
"packages": [
26-
]
27-
}
28-
2923
-- expected_devbox1.json --
3024
{
3125
"packages": [

testscripts/add/add_outputs.test.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ exec devbox run -- prometheus --version
1515

1616

1717

18-
-- devbox.json --
19-
{
20-
"packages": [
21-
]
22-
}
23-
2418
-- expected_devbox.json --
2519
{
2620
"packages": {

testscripts/add/add_replace.test.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Testscript for exercising adding packages
22

3-
exec devbox init
4-
53
exec devbox add [email protected]
64
devboxjson.packages.contains devbox.json [email protected]
75
! devboxjson.packages.contains devbox.json [email protected]

testscripts/add/global_add.test.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,3 @@ exec devbox global shellenv --recompute
88
source.path
99
exec rg --version
1010
exec vim --version
11-
12-
-- devbox.json --
13-
{
14-
"packages": [
15-
]
16-
}

testscripts/info/info.test.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ exec devbox init
22
exec devbox info hello
33
stdout 'hello '
44

5-
exec devbox init
65
exec devbox info hello@latest
76
stdout 'hello '
87

9-
exec devbox init
108
! exec devbox info notapackage
119
stderr 'Package "notapackage" not found'

0 commit comments

Comments
 (0)