/
inspect.go
80 lines (72 loc) · 1.71 KB
/
inspect.go
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
75
76
77
78
79
80
package main
import (
"fmt"
"os"
"io/ioutil"
"github.com/olekukonko/tablewriter"
"path"
"sort"
)
type GitRepo struct {
name string
branch string
status string
path string
}
func findGitRepositories(dir string, gitRepos map[string]GitRepo) {
files, _ := ioutil.ReadDir(dir)
for _, f := range files {
dirName := dir + "/" + f.Name()
if f.IsDir() {
findGitRepositories(dirName, gitRepos)
if f.Name() == ".git" {
gitRepos[dir] = GitRepo{name: path.Base(dir), branch: GitBranch(dir), status: GitStatus(dir), path: path.Dir(dir)}
}
}
}
}
func InspectGit(root string) {
if _, err := os.Stat(root); err == nil {
fmt.Printf("Inspecting system for git repos at '%v'...\n", root)
gitRepos := make(map[string]GitRepo)
findGitRepositories(root, gitRepos)
if len(gitRepos) >= 0 {
keys := make([]string, len(gitRepos))
i := 0
for key := range gitRepos {
keys[i] = key
i++
}
sort.Strings(keys)
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Branch", "Status", "Path"})
for i := range keys {
repo := gitRepos[keys[i]]
v := []string{repo.name, repo.branch, repo.status, repo.path}
table.Append(v)
}
table.Render()
} else {
fmt.Printf("No repos found")
}
} else {
if os.IsNotExist(err) {
fmt.Printf("Directory '%v' does not exist\n", root)
os.Exit(1)
}
}
}
func Inspect(args []string) {
scope := args[0]
if scope == "git" {
var root string
if len(args) == 2 {
root = args[1]
} else {
root = "/"
}
InspectGit(root)
} else {
fmt.Printf("Unknown scope '%v'\n", scope)
}
}