github.com/xkeyideal/glide@v0.0.0-20171121052037-a806f0aaeda0/action/mirrors.go (about) 1 package action 2 3 import ( 4 "os" 5 "path/filepath" 6 7 "github.com/xkeyideal/glide/mirrors" 8 "github.com/xkeyideal/glide/msg" 9 gpath "github.com/xkeyideal/glide/path" 10 ) 11 12 // MirrorsList displays a list of currently setup mirrors. 13 func MirrorsList() error { 14 home := gpath.Home() 15 16 op := filepath.Join(home, "mirrors.yaml") 17 18 if _, err := os.Stat(op); os.IsNotExist(err) { 19 msg.Info("No mirrors exist. No mirrors.yaml file not found") 20 return nil 21 } 22 23 ov, err := mirrors.ReadMirrorsFile(op) 24 if err != nil { 25 msg.Die("Unable to read mirrors.yaml file: %s", err) 26 } 27 28 if len(ov.Repos) == 0 { 29 msg.Info("No mirrors found") 30 return nil 31 } 32 33 msg.Info("Mirrors...") 34 for _, r := range ov.Repos { 35 if r.Vcs == "" { 36 if r.Base != "" { 37 msg.Info("--> %s replaced by %s, base: %s", r.Original, r.Repo, r.Base) 38 } else { 39 msg.Info("--> %s replaced by %s", r.Original, r.Repo) 40 } 41 } else { 42 if r.Base != "" { 43 msg.Info("--> %s replaced by %s (%s), base: %s", r.Original, r.Repo, r.Vcs, r.Base) 44 } else { 45 msg.Info("--> %s replaced by %s (%s)", r.Original, r.Repo, r.Vcs) 46 } 47 } 48 } 49 50 return nil 51 } 52 53 // MirrorsSet sets a mirror to use 54 func MirrorsSet(o, r, v, b string) error { 55 if o == "" || r == "" { 56 msg.Err("Both the original and mirror values are required") 57 return nil 58 } 59 60 home := gpath.Home() 61 62 op := filepath.Join(home, "mirrors.yaml") 63 64 var ov *mirrors.Mirrors 65 if _, err := os.Stat(op); os.IsNotExist(err) { 66 msg.Info("No mirrors.yaml file exists. Creating new one") 67 ov = &mirrors.Mirrors{ 68 Repos: make(mirrors.MirrorRepos, 0), 69 } 70 } else { 71 ov, err = mirrors.ReadMirrorsFile(op) 72 if err != nil { 73 msg.Die("Error reading existing mirrors.yaml file: %s", err) 74 } 75 } 76 77 found := false 78 for i, re := range ov.Repos { 79 if re.Original == o { 80 found = true 81 msg.Info("%s found in mirrors. Replacing with new settings", o) 82 ov.Repos[i].Repo = r 83 ov.Repos[i].Vcs = v 84 ov.Repos[i].Base = b 85 } 86 } 87 88 if !found { 89 nr := &mirrors.MirrorRepo{ 90 Original: o, 91 Repo: r, 92 Vcs: v, 93 Base: b, 94 } 95 ov.Repos = append(ov.Repos, nr) 96 } 97 98 msg.Info("%s being set to %s", o, r) 99 100 err := ov.WriteFile(op) 101 if err != nil { 102 msg.Err("Error writing mirrors.yaml file: %s", err) 103 } else { 104 msg.Info("mirrors.yaml written with changes") 105 } 106 107 return nil 108 } 109 110 // MirrorsRemove removes a mirrors setting 111 func MirrorsRemove(k string) error { 112 if k == "" { 113 msg.Err("The mirror to remove is required") 114 return nil 115 } 116 117 home := gpath.Home() 118 119 op := filepath.Join(home, "mirrors.yaml") 120 121 if _, err := os.Stat(op); os.IsNotExist(err) { 122 msg.Err("mirrors.yaml file not found") 123 return nil 124 } 125 126 ov, err := mirrors.ReadMirrorsFile(op) 127 if err != nil { 128 msg.Die("Unable to read mirrors.yaml file: %s", err) 129 } 130 131 var nre mirrors.MirrorRepos 132 var found bool 133 for _, re := range ov.Repos { 134 if re.Original != k { 135 nre = append(nre, re) 136 } else { 137 found = true 138 } 139 } 140 141 if !found { 142 msg.Warn("%s was not found in mirrors", k) 143 } else { 144 msg.Info("%s was removed from mirrors", k) 145 ov.Repos = nre 146 147 err = ov.WriteFile(op) 148 if err != nil { 149 msg.Err("Error writing mirrors.yaml file: %s", err) 150 } else { 151 msg.Info("mirrors.yaml written with changes") 152 } 153 } 154 155 return nil 156 }