1// Package mirrors handles managing mirrors in the running application
2package mirrors
3
4import (
5	"fmt"
6	"os"
7	"path/filepath"
8
9	"github.com/Masterminds/glide/msg"
10	gpath "github.com/Masterminds/glide/path"
11)
12
13var mirrors map[string]*mirror
14
15func init() {
16	mirrors = make(map[string]*mirror)
17}
18
19type mirror struct {
20	Repo, Vcs string
21}
22
23// Get retrieves information about an mirror. It returns.
24// - bool if found
25// - new repo location
26// - vcs type
27func Get(k string) (bool, string, string) {
28	o, f := mirrors[k]
29	if !f {
30		return false, "", ""
31	}
32
33	return true, o.Repo, o.Vcs
34}
35
36// Load pulls the mirrors into memory
37func Load() error {
38	home := gpath.Home()
39
40	op := filepath.Join(home, "mirrors.yaml")
41
42	var ov *Mirrors
43	if _, err := os.Stat(op); os.IsNotExist(err) {
44		msg.Debug("No mirrors.yaml file exists")
45		ov = &Mirrors{
46			Repos: make(MirrorRepos, 0),
47		}
48		return nil
49	} else if err != nil {
50		ov = &Mirrors{
51			Repos: make(MirrorRepos, 0),
52		}
53		return err
54	}
55
56	var err error
57	ov, err = ReadMirrorsFile(op)
58	if err != nil {
59		return fmt.Errorf("Error reading existing mirrors.yaml file: %s", err)
60	}
61
62	msg.Info("Loading mirrors from mirrors.yaml file")
63	for _, o := range ov.Repos {
64		msg.Debug("Found mirror: %s to %s (%s)", o.Original, o.Repo, o.Vcs)
65		no := &mirror{
66			Repo: o.Repo,
67			Vcs:  o.Vcs,
68		}
69		mirrors[o.Original] = no
70	}
71
72	return nil
73}
74