aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2017-04-04 22:13:33 +0200
committerStefan Majewsky <majewsky@gmx.net>2017-04-04 22:13:33 +0200
commitba69181d4d892aecfc35f9f30fe190d946662284 (patch)
treea278e13d433dc9ade4b19d0ef60a5a5528dbf4ba
parent5169b2d145b0086516491a35887bfc5eb07be2ae (diff)
downloadgofu-ba69181d4d892aecfc35f9f30fe190d946662284.tar.gz
implement subcommands: remotes, repos, each
-rw-r--r--main.go65
-rw-r--r--util.go7
2 files changed, 69 insertions, 3 deletions
diff --git a/main.go b/main.go
index 9027dbb..63b5f6c 100644
--- a/main.go
+++ b/main.go
@@ -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)
+ }
+}
diff --git a/util.go b/util.go
index a1cea5f..24a1693 100644
--- a/util.go
+++ b/util.go
@@ -22,9 +22,16 @@ import (
"bufio"
"fmt"
"os"
+ "sort"
"strings"
)
+//ShowSorted sorts the given lines and prints them on stdout.
+func ShowSorted(lines []string) {
+ sort.Strings(lines)
+ fmt.Println(strings.Join(lines, "\n"))
+}
+
//ShowError prints the given error on stderr if it is non-nil, or returns false otherwise.
func ShowError(err error) bool {
if err == nil {