Skip to content

Commit 88887fe

Browse files
authored
[devbox] rm, add, and build flags (#2473)
## Summary Implement a few improvements to the devbox CLI: - `add` command now removes duplicates - New `rm` command to remove a package - `build` now accepts a `--no-cache` flag ## How was it tested? Rebuilt and tested on example project ## Is this change backwards-compatible? Yes
1 parent 42d0436 commit 88887fe

File tree

11 files changed

+124
-34
lines changed

11 files changed

+124
-34
lines changed

BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ go_library(
2020
"//opensource/devbox/nix",
2121
"//opensource/devbox/planner",
2222
"@com_github_pkg_errors//:errors",
23+
"@com_github_samber_lo//:lo",
2324
],
2425
)

boxcli/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ go_library(
99
"generate.go",
1010
"init.go",
1111
"plan.go",
12+
"rm.go",
1213
"root.go",
1314
"shell.go",
1415
],
1516
importpath = "go.jetpack.io/axiom/opensource/devbox/boxcli",
1617
visibility = ["//visibility:public"],
1718
deps = [
1819
"//opensource/devbox",
20+
"//opensource/devbox/docker",
1921
"@com_github_pkg_errors//:errors",
2022
"@com_github_spf13_cobra//:cobra",
2123
],

boxcli/build.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,36 @@ import (
44
"github.com/pkg/errors"
55
"github.com/spf13/cobra"
66
"go.jetpack.io/axiom/opensource/devbox"
7+
"go.jetpack.io/axiom/opensource/devbox/docker"
78
)
89

910
func BuildCmd() *cobra.Command {
11+
flags := &docker.BuildFlags{}
12+
1013
command := &cobra.Command{
1114
Use: "build [<dir>]",
1215
Args: cobra.MaximumNArgs(1),
13-
RunE: runBuildCmd,
16+
RunE: buildCmdFunc(flags),
1417
}
18+
19+
command.Flags().BoolVar(
20+
&flags.NoCache, "no-cache", false, "Do not use a cache")
21+
1522
return command
1623
}
1724

18-
func runBuildCmd(cmd *cobra.Command, args []string) error {
19-
path := pathArg(args)
25+
type runFunc func(cmd *cobra.Command, args []string) error
2026

21-
// Check the directory exists.
22-
box, err := devbox.Open(path)
23-
if err != nil {
24-
return errors.WithStack(err)
25-
}
27+
func buildCmdFunc(flags *docker.BuildFlags) runFunc {
28+
return func(cmd *cobra.Command, args []string) error {
29+
path := pathArg(args)
30+
31+
// Check the directory exists.
32+
box, err := devbox.Open(path)
33+
if err != nil {
34+
return errors.WithStack(err)
35+
}
2636

27-
return box.Build()
37+
return box.Build(docker.WithFlags(flags))
38+
}
2839
}

boxcli/rm.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package boxcli
2+
3+
import (
4+
"github.com/pkg/errors"
5+
"github.com/spf13/cobra"
6+
"go.jetpack.io/axiom/opensource/devbox"
7+
)
8+
9+
func RemoveCmd() *cobra.Command {
10+
command := &cobra.Command{
11+
Use: "rm <pkg>...",
12+
Args: cobra.MinimumNArgs(1),
13+
RunE: runRemoveCmd,
14+
}
15+
return command
16+
}
17+
18+
func runRemoveCmd(cmd *cobra.Command, args []string) error {
19+
box, err := devbox.Open(".")
20+
if err != nil {
21+
return errors.WithStack(err)
22+
}
23+
24+
return box.Remove(args...)
25+
}

boxcli/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func RootCmd() *cobra.Command {
2222
command.AddCommand(BuildCmd())
2323
command.AddCommand(GenerateCmd())
2424
command.AddCommand(PlanCmd())
25+
command.AddCommand(RemoveCmd())
2526
command.AddCommand(InitCmd())
2627
command.AddCommand(ShellCmd())
2728
return command

devbox.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"path/filepath"
55

66
"github.com/pkg/errors"
7+
"github.com/samber/lo"
78
"go.jetpack.io/axiom/opensource/devbox/cuecfg"
89
"go.jetpack.io/axiom/opensource/devbox/docker"
910
"go.jetpack.io/axiom/opensource/devbox/nix"
@@ -38,25 +39,40 @@ func Open(dir string) (*Devbox, error) {
3839
}
3940

4041
func (d *Devbox) Add(pkgs ...string) error {
42+
// Check packages exist before adding.
4143
for _, pkg := range pkgs {
4244
ok := nix.PkgExists(pkg)
4345
if !ok {
4446
return errors.Errorf("Package %s not found.", pkg)
4547
}
4648
}
47-
// TODO: detect duplicates
48-
d.cfg.Packages = append(d.cfg.Packages, pkgs...)
49+
// Merge and remove duplicates:
50+
merged := append(d.cfg.Packages, pkgs...)
51+
d.cfg.Packages = lo.FindUniques(merged)
52+
53+
// Save config.
4954
return d.saveCfg()
5055
}
5156

52-
func (d *Devbox) Build() error {
57+
func (d *Devbox) Remove(pkgs ...string) error {
58+
// Remove packages from config.
59+
d.cfg.Packages = lo.Without(d.cfg.Packages, pkgs...)
60+
61+
// Save config.
62+
return d.saveCfg()
63+
}
64+
65+
func (d *Devbox) Build(opts ...docker.BuildOptions) error {
66+
defaultFlags := &docker.BuildFlags{
67+
Name: "devbox",
68+
}
69+
opts = append([]docker.BuildOptions{docker.WithFlags(defaultFlags)}, opts...)
70+
5371
err := d.Generate()
5472
if err != nil {
5573
return errors.WithStack(err)
5674
}
57-
return docker.Build(d.srcDir, docker.BuildOpts{
58-
Name: "devbox", // TODO: make it configurable.
59-
})
75+
return docker.Build(d.srcDir, opts...)
6076
}
6177

6278
func (d *Devbox) Plan() *planner.BuildPlan {

docker/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ go_library(
88
],
99
importpath = "go.jetpack.io/axiom/opensource/devbox/docker",
1010
visibility = ["//visibility:public"],
11+
deps = ["@com_github_imdario_mergo//:mergo"],
1112
)

docker/args.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,27 @@ import (
55
"strings"
66
)
77

8-
func ToArgs(args []string, opts BuildOpts) []string {
9-
if opts.Name != "" {
10-
args = append(args, "-t", opts.Name)
8+
func ToArgs(args []string, flags *BuildFlags) []string {
9+
if flags == nil {
10+
return args
11+
}
12+
if args == nil {
13+
args = []string{}
14+
}
15+
if flags.Name != "" {
16+
args = append(args, "-t", flags.Name)
1117

12-
for _, tag := range opts.Tags {
13-
args = append(args, "-t", fmt.Sprintf("%s:%s", opts.Name, tag))
18+
for _, tag := range flags.Tags {
19+
args = append(args, "-t", fmt.Sprintf("%s:%s", flags.Name, tag))
1420
}
1521
}
1622

17-
if len(opts.Platforms) > 0 {
18-
args = append(args, fmt.Sprintf("--platform=%s", strings.Join(opts.Platforms, ",")))
23+
if len(flags.Platforms) > 0 {
24+
args = append(args, fmt.Sprintf("--platform=%s", strings.Join(flags.Platforms, ",")))
25+
}
26+
27+
if flags.NoCache {
28+
args = append(args, "--no-cache")
1929
}
2030

2131
return args

docker/docker.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"os"
55
"os/exec"
66
"path/filepath"
7+
8+
"github.com/imdario/mergo"
79
)
810

911
// This package provides an API to build images using docker.
@@ -16,17 +18,38 @@ import (
1618
// + In the past we've had some trouble with docker go libraries, and dependency
1719
// management.
1820

19-
type BuildOpts struct {
21+
type BuildFlags struct {
2022
Name string
2123
Tags []string
2224
Platforms []string
25+
NoCache bool
26+
}
27+
28+
type BuildOptions func(*BuildFlags)
29+
30+
func WithFlags(src *BuildFlags) BuildOptions {
31+
return func(dst *BuildFlags) {
32+
err := mergo.Merge(dst, src, mergo.WithOverride)
33+
if err != nil {
34+
panic(err)
35+
}
36+
}
2337
}
2438

25-
func Build(path string, opts ...BuildOpts) error {
26-
args := []string{"build", "."}
27-
if len(opts) > 0 {
28-
args = ToArgs(args, opts[0])
39+
func WithoutCache() BuildOptions {
40+
return func(flags *BuildFlags) {
41+
flags.NoCache = true
2942
}
43+
}
44+
45+
func Build(path string, opts ...BuildOptions) error {
46+
flags := &BuildFlags{}
47+
for _, opt := range opts {
48+
opt(flags)
49+
}
50+
51+
args := []string{"buildx", "build", "."}
52+
args = ToArgs(args, flags)
3053

3154
dir, fileName := parsePath(path)
3255
if fileName != "" {

nix/nix.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ func PkgExists(pkg string) bool {
3535

3636
type Info struct {
3737
NixName string
38-
Name string
38+
Name string
3939
Version string
40-
System string
40+
System string
4141
}
4242

4343
func PkgInfo(pkg string) (*Info, bool) {
@@ -70,11 +70,11 @@ func parseInfo(pkg string, data []byte) *Info {
7070
for _, result := range results {
7171
pkgInfo := &Info{
7272
NixName: pkg,
73-
Name: result["pname"].(string),
73+
Name: result["pname"].(string),
7474
Version: result["version"].(string),
75-
System: result["system"].(string),
75+
System: result["system"].(string),
7676
}
7777
return pkgInfo
7878
}
7979
return nil
80-
}
80+
}

0 commit comments

Comments
 (0)