github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/pkg/packager/filters/os.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // SPDX-FileCopyrightText: 2021-Present The Jackal Authors 3 4 // Package filters contains core implementations of the ComponentFilterStrategy interface. 5 package filters 6 7 import ( 8 "errors" 9 10 "github.com/Racer159/jackal/src/types" 11 ) 12 13 // ByLocalOS creates a new filter that filters components based on local (runtime) OS. 14 func ByLocalOS(localOS string) ComponentFilterStrategy { 15 return &localOSFilter{localOS} 16 } 17 18 // localOSFilter filters components based on local (runtime) OS. 19 type localOSFilter struct { 20 localOS string 21 } 22 23 // ErrLocalOSRequired is returned when localOS is not set. 24 var ErrLocalOSRequired = errors.New("localOS is required") 25 26 // Apply applies the filter. 27 func (f *localOSFilter) Apply(pkg types.JackalPackage) ([]types.JackalComponent, error) { 28 if f.localOS == "" { 29 return nil, ErrLocalOSRequired 30 } 31 32 filtered := []types.JackalComponent{} 33 for _, component := range pkg.Components { 34 if component.Only.LocalOS == "" || component.Only.LocalOS == f.localOS { 35 filtered = append(filtered, component) 36 } 37 } 38 return filtered, nil 39 }