github.com/n0needt0/glide@v0.0.0-20160325160517-844a77136d85/repo/cache.go (about)

     1  package repo
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"io/ioutil"
     7  	"net/url"
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  	"time"
    12  
    13  	//"github.com/Masterminds/glide/msg"
    14  )
    15  
    16  var cacheEnabled = true
    17  
    18  var errCacheDisabled = errors.New("Cache disabled")
    19  
    20  // EnsureCacheDir Creates the $HOME/.glide/cache directory (unless home is
    21  // specified to be different) if it does not exist.
    22  /*
    23  func EnsureCacheDir(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
    24  	home := p.Get("home", "").(string)
    25  	if home == "" {
    26  		cacheEnabled = false
    27  		msg.Warn("Unable to locate home directory")
    28  		return false, nil
    29  	}
    30  	err := os.MkdirAll(filepath.Join(home, "cache", "info"), os.ModeDir|os.ModePerm)
    31  	if err != nil {
    32  		cacheEnabled = false
    33  		Warn("Error creating Glide directory %s", home)
    34  	}
    35  	return false, nil
    36  }
    37  */
    38  
    39  // Pass in a repo location and get a cache key from it.
    40  func cacheCreateKey(repo string) (string, error) {
    41  
    42  	// A url needs a scheme. A git repo such as
    43  	// git@github.com:Masterminds/cookoo.git reworked to the url parser.
    44  	c := strings.Contains(repo, "://")
    45  	if !c {
    46  		repo = "ssh://" + repo
    47  	}
    48  
    49  	u, err := url.Parse(repo)
    50  	if err != nil {
    51  		return "", err
    52  	}
    53  
    54  	if !c {
    55  		u.Scheme = ""
    56  	}
    57  
    58  	var key string
    59  	if u.Scheme != "" {
    60  		key = u.Scheme + "-"
    61  	}
    62  	if u.User != nil && u.User.Username() != "" {
    63  		key = key + u.User.Username() + "-"
    64  	}
    65  	key = key + u.Host
    66  	if u.Path != "" {
    67  		key = key + strings.Replace(u.Path, "/", "-", -1)
    68  	}
    69  
    70  	key = strings.Replace(key, ":", "-", -1)
    71  
    72  	return key, nil
    73  }
    74  
    75  type cacheRepoInfo struct {
    76  	DefaultBranch string `json:"default-branch"`
    77  	LastUpdate    string `json:"last-update"`
    78  }
    79  
    80  func saveCacheRepoData(key string, data cacheRepoInfo, location string) error {
    81  	if !cacheEnabled {
    82  		return errCacheDisabled
    83  	}
    84  	data.LastUpdate = time.Now().String()
    85  	d, err := json.Marshal(data)
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	p := filepath.Join(location, "cache", "info", key+".json")
    91  	f, err := os.Create(p)
    92  	if err != nil {
    93  		return err
    94  	}
    95  	defer f.Close()
    96  
    97  	_, err = f.Write(d)
    98  	return err
    99  }
   100  
   101  func cacheRepoData(key, location string) (*cacheRepoInfo, error) {
   102  	if !cacheEnabled {
   103  		return &cacheRepoInfo{}, errCacheDisabled
   104  	}
   105  	c := &cacheRepoInfo{}
   106  	p := filepath.Join(location, "cache", "info", key+".json")
   107  	f, err := ioutil.ReadFile(p)
   108  	if err != nil {
   109  		return &cacheRepoInfo{}, err
   110  	}
   111  	err = json.Unmarshal(f, c)
   112  	if err != nil {
   113  		return &cacheRepoInfo{}, err
   114  	}
   115  	return c, nil
   116  }