6
6
"strings"
7
7
8
8
"github.com/pkg/errors"
9
+ "github.com/samber/lo"
9
10
orderedmap "github.com/wk8/go-ordered-map/v2"
10
11
"go.jetpack.io/devbox/internal/nix"
11
12
"go.jetpack.io/devbox/internal/searcher"
@@ -59,30 +60,28 @@ func (pkgs *Packages) Remove(versionedName string) {
59
60
})
60
61
}
61
62
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
+ }
66
72
}
67
73
68
74
name , version := parseVersionedName (versionedname )
69
75
for idx , pkg := range pkgs .Collection {
70
76
if pkg .name == name && pkg .Version == version {
71
77
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 )
78
82
}
79
83
}
80
84
81
- // Add the platform if it's not already present
82
- if ! alreadyPresent {
83
- pkg .Platforms = append (pkg .Platforms , platform )
84
- }
85
-
86
85
// Adding any platform will restrict installation to it, so
87
86
// the ExcludedPlatforms are no longer needed
88
87
pkg .ExcludedPlatforms = nil
@@ -96,35 +95,34 @@ func (pkgs *Packages) AddPlatform(versionedname, platform string) error {
96
95
return errors .Errorf ("package %s not found" , versionedname )
97
96
}
98
97
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
+ }
103
107
}
104
108
105
109
name , version := parseVersionedName (versionedName )
106
110
for idx , pkg := range pkgs .Collection {
107
111
if pkg .name == name && pkg .Version == version {
108
112
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 )
115
117
}
116
118
}
117
-
118
- if ! alreadyPresent {
119
- pkg .ExcludedPlatforms = append (pkg .ExcludedPlatforms , platform )
120
- }
121
119
if len (pkg .Platforms ) > 0 {
122
120
ux .Finfo (
123
121
os .Stderr ,
124
122
"Excluding a platform for %[1]s is a bit redundant because it will only be installed on: %[2]v. " +
125
123
"Consider removing the `platform` field from %[1]s's definition in your devbox." +
126
124
"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 , ", " ) ,
128
126
)
129
127
}
130
128
0 commit comments