Skip to content

Commit 842fb5b

Browse files
committed
[Config Packages] in devbox add, allow multiple --platform/--exclude-platform flag values
1 parent 35c7cf2 commit 842fb5b

File tree

6 files changed

+99
-51
lines changed

6 files changed

+99
-51
lines changed

devbox.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type Devbox interface {
1919
// Add adds Nix packages to the config so that they're available in the devbox
2020
// environment. It validates that the Nix packages exist, and install them.
2121
// Adding duplicate packages is a no-op.
22-
Add(ctx context.Context, platform, excludePlatform string, pkgs ...string) error
22+
Add(ctx context.Context, platforms, excludePlatforms []string, pkgs ...string) error
2323
Config() *devconfig.Config
2424
ProjectDir() string
2525
// Generate creates the directory of Nix files and the Dockerfile that define

internal/boxcli/add.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import (
1818
const toSearchForPackages = "To search for packages, use the `devbox search` command"
1919

2020
type addCmdFlags struct {
21-
config configFlags
22-
allowInsecure bool
23-
platform string
24-
excludePlatform string
21+
config configFlags
22+
allowInsecure bool
23+
platforms []string
24+
excludePlatforms []string
2525
}
2626

2727
func addCmd() *cobra.Command {
@@ -53,11 +53,11 @@ func addCmd() *cobra.Command {
5353
command.Flags().BoolVar(
5454
&flags.allowInsecure, "allow-insecure", false,
5555
"allow adding packages marked as insecure.")
56-
command.Flags().StringVar(
57-
&flags.platform, "platform", "",
56+
command.Flags().StringSliceVarP(
57+
&flags.platforms, "platform", "p", []string{},
5858
"add packages to run on only this platform.")
59-
command.Flags().StringVar(
60-
&flags.excludePlatform, "exclude-platform", "",
59+
command.Flags().StringSliceVarP(
60+
&flags.excludePlatforms, "exclude-platform", "e", []string{},
6161
"exclude packages from a specific platform.")
6262

6363
return command
@@ -73,5 +73,5 @@ func addCmdFunc(cmd *cobra.Command, args []string, flags addCmdFlags) error {
7373
return errors.WithStack(err)
7474
}
7575

76-
return box.Add(cmd.Context(), flags.platform, flags.excludePlatform, args...)
76+
return box.Add(cmd.Context(), flags.platforms, flags.excludePlatforms, args...)
7777
}

internal/devconfig/packages.go

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/pkg/errors"
9+
"github.com/samber/lo"
910
orderedmap "github.com/wk8/go-ordered-map/v2"
1011
"go.jetpack.io/devbox/internal/nix"
1112
"go.jetpack.io/devbox/internal/searcher"
@@ -59,30 +60,28 @@ func (pkgs *Packages) Remove(versionedName string) {
5960
})
6061
}
6162

62-
// AddPlatform adds a platform to the list of platforms for a given package
63-
func (pkgs *Packages) AddPlatform(versionedname, platform string) error {
64-
if err := nix.EnsureValidPlatform(platform); err != nil {
65-
return errors.WithStack(err)
63+
// AddPlatforms adds a platform to the list of platforms for a given package
64+
func (pkgs *Packages) AddPlatforms(versionedname string, platforms []string) error {
65+
if len(platforms) == 0 {
66+
return nil
67+
}
68+
for _, platform := range platforms {
69+
if err := nix.EnsureValidPlatform(platform); err != nil {
70+
return errors.WithStack(err)
71+
}
6672
}
6773

6874
name, version := parseVersionedName(versionedname)
6975
for idx, pkg := range pkgs.Collection {
7076
if pkg.name == name && pkg.Version == version {
7177

72-
// Check if the platform is already present
73-
alreadyPresent := false
74-
for _, existing := range pkg.Platforms {
75-
if existing == platform {
76-
alreadyPresent = true
77-
break
78+
for _, platform := range platforms {
79+
// Append if the platform is not already present
80+
if !lo.SomeBy(pkg.Platforms, func(p string) bool { return p == platform }) {
81+
pkg.Platforms = append(pkg.Platforms, platform)
7882
}
7983
}
8084

81-
// Add the platform if it's not already present
82-
if !alreadyPresent {
83-
pkg.Platforms = append(pkg.Platforms, platform)
84-
}
85-
8685
// Adding any platform will restrict installation to it, so
8786
// the ExcludedPlatforms are no longer needed
8887
pkg.ExcludedPlatforms = nil
@@ -96,35 +95,34 @@ func (pkgs *Packages) AddPlatform(versionedname, platform string) error {
9695
return errors.Errorf("package %s not found", versionedname)
9796
}
9897

99-
// ExcludePlatform adds a platform to the list of excluded platforms for a given package
100-
func (pkgs *Packages) ExcludePlatform(versionedName, platform string) error {
101-
if err := nix.EnsureValidPlatform(platform); err != nil {
102-
return errors.WithStack(err)
98+
// ExcludePlatforms adds a platform to the list of excluded platforms for a given package
99+
func (pkgs *Packages) ExcludePlatforms(versionedName string, platforms []string) error {
100+
if len(platforms) == 0 {
101+
return nil
102+
}
103+
for _, platform := range platforms {
104+
if err := nix.EnsureValidPlatform(platform); err != nil {
105+
return errors.WithStack(err)
106+
}
103107
}
104108

105109
name, version := parseVersionedName(versionedName)
106110
for idx, pkg := range pkgs.Collection {
107111
if pkg.name == name && pkg.Version == version {
108112

109-
// Check if the platform is already present
110-
alreadyPresent := false
111-
for _, existing := range pkg.ExcludedPlatforms {
112-
if existing == platform {
113-
alreadyPresent = true
114-
break
113+
for _, platform := range platforms {
114+
// Append if the platform is not already present
115+
if !lo.SomeBy(pkg.ExcludedPlatforms, func(p string) bool { return p == platform }) {
116+
pkg.ExcludedPlatforms = append(pkg.ExcludedPlatforms, platform)
115117
}
116118
}
117-
118-
if !alreadyPresent {
119-
pkg.ExcludedPlatforms = append(pkg.ExcludedPlatforms, platform)
120-
}
121119
if len(pkg.Platforms) > 0 {
122120
ux.Finfo(
123121
os.Stderr,
124122
"Excluding a platform for %[1]s is a bit redundant because it will only be installed on: %[2]v. "+
125123
"Consider removing the `platform` field from %[1]s's definition in your devbox."+
126124
"json if you intend for %[1]s to be installed on all platforms except %[3]s.\n",
127-
versionedName, strings.Join(pkg.Platforms, ", "), platform,
125+
versionedName, strings.Join(pkg.Platforms, ", "), strings.Join(platforms, ", "),
128126
)
129127
}
130128

internal/impl/packages.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
// Add adds the `pkgs` to the config (i.e. devbox.json) and nix profile for this
3535
// devbox project
3636
// nolint:revive // warns about cognitive complexity
37-
func (d *Devbox) Add(ctx context.Context, platform, excludePlatform string, pkgsNames ...string) error {
37+
func (d *Devbox) Add(ctx context.Context, platforms, excludePlatforms []string, pkgsNames ...string) error {
3838
ctx, task := trace.NewTask(ctx, "devboxAdd")
3939
defer task.End()
4040

@@ -88,15 +88,11 @@ func (d *Devbox) Add(ctx context.Context, platform, excludePlatform string, pkgs
8888
}
8989

9090
for _, pkg := range addedPackageNames {
91-
if platform != "" {
92-
if err := d.cfg.Packages.AddPlatform(pkg, platform); err != nil {
93-
return err
94-
}
91+
if err := d.cfg.Packages.AddPlatforms(pkg, platforms); err != nil {
92+
return err
9593
}
96-
if excludePlatform != "" {
97-
if err := d.cfg.Packages.ExcludePlatform(pkg, excludePlatform); err != nil {
98-
return err
99-
}
94+
if err := d.cfg.Packages.ExcludePlatforms(pkg, excludePlatforms); err != nil {
95+
return err
10096
}
10197
}
10298

internal/impl/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (d *Devbox) Update(ctx context.Context, pkgs ...string) error {
3434
// Calling Add function with the original package names, since
3535
// Add will automatically append @latest if search is able to handle that.
3636
// If not, it will fallback to the nixpkg format.
37-
if err := d.Add(ctx, "" /*platform*/, "" /*excludePlatform*/, pkg.Raw); err != nil {
37+
if err := d.Add(ctx, nil /*platforms*/, nil /*excludePlatforms*/, pkg.Raw); err != nil {
3838
return err
3939
}
4040
} else {

testscripts/add/add_platforms.test.txt

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Testscript for exercising adding packages
22

3-
# exec devbox init
3+
#### Part 1: Adding with a single platform or exclude-platform
4+
45
exec devbox install
56
! exec rg --version
67
! exec vim --version
@@ -17,6 +18,14 @@ exec devbox add vim --exclude-platform x86_64-linux
1718

1819
json.superset devbox.json expected_devbox2.json
1920

21+
#### Part 2: Adding with multiple platforms or exclude-platforms
22+
23+
exec devbox add hello --platform x86_64-darwin,x86_64-linux --platform aarch64-darwin
24+
json.superset devbox.json expected_devbox3.json
25+
26+
exec devbox add cowsay --exclude-platform x86_64-darwin,x86_64-linux --exclude-platform aarch64-darwin
27+
json.superset devbox.json expected_devbox4.json
28+
2029
-- devbox.json --
2130
{
2231
"packages": [
@@ -52,3 +61,48 @@ json.superset devbox.json expected_devbox2.json
5261
}
5362
}
5463
}
64+
65+
-- expected_devbox3.json --
66+
67+
{
68+
"packages": {
69+
"hello": "",
70+
"cowsay": "latest",
71+
"ripgrep": {
72+
"version": "latest",
73+
"platforms": ["x86_64-darwin", "x86_64-linux"]
74+
},
75+
"vim": {
76+
"version": "latest",
77+
"excluded_platforms": ["x86_64-linux"]
78+
},
79+
"hello": {
80+
"version": "latest",
81+
"platforms": ["x86_64-darwin", "x86_64-linux", "aarch64-darwin"]
82+
}
83+
}
84+
}
85+
86+
-- expected_devbox4.json --
87+
88+
{
89+
"packages": {
90+
"hello": "",
91+
"cowsay": {
92+
"version": "latest",
93+
"excluded_platforms": ["x86_64-darwin", "x86_64-linux", "aarch64-darwin"]
94+
},
95+
"ripgrep": {
96+
"version": "latest",
97+
"platforms": ["x86_64-darwin", "x86_64-linux"]
98+
},
99+
"vim": {
100+
"version": "latest",
101+
"excluded_platforms": ["x86_64-linux"]
102+
},
103+
"hello": {
104+
"version": "latest",
105+
"platforms": ["x86_64-darwin", "x86_64-linux", "aarch64-darwin"]
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)