Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions boxcli/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,32 @@ import (
"go.jetpack.io/devbox"
)

type addCmdFlags struct {
config configFlags
}

func AddCmd() *cobra.Command {
flags := addCmdFlags{}

command := &cobra.Command{
Use: "add <pkg>...",
Short: "Add a new package to your devbox",
Args: cobra.MinimumNArgs(1),
PersistentPreRunE: nixShellPersistentPreRunE,
RunE: addCmdFunc(),
RunE: func(cmd *cobra.Command, args []string) error {
return addCmdFunc(cmd, args, flags)
},
}

flags.config.register(command)
return command
}

func addCmdFunc() runFunc {
return func(cmd *cobra.Command, args []string) error {
box, err := devbox.Open(".", os.Stdout)
if err != nil {
return errors.WithStack(err)
}

return box.Add(args...)
func addCmdFunc(_ *cobra.Command, args []string, flags addCmdFlags) error {
box, err := devbox.Open(flags.config.path, os.Stdout)
if err != nil {
return errors.WithStack(err)
}

return box.Add(args...)
}
27 changes: 26 additions & 1 deletion boxcli/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,40 @@
package boxcli

import (
"fmt"
"path/filepath"

"github.com/fatih/color"
"github.com/pkg/errors"
"go.jetpack.io/devbox/boxcli/usererr"
)

// Functions that help parse arguments

// If args empty, defaults to the current directory
// Otherwise grabs the path from the first argument
func configPathFromUser(args []string, flags *configFlags) (string, error) {

if flags.path != "" && len(args) > 0 {
return "", usererr.New(
"Cannot specify devbox.json's path via both --config and the command arguments. " +
"Please use --config only.",
)
}

if flags.path != "" {
return flags.path, nil
}

if len(args) > 0 {
fmt.Printf(
"%s devbox <command> <path> is deprecated, use devbox <command> --config <path> instead\n",
color.HiYellowString("Warning:"),
)
}
return pathArg(args), nil
}

func pathArg(args []string) string {
if len(args) > 0 {
p, err := filepath.Abs(args[0])
Expand All @@ -21,5 +46,5 @@ func pathArg(args []string) string {
}
return p
}
return "."
return ""
}
43 changes: 26 additions & 17 deletions boxcli/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,48 @@ import (
"go.jetpack.io/devbox/docker"
)

type buildCmdFlags struct {
config configFlags
docker docker.BuildFlags
}

func BuildCmd() *cobra.Command {
flags := &docker.BuildFlags{}
flags := buildCmdFlags{}

command := &cobra.Command{
Use: "build [<dir>]",
Use: "build",
Short: "Build an OCI image that can run as a container",
Long: "Builds your current source directory and devbox configuration as a Docker container. Devbox will create a plan for your container based on your source code, and then apply the packages and stage overrides in your devbox.json. \n To learn more about how to configure your builds, see the [configuration reference](/docs/configuration_reference)",
Args: cobra.MaximumNArgs(1),
RunE: buildCmdFunc(flags),
RunE: func(cmd *cobra.Command, args []string) error {
return buildCmdFunc(cmd, args, flags)
},
}

flags.config.register(command)
command.Flags().StringVar(
&flags.Name, "name", "devbox", "name for the container")
&flags.docker.Name, "name", "devbox", "name for the container")
command.Flags().BoolVar(
&flags.NoCache, "no-cache", false, "Do not use a cache")
&flags.docker.NoCache, "no-cache", false, "Do not use a cache")
command.Flags().StringVar(
&flags.Engine, "engine", "docker", "Engine used to build the container: 'docker', 'podman'")
&flags.docker.Engine, "engine", "docker", "Engine used to build the container: 'docker', 'podman'")
command.Flags().StringSliceVar(
&flags.Tags, "tags", []string{}, "tags for the container")
&flags.docker.Tags, "tags", []string{}, "tags for the container")

return command
}

func buildCmdFunc(flags *docker.BuildFlags) runFunc {
return func(cmd *cobra.Command, args []string) error {
path := pathArg(args)

// Check the directory exists.
box, err := devbox.Open(path, os.Stdout)
if err != nil {
return errors.WithStack(err)
}
func buildCmdFunc(_ *cobra.Command, args []string, flags buildCmdFlags) error {
path, err := configPathFromUser(args, &flags.config)
if err != nil {
return err
}

return box.Build(flags)
// Check the directory exists.
box, err := devbox.Open(path, os.Stdout)
if err != nil {
return errors.WithStack(err)
}

return box.Build(&flags.docker)
}
16 changes: 16 additions & 0 deletions boxcli/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package boxcli

import (
"github.com/spf13/cobra"
)

// to be composed into xyzCmdFlags structs
type configFlags struct {
path string
}

func (flags *configFlags) register(cmd *cobra.Command) {
cmd.Flags().StringVarP(
&flags.path, "config", "c", "", "path to directory containing a devbox.json config file",
)
}
22 changes: 18 additions & 4 deletions boxcli/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,32 @@ import (
"go.jetpack.io/devbox"
)

type generateCmdFlags struct {
config configFlags
}

func GenerateCmd() *cobra.Command {
flags := &generateCmdFlags{}

command := &cobra.Command{
Use: "generate [<dir>]",
Use: "generate",
Args: cobra.MaximumNArgs(1),
Hidden: true, // For debugging only
RunE: runGenerateCmd,
RunE: func(cmd *cobra.Command, args []string) error {
return runGenerateCmd(cmd, args, flags)
},
}

flags.config.register(command)

return command
}

func runGenerateCmd(cmd *cobra.Command, args []string) error {
path := pathArg(args)
func runGenerateCmd(_ *cobra.Command, args []string, flags *generateCmdFlags) error {
path, err := configPathFromUser(args, &flags.config)
if err != nil {
return err
}

// Check the directory exists.
box, err := devbox.Open(path, os.Stdout)
Expand Down
7 changes: 5 additions & 2 deletions boxcli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ func InitCmd() *cobra.Command {
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: runInitCmd,
RunE: func(cmd *cobra.Command, args []string) error {
return runInitCmd(cmd, args)
},
}

return command
}

func runInitCmd(cmd *cobra.Command, args []string) error {
func runInitCmd(_ *cobra.Command, args []string) error {
path := pathArg(args)

_, err := devbox.InitConfig(path)
Expand Down
21 changes: 17 additions & 4 deletions boxcli/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,31 @@ import (
"go.jetpack.io/devbox"
)

type planCmdFlags struct {
config configFlags
}

func PlanCmd() *cobra.Command {
flags := planCmdFlags{}

command := &cobra.Command{
Use: "plan [<dir>]",
Use: "plan",
Short: "Preview the plan used to build your environment",
Args: cobra.MaximumNArgs(1),
RunE: runPlanCmd,
RunE: func(cmd *cobra.Command, args []string) error {
return runPlanCmd(cmd, args, flags)
},
}

flags.config.register(command)
return command
}

func runPlanCmd(cmd *cobra.Command, args []string) error {
path := pathArg(args)
func runPlanCmd(_ *cobra.Command, args []string, flags planCmdFlags) error {
path, err := configPathFromUser(args, &flags.config)
if err != nil {
return err
}

// Check the directory exists.
box, err := devbox.Open(path, os.Stdout)
Expand Down
15 changes: 12 additions & 3 deletions boxcli/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,27 @@ import (
"go.jetpack.io/devbox"
)

type removeCmdFlags struct {
config configFlags
}

func RemoveCmd() *cobra.Command {
flags := removeCmdFlags{}
command := &cobra.Command{
Use: "rm <pkg>...",
Short: "Remove a package from your devbox",
Args: cobra.MinimumNArgs(1),
RunE: runRemoveCmd,
RunE: func(cmd *cobra.Command, args []string) error {
return runRemoveCmd(cmd, args, flags)
},
}

flags.config.register(command)
return command
}

func runRemoveCmd(cmd *cobra.Command, args []string) error {
box, err := devbox.Open(".", os.Stdout)
func runRemoveCmd(_ *cobra.Command, args []string, flags removeCmdFlags) error {
box, err := devbox.Open(flags.config.path, os.Stdout)
if err != nil {
return errors.WithStack(err)
}
Expand Down
2 changes: 0 additions & 2 deletions boxcli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,3 @@ func Main() {
code := Execute(context.Background(), os.Args[1:])
os.Exit(code)
}

type runFunc func(cmd *cobra.Command, args []string) error
Loading