github.com/chjlangzi/glide@v0.0.0-20171121052037-a806f0aaeda0/mirrors/cfg.go (about)

     1  package mirrors
     2  
     3  import (
     4  	"io/ioutil"
     5  	"sort"
     6  	"strings"
     7  
     8  	"gopkg.in/yaml.v2"
     9  )
    10  
    11  // Mirrors contains global mirrors to local configuration
    12  type Mirrors struct {
    13  
    14  	// Repos contains repo mirror configuration
    15  	Repos MirrorRepos `yaml:"repos"`
    16  }
    17  
    18  // Marshal converts a Mirror instance to YAML
    19  func (ov *Mirrors) Marshal() ([]byte, error) {
    20  
    21  	yml, err := yaml.Marshal(&ov)
    22  	if err != nil {
    23  		return []byte{}, err
    24  	}
    25  	return yml, nil
    26  }
    27  
    28  // WriteFile writes an mirrors.yaml file
    29  //
    30  // This is a convenience function that marshals the YAML and then writes it to
    31  // the given file. If the file exists, it will be clobbered.
    32  func (ov *Mirrors) WriteFile(opath string) error {
    33  	o, err := ov.Marshal()
    34  	if err != nil {
    35  		return err
    36  	}
    37  	return ioutil.WriteFile(opath, o, 0666)
    38  }
    39  
    40  // ReadMirrorsFile loads the contents of an mirrors.yaml file.
    41  func ReadMirrorsFile(opath string) (*Mirrors, error) {
    42  	yml, err := ioutil.ReadFile(opath)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	ov, err := FromYaml(yml)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	return ov, nil
    51  }
    52  
    53  // FromYaml returns an instance of Mirrors from YAML
    54  func FromYaml(yml []byte) (*Mirrors, error) {
    55  	ov := &Mirrors{}
    56  	err := yaml.Unmarshal([]byte(yml), &ov)
    57  	return ov, err
    58  }
    59  
    60  // MarshalYAML is a hook for gopkg.in/yaml.v2.
    61  // It sorts mirror repos lexicographically for reproducibility.
    62  func (ov *Mirrors) MarshalYAML() (interface{}, error) {
    63  
    64  	sort.Sort(ov.Repos)
    65  
    66  	return ov, nil
    67  }
    68  
    69  // MirrorRepos is a slice of Mirror pointers
    70  type MirrorRepos []*MirrorRepo
    71  
    72  // Len returns the length of the MirrorRepos. This is needed for sorting with
    73  // the sort package.
    74  func (o MirrorRepos) Len() int {
    75  	return len(o)
    76  }
    77  
    78  // Less is needed for the sort interface. It compares two MirrorRepos based on
    79  // their original value.
    80  func (o MirrorRepos) Less(i, j int) bool {
    81  
    82  	// Names are normalized to lowercase because case affects sorting order. For
    83  	// example, Masterminds comes before kylelemons. Making them lowercase
    84  	// causes kylelemons to come first which is what is expected.
    85  	return strings.ToLower(o[i].Original) < strings.ToLower(o[j].Original)
    86  }
    87  
    88  // Swap is needed for the sort interface. It swaps the position of two
    89  // MirrorRepos.
    90  func (o MirrorRepos) Swap(i, j int) {
    91  	o[i], o[j] = o[j], o[i]
    92  }
    93  
    94  // MirrorRepo represents a single repo mirror
    95  type MirrorRepo struct {
    96  	Original string `yaml:"original"`
    97  	Repo     string `yaml:"repo"`
    98  	Base     string `yaml:"base,omitempty"`
    99  	Vcs      string `yaml:"vcs,omitempty"`
   100  }