Skip to content

Commit 577032b

Browse files
committed
minor code improvements
1 parent 842fb5b commit 577032b

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

internal/devconfig/packages.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,16 @@ func (pkgs *Packages) AddPlatforms(versionedname string, platforms []string) err
6565
if len(platforms) == 0 {
6666
return nil
6767
}
68-
for _, platform := range platforms {
69-
if err := nix.EnsureValidPlatform(platform); err != nil {
70-
return errors.WithStack(err)
71-
}
68+
if err := nix.EnsureValidPlatform(platforms...); err != nil {
69+
return errors.WithStack(err)
7270
}
7371

7472
name, version := parseVersionedName(versionedname)
7573
for idx, pkg := range pkgs.Collection {
7674
if pkg.name == name && pkg.Version == version {
7775

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)
82-
}
83-
}
76+
// Append if the platform is not already present
77+
pkg.Platforms = lo.Uniq(append(pkg.Platforms, platforms...))
8478

8579
// Adding any platform will restrict installation to it, so
8680
// the ExcludedPlatforms are no longer needed
@@ -110,12 +104,7 @@ func (pkgs *Packages) ExcludePlatforms(versionedName string, platforms []string)
110104
for idx, pkg := range pkgs.Collection {
111105
if pkg.name == name && pkg.Version == version {
112106

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)
117-
}
118-
}
107+
pkg.ExcludedPlatforms = lo.Uniq(append(pkg.ExcludedPlatforms, platforms...))
119108
if len(pkg.Platforms) > 0 {
120109
ux.Finfo(
121110
os.Stderr,

internal/nix/nix.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,24 @@ var nixPlatforms = []string{
195195
"armv7l-linux",
196196
}
197197

198-
// EnsureValidPlatform returns an error if the platform is not supported by nix.
198+
// ensureValidPlatform returns an error if the platform is not supported by nix.
199199
// https://nixos.org/manual/nix/stable/installation/supported-platforms.html
200-
func EnsureValidPlatform(platform string) error {
201-
for _, p := range nixPlatforms {
202-
if p == platform {
203-
return nil
200+
func EnsureValidPlatform(platforms ...string) error {
201+
ensureValid := func(platform string) error {
202+
for _, p := range nixPlatforms {
203+
if p == platform {
204+
return nil
205+
}
204206
}
207+
return usererr.New("Unsupported platform: %s. Valid platforms are: %v", platform, nixPlatforms)
205208
}
206-
return usererr.New("Unsupported platform: %s. Valid platforms are: %v", platform, nixPlatforms)
209+
210+
for _, p := range platforms {
211+
if err := ensureValid(p); err != nil {
212+
return err
213+
}
214+
}
215+
return nil
207216
}
208217

209218
// Warning: be careful using the bins in default/bin, they won't always match bins

0 commit comments

Comments
 (0)