()
| 21 | } |
| 22 | |
| 23 | func initCmd() *cobra.Command { |
| 24 | flags := &initFlags{} |
| 25 | command := &cobra.Command{ |
| 26 | Use: "init [<dir>]", |
| 27 | Short: "Initialize a directory as a devbox project", |
| 28 | Long: "Initialize a directory as a devbox project. " + |
| 29 | "This will create an empty devbox.json in the current directory. " + |
| 30 | "You can then add packages using `devbox add`", |
| 31 | Args: cobra.MaximumNArgs(1), |
| 32 | RunE: func(cmd *cobra.Command, args []string) error { |
| 33 | err := runInitCmd(cmd, args, flags) |
| 34 | path := pathArg(args) |
| 35 | if path == "" || path == "." { |
| 36 | path, _ = os.Getwd() |
| 37 | } |
| 38 | if errors.Is(err, os.ErrExist) { |
| 39 | ux.Fwarningf(cmd.ErrOrStderr(), "devbox.json already exists in %q.", path) |
| 40 | return nil |
| 41 | } |
| 42 | if err != nil { |
| 43 | return err |
| 44 | } |
| 45 | if flags.dryRun { |
| 46 | return nil |
| 47 | } |
| 48 | fmt.Fprintf(cmd.OutOrStdout(), "Created devbox.json in %s\n", path) |
| 49 | fmt.Fprintln(cmd.OutOrStdout(), "Run `devbox add <package>` to add packages, or `devbox shell` to start a dev shell.") |
| 50 | return nil |
| 51 | }, |
| 52 | } |
| 53 | |
| 54 | command.Flags().BoolVar(&flags.auto, "auto", false, "Automatically detect packages to add") |
| 55 | command.Flags().BoolVar(&flags.dryRun, "dry-run", false, "Dry run for auto mode. Prints the config that would be used") |
| 56 | _ = command.Flags().MarkHidden("auto") |
| 57 | _ = command.Flags().MarkHidden("dry-run") |
| 58 | |
| 59 | return command |
| 60 | } |
| 61 | |
| 62 | func runInitCmd(cmd *cobra.Command, args []string, flags *initFlags) error { |
| 63 | path := pathArg(args) |
no test coverage detected