github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/pkg/packager/filters/utils.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  	"path"
     9  	"strings"
    10  
    11  	"github.com/Racer159/jackal/src/types"
    12  )
    13  
    14  type selectState int
    15  
    16  const (
    17  	unknown selectState = iota
    18  	included
    19  	excluded
    20  )
    21  
    22  func includedOrExcluded(componentName string, requestedComponentNames []string) (selectState, string) {
    23  	// Check if the component has a leading dash indicating it should be excluded - this is done first so that exclusions precede inclusions
    24  	for _, requestedComponent := range requestedComponentNames {
    25  		if strings.HasPrefix(requestedComponent, "-") {
    26  			// If the component glob matches one of the requested components, then return true
    27  			// This supports globbing with "path" in order to have the same behavior across OSes (if we ever allow namespaced components with /)
    28  			if matched, _ := path.Match(strings.TrimPrefix(requestedComponent, "-"), componentName); matched {
    29  				return excluded, requestedComponent
    30  			}
    31  		}
    32  	}
    33  	// Check if the component matches a glob pattern and should be included
    34  	for _, requestedComponent := range requestedComponentNames {
    35  		// If the component glob matches one of the requested components, then return true
    36  		// This supports globbing with "path" in order to have the same behavior across OSes (if we ever allow namespaced components with /)
    37  		if matched, _ := path.Match(requestedComponent, componentName); matched {
    38  			return included, requestedComponent
    39  		}
    40  	}
    41  
    42  	// All other cases we don't know if we should include or exclude yet
    43  	return unknown, ""
    44  }
    45  
    46  // isRequired returns if the component is required or not.
    47  func isRequired(c types.JackalComponent) bool {
    48  	requiredExists := c.Required != nil
    49  	required := requiredExists && *c.Required
    50  
    51  	if requiredExists {
    52  		return required
    53  	}
    54  	return false
    55  }