From 22d80eda906d1958dfde5072717a12995ee6df2d Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Fri, 5 May 2017 20:32:19 +0200 Subject: implement rtree drop rtree is functionally complete, but the implementation is very messy now. :( --- pkg/cli/query.go | 22 ++++++++++++++++++++-- pkg/rtree/index.go | 34 ++++++++++++++++++++++++++++++++++ pkg/rtree/main.go | 14 +++++++++++++- 3 files changed, 67 insertions(+), 3 deletions(-) (limited to 'pkg') diff --git a/pkg/cli/query.go b/pkg/cli/query.go index 0d367be..40baaec 100644 --- a/pkg/cli/query.go +++ b/pkg/cli/query.go @@ -28,6 +28,26 @@ import ( terminal "golang.org/x/crypto/ssh/terminal" ) +//Confirm displays a yes/no question and returns whether the user answered "yes". +func Confirm(question string) bool { + os.Stdout.Write([]byte(strings.TrimSpace(question) + " [y/n] ")) + + buf := buffer{Input: os.Stdin} + for { + switch string(buf.getNextInput()) { + case "y", "Y": + os.Stdout.Write([]byte("-> yes\n")) + return true + case "n", "N": + os.Stdout.Write([]byte("-> no\n")) + return false + case "\x03": // Ctrl-C + fmt.Fprintln(os.Stderr, "\nInterrupted!") + os.Exit(255) + } + } +} + //Choice is a thing that the user can choose during Query(). type Choice struct { //If given, the choice can be selected by pressing the key that @@ -89,8 +109,6 @@ OUTER: case "\x03": // Ctrl-C fmt.Fprintln(os.Stderr, "Interrupted!") os.Exit(255) - default: - fmt.Printf("%#v\n", string(input)) } //prepare to re-render choices diff --git a/pkg/rtree/index.go b/pkg/rtree/index.go index 2edc452..f6ee2f4 100644 --- a/pkg/rtree/index.go +++ b/pkg/rtree/index.go @@ -108,6 +108,17 @@ func (i *Index) Write() { path := indexPath() util.FatalIfError(os.MkdirAll(filepath.Dir(path), 0755)) util.FatalIfError(ioutil.WriteFile(path, buf, 0644)) + + //perform sanity check (TODO: do this instead when rebuilding the index) + seen := make(map[string]bool) + warned := make(map[string]bool) + for _, repo := range i.Repos { + if seen[repo.CheckoutPath] && !warned[repo.CheckoutPath] { + fmt.Fprintf(os.Stderr, "warning: repo %s appears multiple times in the index file!\n", repo.AbsolutePath()) + warned[repo.CheckoutPath] = true + } + seen[repo.CheckoutPath] = true + } } //InteractiveRebuild implements the `rtree index` subcommand. @@ -235,6 +246,7 @@ func (i *Index) InteractiveFindRepo(remoteURL string, allowClone bool) *Repo { } if !allowClone { + util.ShowError(errors.New("no such remote in index (you can validate the index with `rtree index`)")) return nil } @@ -347,3 +359,25 @@ func (i *Index) InteractiveImportRepo(dirPath string) { util.FatalIfError(repo.Move(checkoutPath, true)) i.Repos = append(i.Repos, &repo) } + +//InteractiveDropRepo deletes the given repo from the rtree and removes it from +//the index. +func (i *Index) InteractiveDropRepo(repo *Repo) { + ok := repo.InteractiveExec("git", "status") + if !ok { + return + } + if !cli.Confirm(">> Drop this repo?") { + return + } + + util.FatalIfError(os.RemoveAll(repo.AbsolutePath())) + + reposNew := make([]*Repo, 0, len(i.Repos)-1) + for _, r := range i.Repos { + if r.CheckoutPath != repo.CheckoutPath { + reposNew = append(reposNew, r) + } + } + i.Repos = reposNew +} diff --git a/pkg/rtree/main.go b/pkg/rtree/main.go index 4c8fb98..683daeb 100644 --- a/pkg/rtree/main.go +++ b/pkg/rtree/main.go @@ -38,7 +38,10 @@ func Exec(args []string) { } commandGet(args[1]) case "drop": - panic("unimplemented") + if len(args) != 2 { + usageAndExit() + } + commandDrop(args[1]) case "index": if len(args) != 1 { usageAndExit() @@ -88,6 +91,15 @@ func commandGet(url string) { } } +func commandDrop(url string) { + index := ReadIndex() + repo := index.InteractiveFindRepo(url, false) + if repo != nil { + index.InteractiveDropRepo(repo) + index.Write() + } +} + func commandIndex() { index := ReadIndex() util.FatalIfError(index.InteractiveRebuild()) -- cgit v1.3.1