summaryrefslogtreecommitdiff
path: root/main.go
blob: 34b6d35d9b33aad09553baf0352b790d44c94e60 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main

import (
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"syscall"
//	"gopkg.in/alecthomas/kingpin.v2"
)

//var (
//	currentDirectory, err = os.Getwd()
//	directory = kingpin.Flag("directory", "Directory to roam for git repositories.").Default(currentDirectory).Short('d').String()
//)

func main() {

	args := os.Args[1:]
	if len(args) > 0 {
		repo := args[0]
		FindRepos(repo)
	} else {
		repo, err := os.Getwd()
		if err != nil {
			log.Fatal(err)
		}
		FindRepos(repo)
	}
}

func FindRepos(directory string) []string {
	var gitRepositories []string
	files, err := ioutil.ReadDir(directory)

	if err != nil {
		log.Fatal(err)
	}

	for _, f := range files {
		if IsRepo((directory + "/" + f.Name())) == true {
			log.Println(f.Name() + " is a git repository")
		}
	}
	return gitRepositories
}

func IsRepo(directory string) bool {

	argstr := []string{ "-C", directory, "status"}

	cmd := exec.Command("git", argstr...)

	if err := cmd.Start(); err != nil {
		log.Fatalf("cmd.Start: %v")
	}

	if err := cmd.Wait(); err != nil {
		if exiterr, ok := err.(*exec.ExitError); ok {
			// The program has exited with an exit code != 0

			// Do nothing
			if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
				log.Printf(directory + " is not a git repository, git command return code: %d", status.ExitStatus())
			}
		} else {
			log.Fatalf("cmd.Wait: %v", err)
		}
	} else {
		return true
	}
	return false
}