github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/pkg/packager/filters/select.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  	"github.com/Racer159/jackal/src/types"
     9  	"github.com/defenseunicorns/pkg/helpers"
    10  )
    11  
    12  // BySelectState creates a new simple included filter.
    13  func BySelectState(optionalComponents string) ComponentFilterStrategy {
    14  	requested := helpers.StringToSlice(optionalComponents)
    15  
    16  	return &selectStateFilter{
    17  		requested,
    18  	}
    19  }
    20  
    21  // selectStateFilter sorts based purely on the internal included state of the component.
    22  type selectStateFilter struct {
    23  	requestedComponents []string
    24  }
    25  
    26  // Apply applies the filter.
    27  func (f *selectStateFilter) Apply(pkg types.JackalPackage) ([]types.JackalComponent, error) {
    28  	isPartial := len(f.requestedComponents) > 0 && f.requestedComponents[0] != ""
    29  
    30  	result := []types.JackalComponent{}
    31  
    32  	for _, component := range pkg.Components {
    33  		selectState := unknown
    34  
    35  		if isPartial {
    36  			selectState, _ = includedOrExcluded(component.Name, f.requestedComponents)
    37  
    38  			if selectState == excluded {
    39  				continue
    40  			}
    41  		} else {
    42  			selectState = included
    43  		}
    44  
    45  		if selectState == included {
    46  			result = append(result, component)
    47  		}
    48  	}
    49  
    50  	return result, nil
    51  }