gitlab.com/jfprevost/gitlab-runner-notlscheck@v11.11.4+incompatible/executors/docker/internal/volumes/parser/base_parser.go (about)

     1  package parser
     2  
     3  import (
     4  	"regexp"
     5  
     6  	"gitlab.com/gitlab-org/gitlab-runner/helpers/path"
     7  )
     8  
     9  type baseParser struct {
    10  	path path.Path
    11  }
    12  
    13  // The way how matchesToVolumeSpecParts parses the volume mount specification and assigns
    14  // parts was inspired by how Docker Engine's `windowsParser` is created. The original sources
    15  // can be found at:
    16  //
    17  // https://github.com/docker/engine/blob/a79fabbfe84117696a19671f4aa88b82d0f64fc1/volume/mounts/windows_parser.go
    18  //
    19  // The original source is licensed under Apache License 2.0 and the copyright for it
    20  // goes to Docker, Inc.
    21  func (p *baseParser) matchesToVolumeSpecParts(spec string, specExp *regexp.Regexp) (map[string]string, error) {
    22  	match := specExp.FindStringSubmatch(spec)
    23  
    24  	if len(match) == 0 {
    25  		return nil, NewInvalidVolumeSpecErr(spec)
    26  	}
    27  
    28  	matchgroups := make(map[string]string)
    29  	for i, name := range specExp.SubexpNames() {
    30  		matchgroups[name] = match[i]
    31  	}
    32  
    33  	parts := map[string]string{
    34  		"source":      "",
    35  		"destination": "",
    36  		"mode":        "",
    37  	}
    38  
    39  	for group := range parts {
    40  		content, ok := matchgroups[group]
    41  		if ok {
    42  			parts[group] = content
    43  		}
    44  	}
    45  
    46  	return parts, nil
    47  }
    48  
    49  func (p *baseParser) Path() path.Path {
    50  	return p.path
    51  }