github.com/MealCraft/glide@v0.13.4/action/mirrors.go (about)

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