diff options
| author | Stefan Majewsky <majewsky@gmx.net> | 2017-04-04 22:13:33 +0200 |
|---|---|---|
| committer | Stefan Majewsky <majewsky@gmx.net> | 2017-04-04 22:13:33 +0200 |
| commit | ba69181d4d892aecfc35f9f30fe190d946662284 (patch) | |
| tree | a278e13d433dc9ade4b19d0ef60a5a5528dbf4ba /main.go | |
| parent | 5169b2d145b0086516491a35887bfc5eb07be2ae (diff) | |
| download | gofu-ba69181d4d892aecfc35f9f30fe190d946662284.tar.gz | |
implement subcommands: remotes, repos, each
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 65 |
1 files changed, 62 insertions, 3 deletions
@@ -21,7 +21,9 @@ package main import ( "fmt" "os" + "os/exec" "path/filepath" + "sort" "strings" ) @@ -40,13 +42,22 @@ func main() { } commandIndex() case "repos": - panic("unimplemented") + if len(os.Args) != 2 { + usageAndExit() + } + commandRepos() case "remotes": - panic("unimplemented") + if len(os.Args) != 2 { + usageAndExit() + } + commandRemotes() case "import": panic("unimplemented") case "each": - panic("unimplemented") + if len(os.Args) < 3 { + usageAndExit() + } + commandEach(os.Args[2], os.Args[3:]) default: usageAndExit() } @@ -133,3 +144,51 @@ func commandIndex() { newIndex.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) +} + +func commandEach(command string, args []string) { + index := ReadIndex() + + var paths []string + for _, repo := range index.Repos { + paths = append(paths, repo.AbsolutePath()) + } + sort.Strings(paths) + + hadErrors := false + for _, path := range paths { + fmt.Fprintf(os.Stdout, "\x1B[1;36m>> \x1B[0;36m%s\x1B[0m\n", path) + cmd := exec.Command(command, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.Dir = path + err := cmd.Run() + if err != nil { + fmt.Fprintf(os.Stderr, "\x1B[1;31m!! \x1B[0;31m%s\x1B[0m\n", err.Error()) + hadErrors = true + } + } + + if hadErrors { + os.Exit(1) + } +} |
