-
Notifications
You must be signed in to change notification settings - Fork 309
Expand file tree
/
Copy pathinit.go
More file actions
78 lines (67 loc) · 2.07 KB
/
init.go
File metadata and controls
78 lines (67 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2024 Jetify Inc. and contributors. All rights reserved.
// Use of this source code is governed by the license in the LICENSE file.
package boxcli
import (
"fmt"
"os"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.jetify.com/devbox/internal/devbox"
"go.jetify.com/devbox/internal/ux"
"go.jetify.com/devbox/pkg/autodetect"
)
type initFlags struct {
auto bool
dryRun bool
}
func initCmd() *cobra.Command {
flags := &initFlags{}
command := &cobra.Command{
Use: "init [<dir>]",
Short: "Initialize a directory as a devbox project",
Long: "Initialize a directory as a devbox project. " +
"This will create an empty devbox.json in the current directory. " +
"You can then add packages using `devbox add`",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
err := runInitCmd(cmd, args, flags)
path := pathArg(args)
if path == "" || path == "." {
path, _ = os.Getwd()
}
if errors.Is(err, os.ErrExist) {
ux.Fwarningf(cmd.ErrOrStderr(), "devbox.json already exists in %q.", path)
return nil
}
if err != nil {
return err
}
if flags.dryRun {
return nil
}
fmt.Fprintf(cmd.OutOrStdout(), "Created devbox.json in %s\n", path)
fmt.Fprintln(cmd.OutOrStdout(), "Run `devbox add <package>` to add packages, or `devbox shell` to start a dev shell.")
return nil
},
}
command.Flags().BoolVar(&flags.auto, "auto", false, "Automatically detect packages to add")
command.Flags().BoolVar(&flags.dryRun, "dry-run", false, "Dry run for auto mode. Prints the config that would be used")
_ = command.Flags().MarkHidden("auto")
_ = command.Flags().MarkHidden("dry-run")
return command
}
func runInitCmd(cmd *cobra.Command, args []string, flags *initFlags) error {
path := pathArg(args)
if flags.auto {
if flags.dryRun {
config, err := autodetect.DryRun(cmd.Context(), path)
if err != nil {
return errors.WithStack(err)
}
fmt.Fprintln(cmd.OutOrStdout(), string(config))
return nil
}
return autodetect.InitConfig(cmd.Context(), path)
}
return devbox.InitConfig(path)
}