github.com/cobalt77/jfrog-client-go@v0.14.5/artifactory/buildinfo/config.go (about)

     1  package buildinfo
     2  
     3  import (
     4  	"path/filepath"
     5  	"strings"
     6  
     7  	"github.com/cobalt77/jfrog-client-go/auth"
     8  	"github.com/cobalt77/jfrog-client-go/utils/errorutils"
     9  )
    10  
    11  const BuildInfoEnvPrefix = "buildInfo.env."
    12  
    13  type Configuration struct {
    14  	ArtDetails auth.ServiceDetails
    15  	BuildUrl   string
    16  	DryRun     bool
    17  	EnvInclude string
    18  	EnvExclude string
    19  }
    20  
    21  func (config *Configuration) GetArtifactoryDetails() auth.ServiceDetails {
    22  	return config.ArtDetails
    23  }
    24  
    25  func (config *Configuration) SetArtifactoryDetails(artDetails auth.ServiceDetails) {
    26  	config.ArtDetails = artDetails
    27  }
    28  
    29  func (config *Configuration) IsDryRun() bool {
    30  	return config.DryRun
    31  }
    32  
    33  type Filter func(map[string]string) (map[string]string, error)
    34  
    35  // IncludeFilter returns a function used to filter entries of a map based on key
    36  func (config Configuration) IncludeFilter() Filter {
    37  	pats := strings.Split(config.EnvInclude, ";")
    38  	return func(tempMap map[string]string) (map[string]string, error) {
    39  		result := make(map[string]string)
    40  		for k, v := range tempMap {
    41  			for _, filterPattern := range pats {
    42  				matched, err := filepath.Match(strings.ToLower(filterPattern), strings.ToLower(strings.TrimPrefix(k, BuildInfoEnvPrefix)))
    43  				if errorutils.CheckError(err) != nil {
    44  					return nil, err
    45  				}
    46  				if matched {
    47  					result[k] = v
    48  					break
    49  				}
    50  			}
    51  		}
    52  		return result, nil
    53  	}
    54  }
    55  
    56  // ExcludeFilter returns a function used to filter entries of a map based on key
    57  func (config Configuration) ExcludeFilter() Filter {
    58  	pats := strings.Split(config.EnvExclude, ";")
    59  	return func(tempMap map[string]string) (map[string]string, error) {
    60  		result := make(map[string]string)
    61  		for k, v := range tempMap {
    62  			include := true
    63  			for _, filterPattern := range pats {
    64  				matched, err := filepath.Match(strings.ToLower(filterPattern), strings.ToLower(strings.TrimPrefix(k, BuildInfoEnvPrefix)))
    65  				if errorutils.CheckError(err) != nil {
    66  					return nil, err
    67  				}
    68  				if matched {
    69  					include = false
    70  					break
    71  				}
    72  			}
    73  			if include {
    74  				result[k] = v
    75  			}
    76  		}
    77  		return result, nil
    78  	}
    79  }