@@ -2,14 +2,13 @@ package devconfig
2
2
3
3
import (
4
4
"encoding/json"
5
- "os"
6
- "strings"
7
5
8
6
"github.com/pkg/errors"
7
+ "github.com/samber/lo"
9
8
orderedmap "github.com/wk8/go-ordered-map/v2"
9
+ "go.jetpack.io/devbox/internal/boxcli/usererr"
10
10
"go.jetpack.io/devbox/internal/nix"
11
11
"go.jetpack.io/devbox/internal/searcher"
12
- "go.jetpack.io/devbox/internal/ux"
13
12
"golang.org/x/exp/slices"
14
13
)
15
14
@@ -59,29 +58,21 @@ func (pkgs *Packages) Remove(versionedName string) {
59
58
})
60
59
}
61
60
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 {
61
+ // AddPlatforms adds a platform to the list of platforms for a given package
62
+ func (pkgs * Packages ) AddPlatforms (versionedname string , platforms []string ) error {
63
+ if len (platforms ) == 0 {
64
+ return nil
65
+ }
66
+ if err := nix .EnsureValidPlatform (platforms ... ); err != nil {
65
67
return errors .WithStack (err )
66
68
}
67
69
68
70
name , version := parseVersionedName (versionedname )
69
71
for idx , pkg := range pkgs .Collection {
70
72
if pkg .name == name && pkg .Version == version {
71
73
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
- }
79
- }
80
-
81
- // Add the platform if it's not already present
82
- if ! alreadyPresent {
83
- pkg .Platforms = append (pkg .Platforms , platform )
84
- }
74
+ // Append if the platform is not already present
75
+ pkg .Platforms = lo .Uniq (append (pkg .Platforms , platforms ... ))
85
76
86
77
// Adding any platform will restrict installation to it, so
87
78
// the ExcludedPlatforms are no longer needed
@@ -96,35 +87,27 @@ func (pkgs *Packages) AddPlatform(versionedname, platform string) error {
96
87
return errors .Errorf ("package %s not found" , versionedname )
97
88
}
98
89
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 )
90
+ // ExcludePlatforms adds a platform to the list of excluded platforms for a given package
91
+ func (pkgs * Packages ) ExcludePlatforms (versionedName string , platforms []string ) error {
92
+ if len (platforms ) == 0 {
93
+ return nil
94
+ }
95
+ for _ , platform := range platforms {
96
+ if err := nix .EnsureValidPlatform (platform ); err != nil {
97
+ return errors .WithStack (err )
98
+ }
103
99
}
104
100
105
101
name , version := parseVersionedName (versionedName )
106
102
for idx , pkg := range pkgs .Collection {
107
103
if pkg .name == name && pkg .Version == version {
108
104
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
115
- }
116
- }
117
-
118
- if ! alreadyPresent {
119
- pkg .ExcludedPlatforms = append (pkg .ExcludedPlatforms , platform )
120
- }
105
+ pkg .ExcludedPlatforms = lo .Uniq (append (pkg .ExcludedPlatforms , platforms ... ))
121
106
if len (pkg .Platforms ) > 0 {
122
- ux .Finfo (
123
- os .Stderr ,
124
- "Excluding a platform for %[1]s is a bit redundant because it will only be installed on: %[2]v. " +
125
- "Consider removing the `platform` field from %[1]s's definition in your devbox." +
126
- "json if you intend for %[1]s to be installed on all platforms except %[3]s.\n " ,
127
- versionedName , strings .Join (pkg .Platforms , ", " ), platform ,
107
+ return usererr .New (
108
+ "cannot exclude any platform for package %s because it already has `platforms` defined. " +
109
+ "Please delete the `platforms` for this package from devbox.json and re-try." ,
110
+ pkg .VersionedName (),
128
111
)
129
112
}
130
113
0 commit comments