Skip to content

Commit 1c86862

Browse files
committed
minor code improvements
1 parent 7e1277e commit 1c86862

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
@@ -190,15 +190,24 @@ var nixPlatforms = []string{
190190
"armv7l-linux",
191191
}
192192

193-
// EnsureValidPlatform returns an error if the platform is not supported by nix.
193+
// ensureValidPlatform returns an error if the platform is not supported by nix.
194194
// https://nixos.org/manual/nix/stable/installation/supported-platforms.html
195-
func EnsureValidPlatform(platform string) error {
196-
for _, p := range nixPlatforms {
197-
if p == platform {
198-
return nil
195+
func EnsureValidPlatform(platforms ...string) error {
196+
ensureValid := func(platform string) error {
197+
for _, p := range nixPlatforms {
198+
if p == platform {
199+
return nil
200+
}
199201
}
202+
return usererr.New("Unsupported platform: %s. Valid platforms are: %v", platform, nixPlatforms)
200203
}
201-
return usererr.New("Unsupported platform: %s. Valid platforms are: %v", platform, nixPlatforms)
204+
205+
for _, p := range platforms {
206+
if err := ensureValid(p); err != nil {
207+
return err
208+
}
209+
}
210+
return nil
202211
}
203212

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

0 commit comments

Comments
 (0)