github.com/zoumo/helm@v2.5.0+incompatible/pkg/plugin/cache/cache.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  // Package cache provides a key generator for vcs urls.
    17  package cache // import "k8s.io/helm/pkg/plugin/cache"
    18  
    19  import (
    20  	"net/url"
    21  	"regexp"
    22  	"strings"
    23  )
    24  
    25  // Thanks glide!
    26  
    27  // scpSyntaxRe matches the SCP-like addresses used to access repos over SSH.
    28  var scpSyntaxRe = regexp.MustCompile(`^([a-zA-Z0-9_]+)@([a-zA-Z0-9._-]+):(.*)$`)
    29  
    30  // Key generates a cache key based on a url or scp string. The key is file
    31  // system safe.
    32  func Key(repo string) (string, error) {
    33  
    34  	var u *url.URL
    35  	var err error
    36  	var strip bool
    37  	if m := scpSyntaxRe.FindStringSubmatch(repo); m != nil {
    38  		// Match SCP-like syntax and convert it to a URL.
    39  		// Eg, "git@github.com:user/repo" becomes
    40  		// "ssh://git@github.com/user/repo".
    41  		u = &url.URL{
    42  			Scheme: "ssh",
    43  			User:   url.User(m[1]),
    44  			Host:   m[2],
    45  			Path:   "/" + m[3],
    46  		}
    47  		strip = true
    48  	} else {
    49  		u, err = url.Parse(repo)
    50  		if err != nil {
    51  			return "", err
    52  		}
    53  	}
    54  
    55  	if strip {
    56  		u.Scheme = ""
    57  	}
    58  
    59  	var key string
    60  	if u.Scheme != "" {
    61  		key = u.Scheme + "-"
    62  	}
    63  	if u.User != nil && u.User.Username() != "" {
    64  		key = key + u.User.Username() + "-"
    65  	}
    66  	key = key + u.Host
    67  	if u.Path != "" {
    68  		key = key + strings.Replace(u.Path, "/", "-", -1)
    69  	}
    70  
    71  	key = strings.Replace(key, ":", "-", -1)
    72  
    73  	return key, nil
    74  }