Skip to content

Commit 750bfc1

Browse files
authored
[zeroconfig] Change autodetect implementation so it generates devbox.json (#2331)
## Summary closes DEV-152 instead of installing packages, this only generates the devbox.json making it easier to use and test and much faster. ## How was it tested? `devbox init --auto --dry-run`
1 parent a306f27 commit 750bfc1

File tree

6 files changed

+56
-56
lines changed

6 files changed

+56
-56
lines changed

internal/boxcli/init.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package boxcli
55

66
import (
7+
"fmt"
8+
79
"github.com/pkg/errors"
810
"github.com/spf13/cobra"
911

@@ -31,7 +33,7 @@ func initCmd() *cobra.Command {
3133
}
3234

3335
command.Flags().BoolVar(&flags.auto, "auto", false, "Automatically detect packages to add")
34-
command.Flags().BoolVar(&flags.dryRun, "dry-run", false, "Dry run")
36+
command.Flags().BoolVar(&flags.dryRun, "dry-run", false, "Dry run for auto mode. Prints the config that would be used")
3537
_ = command.Flags().MarkHidden("auto")
3638
_ = command.Flags().MarkHidden("dry-run")
3739

@@ -41,18 +43,17 @@ func initCmd() *cobra.Command {
4143
func runInitCmd(cmd *cobra.Command, args []string, flags *initFlags) error {
4244
path := pathArg(args)
4345

44-
if flags.auto && flags.dryRun {
45-
return autodetect.DryRun(cmd.Context(), path, cmd.ErrOrStderr())
46-
}
47-
48-
err := devbox.InitConfig(path)
49-
if err != nil {
50-
return errors.WithStack(err)
51-
}
52-
5346
if flags.auto {
54-
err = autodetect.PopulateConfig(cmd.Context(), path, cmd.ErrOrStderr())
47+
if flags.dryRun {
48+
config, err := autodetect.DryRun(cmd.Context(), path)
49+
if err != nil {
50+
return errors.WithStack(err)
51+
}
52+
fmt.Fprintln(cmd.OutOrStdout(), string(config))
53+
return nil
54+
}
55+
return autodetect.InitConfig(cmd.Context(), path)
5556
}
5657

57-
return errors.WithStack(err)
58+
return devbox.InitConfig(path)
5859
}

internal/devbox/devbox.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ type Devbox struct {
7272
var legacyPackagesWarningHasBeenShown = false
7373

7474
func InitConfig(dir string) error {
75-
return devconfig.Init(dir)
75+
_, err := devconfig.Init(dir)
76+
return err
7677
}
7778

7879
func Open(opts *devopt.Opts) (*Devbox, error) {

internal/devbox/devbox_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func TestComputeDevboxPathWhenRemoving(t *testing.T) {
122122

123123
func devboxForTesting(t *testing.T) *Devbox {
124124
path := t.TempDir()
125-
err := devconfig.Init(path)
125+
_, err := devconfig.Init(path)
126126
require.NoError(t, err, "InitConfig should not fail")
127127
d, err := Open(&devopt.Opts{
128128
Dir: path,

internal/devconfig/config_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
func TestOpen(t *testing.T) {
1717
t.Run("Dir", func(t *testing.T) {
1818
root, _, _ := mkNestedDirs(t)
19-
if err := Init(root); err != nil {
19+
if _, err := Init(root); err != nil {
2020
t.Fatalf("Init(%q) error: %v", root, err)
2121
}
2222

@@ -31,7 +31,7 @@ func TestOpen(t *testing.T) {
3131
})
3232
t.Run("File", func(t *testing.T) {
3333
root, _, _ := mkNestedDirs(t)
34-
if err := Init(root); err != nil {
34+
if _, err := Init(root); err != nil {
3535
t.Fatalf("Init(%q) error: %v", root, err)
3636
}
3737
path := filepath.Join(root, "devbox.json")
@@ -50,7 +50,7 @@ func TestOpen(t *testing.T) {
5050
func TestOpenError(t *testing.T) {
5151
t.Run("NotExist", func(t *testing.T) {
5252
root, _, _ := mkNestedDirs(t)
53-
if err := Init(root); err != nil {
53+
if _, err := Init(root); err != nil {
5454
t.Fatalf("Init(%q) error: %v", root, err)
5555
}
5656

@@ -79,7 +79,7 @@ func TestOpenError(t *testing.T) {
7979
})
8080
t.Run("ParentNotFound", func(t *testing.T) {
8181
root, child, _ := mkNestedDirs(t)
82-
if err := Init(root); err != nil {
82+
if _, err := Init(root); err != nil {
8383
t.Fatalf("Init(%q) error: %v", root, err)
8484
}
8585

@@ -96,10 +96,10 @@ func TestOpenError(t *testing.T) {
9696
func TestFind(t *testing.T) {
9797
t.Run("StartInSameDir", func(t *testing.T) {
9898
root, child, _ := mkNestedDirs(t)
99-
if err := Init(root); err != nil {
99+
if _, err := Init(root); err != nil {
100100
t.Fatalf("Init(%q) error: %v", root, err)
101101
}
102-
if err := Init(child); err != nil {
102+
if _, err := Init(child); err != nil {
103103
t.Fatalf("Init(%q) error: %v", child, err)
104104
}
105105

@@ -114,7 +114,7 @@ func TestFind(t *testing.T) {
114114
})
115115
t.Run("StartInChildDir", func(t *testing.T) {
116116
root, child, _ := mkNestedDirs(t)
117-
if err := Init(root); err != nil {
117+
if _, err := Init(root); err != nil {
118118
t.Fatalf("Init(%q) error: %v", root, err)
119119
}
120120

@@ -129,10 +129,10 @@ func TestFind(t *testing.T) {
129129
})
130130
t.Run("StartInNestedChildDir", func(t *testing.T) {
131131
root, child, nested := mkNestedDirs(t)
132-
if err := Init(root); err != nil {
132+
if _, err := Init(root); err != nil {
133133
t.Fatalf("Init(%q) error: %v", root, err)
134134
}
135-
if err := Init(child); err != nil {
135+
if _, err := Init(child); err != nil {
136136
t.Fatalf("Init(%q) error: %v", child, err)
137137
}
138138

@@ -147,7 +147,7 @@ func TestFind(t *testing.T) {
147147
})
148148
t.Run("IgnoreDirsWithMatchingName", func(t *testing.T) {
149149
root, child, _ := mkNestedDirs(t)
150-
if err := Init(root); err != nil {
150+
if _, err := Init(root); err != nil {
151151
t.Fatalf("Init(%q) error: %v", root, err)
152152
}
153153

@@ -171,7 +171,7 @@ func TestFind(t *testing.T) {
171171
})
172172
t.Run("ExactFile", func(t *testing.T) {
173173
root, _, _ := mkNestedDirs(t)
174-
if err := Init(root); err != nil {
174+
if _, err := Init(root); err != nil {
175175
t.Fatalf("Init(%q) error: %v", root, err)
176176
}
177177

@@ -189,7 +189,7 @@ func TestFind(t *testing.T) {
189189
func TestFindError(t *testing.T) {
190190
t.Run("NotExist", func(t *testing.T) {
191191
root, _, _ := mkNestedDirs(t)
192-
if err := Init(root); err != nil {
192+
if _, err := Init(root); err != nil {
193193
t.Fatalf("Init(%q) error: %v", root, err)
194194
}
195195

@@ -207,7 +207,7 @@ func TestFindError(t *testing.T) {
207207
})
208208
t.Run("NotFound", func(t *testing.T) {
209209
root, child, _ := mkNestedDirs(t)
210-
if err := Init(child); err != nil {
210+
if _, err := Init(child); err != nil {
211211
t.Fatalf("Init(%q) error: %v", root, err)
212212
}
213213

@@ -221,10 +221,10 @@ func TestFindError(t *testing.T) {
221221
})
222222
t.Run("Permissions", func(t *testing.T) {
223223
root, child, _ := mkNestedDirs(t)
224-
if err := Init(root); err != nil {
224+
if _, err := Init(root); err != nil {
225225
t.Fatalf("Init(%q) error: %v", root, err)
226226
}
227-
if err := Init(child); err != nil {
227+
if _, err := Init(child); err != nil {
228228
t.Fatalf("Init(%q) error: %v", child, err)
229229
}
230230
path := filepath.Join(child, "devbox.json")
@@ -260,7 +260,7 @@ func TestFindError(t *testing.T) {
260260
})
261261
t.Run("ExactFilePermissions", func(t *testing.T) {
262262
root, _, _ := mkNestedDirs(t)
263-
if err := Init(root); err != nil {
263+
if _, err := Init(root); err != nil {
264264
t.Fatalf("Init(%q) error: %v", root, err)
265265
}
266266
path := filepath.Join(root, "devbox.json")

internal/devconfig/init.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,31 @@ import (
1111
"go.jetpack.io/devbox/internal/devconfig/configfile"
1212
)
1313

14-
func Init(dir string) error {
14+
func Init(dir string) (*Config, error) {
1515
file, err := os.OpenFile(
1616
filepath.Join(dir, configfile.DefaultName),
1717
os.O_RDWR|os.O_CREATE|os.O_EXCL,
1818
0o644,
1919
)
2020
if errors.Is(err, os.ErrExist) {
21-
return nil
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
2224
}
2325
if err != nil {
24-
return err
26+
return nil, err
2527
}
2628
defer func() {
2729
if err != nil {
2830
os.Remove(file.Name())
2931
}
3032
}()
3133

32-
_, err = file.Write(DefaultConfig().Root.Bytes())
34+
newConfig := DefaultConfig()
35+
_, err = file.Write(newConfig.Root.Bytes())
36+
defer file.Close()
3337
if err != nil {
34-
file.Close()
35-
return err
38+
return nil, err
3639
}
37-
return file.Close()
40+
return newConfig, nil
3841
}

pkg/autodetect/autodetect.go

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,35 @@ package autodetect
22

33
import (
44
"context"
5-
"fmt"
6-
"io"
75

8-
"go.jetpack.io/devbox/internal/devbox"
9-
"go.jetpack.io/devbox/internal/devbox/devopt"
6+
"go.jetpack.io/devbox/internal/devconfig"
107
"go.jetpack.io/devbox/pkg/autodetect/detector"
118
)
129

13-
func PopulateConfig(ctx context.Context, path string, stderr io.Writer) error {
14-
pkgs, err := packages(ctx, path)
10+
func InitConfig(ctx context.Context, path string) error {
11+
config, err := devconfig.Init(path)
1512
if err != nil {
1613
return err
1714
}
18-
devbox, err := devbox.Open(&devopt.Opts{
19-
Dir: path,
20-
Stderr: stderr,
21-
})
22-
if err != nil {
23-
return err
15+
16+
return populateConfig(ctx, path, config)
17+
}
18+
19+
func DryRun(ctx context.Context, path string) ([]byte, error) {
20+
config := devconfig.DefaultConfig()
21+
if err := populateConfig(ctx, path, config); err != nil {
22+
return nil, err
2423
}
25-
return devbox.Add(ctx, pkgs, devopt.AddOpts{})
24+
return config.Root.Bytes(), nil
2625
}
2726

28-
func DryRun(ctx context.Context, path string, stderr io.Writer) error {
27+
func populateConfig(ctx context.Context, path string, config *devconfig.Config) error {
2928
pkgs, err := packages(ctx, path)
3029
if err != nil {
3130
return err
32-
} else if len(pkgs) == 0 {
33-
fmt.Fprintln(stderr, "No packages to add")
34-
return nil
3531
}
36-
fmt.Fprintln(stderr, "Packages to add:")
3732
for _, pkg := range pkgs {
38-
fmt.Fprintln(stderr, pkg)
33+
config.PackageMutator().Add(pkg)
3934
}
4035
return nil
4136
}

0 commit comments

Comments
 (0)