github.com/zntrio/harp/v2@v2.0.9/pkg/bundle/selector/match_path.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package selector
    19  
    20  import (
    21  	"fmt"
    22  	"regexp"
    23  
    24  	"github.com/gobwas/glob"
    25  
    26  	bundlev1 "github.com/zntrio/harp/v2/api/gen/go/harp/bundle/v1"
    27  )
    28  
    29  // MatchPathStrict returns a path matcher specification with strict profile.
    30  func MatchPathStrict(value string) Specification {
    31  	return &matchPath{
    32  		strict: value,
    33  	}
    34  }
    35  
    36  // MatchPathRegex returns a path matcher specification with regexp.
    37  func MatchPathRegex(pattern string) (Specification, error) {
    38  	// Compile and check filter
    39  	m, err := regexp.Compile(pattern)
    40  	if err != nil {
    41  		return nil, fmt.Errorf("unable to compile regex filter: %w", err)
    42  	}
    43  
    44  	// No error
    45  	return &matchPath{
    46  		regex: m,
    47  	}, nil
    48  }
    49  
    50  // MatchPathGlob returns a path matcher specification with glob query.
    51  func MatchPathGlob(pattern string) (Specification, error) {
    52  	// Compile and check filter
    53  	m, err := glob.Compile(pattern)
    54  	if err != nil {
    55  		return nil, fmt.Errorf("unable to compile glob filter: %w", err)
    56  	}
    57  
    58  	// No error
    59  	return &matchPath{
    60  		g: m,
    61  	}, nil
    62  }
    63  
    64  // MatchPath checks if secret path match the given string.
    65  type matchPath struct {
    66  	strict string
    67  	regex  *regexp.Regexp
    68  	g      glob.Glob
    69  }
    70  
    71  // IsSatisfiedBy returns specification satisfaction status.
    72  func (s *matchPath) IsSatisfiedBy(object interface{}) bool {
    73  	// If object is a package
    74  	if p, ok := object.(*bundlev1.Package); ok {
    75  		switch {
    76  		case s.strict != "":
    77  			return p.Name == s.strict
    78  		case s.regex != nil:
    79  			return s.regex.MatchString(p.Name)
    80  		case s.g != nil:
    81  			return s.g.Match(p.Name)
    82  		}
    83  	}
    84  
    85  	return false
    86  }