diff options
| author | Stefan Majewsky <majewsky@gmx.net> | 2017-05-02 21:22:20 +0200 |
|---|---|---|
| committer | Stefan Majewsky <majewsky@gmx.net> | 2017-05-02 21:22:20 +0200 |
| commit | 72733e8203654cd26464c8c5a6c955b025102cca (patch) | |
| tree | 4a8c312fb25dcf79f2cd61b88b7f2ff6baf8d996 | |
| parent | 6b382c55ed7885857a6232a0c8b086ab1f64985e (diff) | |
| download | gofu-72733e8203654cd26464c8c5a6c955b025102cca.tar.gz | |
restructure rtree into gofu (with rtree as the first applet)
| l--------- | .gopath/src/github.com/majewsky/gofu (renamed from .gopath/src/github.com/majewsky/rtree) | 0 | ||||
| -rw-r--r-- | Makefile | 9 | ||||
| -rw-r--r-- | main.go | 91 | ||||
| -rw-r--r-- | pkg/rtree/index.go (renamed from index.go) | 30 | ||||
| -rw-r--r-- | pkg/rtree/main.go | 115 | ||||
| -rw-r--r-- | pkg/rtree/remote.go (renamed from remote.go) | 6 | ||||
| -rw-r--r-- | pkg/rtree/repo.go (renamed from repo.go) | 6 | ||||
| -rw-r--r-- | pkg/util/util.go (renamed from util.go) | 2 |
8 files changed, 160 insertions, 99 deletions
diff --git a/.gopath/src/github.com/majewsky/rtree b/.gopath/src/github.com/majewsky/gofu index c866b86..c866b86 120000 --- a/.gopath/src/github.com/majewsky/rtree +++ b/.gopath/src/github.com/majewsky/gofu @@ -1,17 +1,18 @@ -PKG = github.com/majewsky/rtree +PKG = github.com/majewsky/gofu PREFIX = /usr -all: build/rtree +all: build/gofu GO = GOPATH=$(CURDIR)/.gopath GOBIN=$(CURDIR)/build go GO_BUILDFLAGS = GO_LDFLAGS = -s -w -build/rtree: FORCE +build/gofu: FORCE $(GO) install $(GO_BUILDFLAGS) -ldflags '$(GO_LDFLAGS)' '$(PKG)' install: FORCE all - install -D -m 0755 build/rtree "$(DESTDIR)$(PREFIX)/bin/rtree" + install -D -m 0755 build/gofu "$(DESTDIR)$(PREFIX)/bin/gofu" + ln -s gofu "$(DESTDIR)$(PREFIX)/bin/rtree" vendor: FORCE golangvend @@ -21,89 +21,28 @@ package main import ( "fmt" "os" + "path/filepath" + + "github.com/majewsky/gofu/pkg/rtree" ) func main() { - if len(os.Args) == 1 { - usageAndExit() - } - switch os.Args[1] { - case "get": - panic("unimplemented") - case "drop": - panic("unimplemented") - case "index": - if len(os.Args) != 2 { - usageAndExit() - } - commandIndex() - case "repos": - if len(os.Args) != 2 { - usageAndExit() - } - commandRepos() - case "remotes": - if len(os.Args) != 2 { - usageAndExit() - } - commandRemotes() - case "import": - panic("unimplemented") - case "each": - if len(os.Args) < 3 { - usageAndExit() - } - commandEach(os.Args[2], os.Args[3:]) - default: - usageAndExit() - } -} - -func usageAndExit() { - fmt.Fprintln(os.Stderr, "Usage:") - fmt.Fprintln(os.Stderr, " rtree [get|drop] <url>") - fmt.Fprintln(os.Stderr, " rtree [index|repos|remotes]") - fmt.Fprintln(os.Stderr, " rtree import <path>") - fmt.Fprintln(os.Stderr, " rtree each <command>") - os.Exit(1) -} - -func commandIndex() { - index := ReadIndex() - FatalIfError(index.InteractiveRebuild()) - index.Write() -} - -func commandRepos() { - index := ReadIndex() - var items []string - for _, repo := range index.Repos { - items = append(items, repo.CheckoutPath) - } - ShowSorted(items) -} - -func commandRemotes() { - index := ReadIndex() - var items []string - for _, repo := range index.Repos { - for _, remote := range repo.Remotes { - items = append(items, remote.URL) - } - } - ShowSorted(items) + execApplet(filepath.Base(os.Args[0]), os.Args[1:], true) } -func commandEach(command string, args []string) { - allOK := true - for _, repo := range ReadIndex().Repos { - ok := repo.InteractiveExec(command, args...) - if !ok { - allOK = false +func execApplet(applet string, args []string, allowGofu bool) { + //allow explicit specification of applet as "./build/gofu <applet> <args>" + if allowGofu && applet == "gofu" { + if len(args) == 0 { + fmt.Fprintln(os.Stderr, "Usage: gofu <applet> [args...]") + os.Exit(1) } + execApplet(args[0], args[1:], false) + return } - if !allOK { - os.Exit(1) + switch applet { + case "rtree": + rtree.Exec(args) } } diff --git a/index.go b/pkg/rtree/index.go index b579e7e..a4a0b74 100644 --- a/index.go +++ b/pkg/rtree/index.go @@ -16,7 +16,7 @@ * *******************************************************************************/ -package main +package rtree import ( "errors" @@ -27,6 +27,8 @@ import ( "sort" "strings" + "github.com/majewsky/gofu/pkg/util" + yaml "gopkg.in/yaml.v2" ) @@ -38,7 +40,7 @@ type Index struct { func indexPath() string { homeDir := os.Getenv("HOME") if homeDir == "" { - FatalIfError(errors.New("$HOME is not set (rtree needs the HOME variable to locate its index file)")) + util.FatalIfError(errors.New("$HOME is not set (rtree needs the HOME variable to locate its index file)")) } return filepath.Join(homeDir, ".rtree/index.yaml") } @@ -52,38 +54,38 @@ func ReadIndex() *Index { if os.IsNotExist(err) { return &Index{Repos: nil} } - FatalIfError(err) + util.FatalIfError(err) } //deserialize YAML var index Index - FatalIfError(yaml.Unmarshal(buf, &index)) + util.FatalIfError(yaml.Unmarshal(buf, &index)) //validate YAML valid := true for idx, repo := range index.Repos { if repo.CheckoutPath == "" { - ShowError(fmt.Errorf("missing \"repos[%d].path\"", idx)) + util.ShowError(fmt.Errorf("missing \"repos[%d].path\"", idx)) valid = false } if len(repo.Remotes) == 0 { - ShowError(fmt.Errorf("missing \"repos[%d].remotes\"", idx)) + util.ShowError(fmt.Errorf("missing \"repos[%d].remotes\"", idx)) valid = false } for idx2, remote := range repo.Remotes { switch { case remote.Name == "": - ShowError(fmt.Errorf("missing \"repos[%d].remotes[%d].name\"", idx, idx2)) + util.ShowError(fmt.Errorf("missing \"repos[%d].remotes[%d].name\"", idx, idx2)) valid = false case remote.URL == "": - ShowError(fmt.Errorf("missing \"repos[%d].remotes[%d].url\"", idx, idx2)) + util.ShowError(fmt.Errorf("missing \"repos[%d].remotes[%d].url\"", idx, idx2)) valid = false } } } if !valid { - FatalIfError(errors.New("index file is corrupted; see errors above")) + util.FatalIfError(errors.New("index file is corrupted; see errors above")) } sort.Sort(reposByAbsPath(index.Repos)) @@ -99,10 +101,10 @@ func (r reposByAbsPath) Swap(i, j int) { r[i], r[j] = r[j], r[i] } //Write writes the index file to disk. func (i *Index) Write() { buf, err := yaml.Marshal(i) - FatalIfError(err) + util.FatalIfError(err) path := indexPath() - FatalIfError(os.MkdirAll(filepath.Dir(path), 0755)) - FatalIfError(ioutil.WriteFile(path, buf, 0644)) + util.FatalIfError(os.MkdirAll(filepath.Dir(path), 0755)) + util.FatalIfError(ioutil.WriteFile(path, buf, 0644)) } //InteractiveRebuild implements the `rtree index` subcommand. @@ -140,12 +142,12 @@ func (i *Index) InteractiveRebuild() error { var choice string if len(remoteURLs) == 0 { - choice = Prompt( + choice = util.Prompt( "no remote to restore from; (d)elete from index or (s)kip?", []string{"d", "s"}, ) } else { - choice = Prompt( + choice = util.Prompt( fmt.Sprintf("(r)estore from %s, (d)elete from index, or (s)kip?", strings.Join(remoteURLs, " and ")), []string{"r", "d", "s"}, ) diff --git a/pkg/rtree/main.go b/pkg/rtree/main.go new file mode 100644 index 0000000..9513d06 --- /dev/null +++ b/pkg/rtree/main.go @@ -0,0 +1,115 @@ +/******************************************************************************* +* +* Copyright 2017 Stefan Majewsky <majewsky@gmx.net> +* +* This program is free software: you can redistribute it and/or modify it under +* the terms of the GNU General Public License as published by the Free Software +* Foundation, either version 3 of the License, or (at your option) any later +* version. +* +* This program is distributed in the hope that it will be useful, but WITHOUT ANY +* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +* A PARTICULAR PURPOSE. See the GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License along with +* this program. If not, see <http://www.gnu.org/licenses/>. +* +*******************************************************************************/ + +package rtree + +import ( + "fmt" + "os" + + "github.com/majewsky/gofu/pkg/util" +) + +//Exec executes the rtree applet and does not return. The argument is os.Args +//minus the leading "rtree" or "gofu rtree". +func Exec(args []string) { + if len(args) == 0 { + usageAndExit() + } + switch args[0] { + case "get": + panic("unimplemented") + case "drop": + panic("unimplemented") + case "index": + if len(args) != 1 { + usageAndExit() + } + commandIndex() + case "repos": + if len(args) != 1 { + usageAndExit() + } + commandRepos() + case "remotes": + if len(args) != 1 { + usageAndExit() + } + commandRemotes() + case "import": + panic("unimplemented") + case "each": + if len(args) < 2 { + usageAndExit() + } + commandEach(args[1], args[2:]) + default: + usageAndExit() + } + + os.Exit(0) +} + +func usageAndExit() { + fmt.Fprintln(os.Stderr, "Usage:") + fmt.Fprintln(os.Stderr, " rtree [get|drop] <url>") + fmt.Fprintln(os.Stderr, " rtree [index|repos|remotes]") + fmt.Fprintln(os.Stderr, " rtree import <path>") + fmt.Fprintln(os.Stderr, " rtree each <command>") + os.Exit(1) +} + +func commandIndex() { + index := ReadIndex() + util.FatalIfError(index.InteractiveRebuild()) + index.Write() +} + +func commandRepos() { + index := ReadIndex() + var items []string + for _, repo := range index.Repos { + items = append(items, repo.CheckoutPath) + } + util.ShowSorted(items) +} + +func commandRemotes() { + index := ReadIndex() + var items []string + for _, repo := range index.Repos { + for _, remote := range repo.Remotes { + items = append(items, remote.URL) + } + } + util.ShowSorted(items) +} + +func commandEach(command string, args []string) { + allOK := true + for _, repo := range ReadIndex().Repos { + ok := repo.InteractiveExec(command, args...) + if !ok { + allOK = false + } + } + + if !allOK { + os.Exit(1) + } +} diff --git a/remote.go b/pkg/rtree/remote.go index ed5efd7..4170717 100644 --- a/remote.go +++ b/pkg/rtree/remote.go @@ -16,13 +16,15 @@ * *******************************************************************************/ -package main +package rtree import ( "bytes" "os/exec" "regexp" "strings" + + "github.com/majewsky/gofu/pkg/util" ) //remoteAlias describes an alias that can be used in a Git remote URL (as @@ -38,7 +40,7 @@ func init() { cmd := exec.Command("git", "config", "--global", "-l") var buf bytes.Buffer cmd.Stdout = &buf - FatalIfError(cmd.Run()) + util.FatalIfError(cmd.Run()) rx := regexp.MustCompile(`^url\.([^=]+)\.insteadof=(.+)$`) for _, line := range strings.Split(string(buf.Bytes()), "\n") { diff --git a/repo.go b/pkg/rtree/repo.go index 532b5d1..f231d0a 100644 --- a/repo.go +++ b/pkg/rtree/repo.go @@ -16,7 +16,7 @@ * *******************************************************************************/ -package main +package rtree import ( "bytes" @@ -27,6 +27,8 @@ import ( "path/filepath" "regexp" "strings" + + "github.com/majewsky/gofu/pkg/util" ) //RootPath is the directory below which all repositories are located. Its value @@ -36,7 +38,7 @@ var RootPath string func init() { gopath := os.Getenv("GOPATH") if gopath == "" { - FatalIfError(errors.New("$GOPATH is not set (rtree needs the GOPATH variable to know where to look for and place repos)")) + util.FatalIfError(errors.New("$GOPATH is not set (rtree needs the GOPATH variable to know where to look for and place repos)")) } RootPath = filepath.Join(gopath, "src") } diff --git a/util.go b/pkg/util/util.go index 24a1693..3c5e724 100644 --- a/util.go +++ b/pkg/util/util.go @@ -16,7 +16,7 @@ * *******************************************************************************/ -package main +package util import ( "bufio" |
