-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
113 lines (99 loc) · 2.36 KB
/
main.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"github.com/steinfletcher/github-org-clone/cloner"
"github.com/steinfletcher/github-org-clone/github"
"github.com/steinfletcher/github-org-clone/shell"
"github.com/urfave/cli"
"log"
"os"
"time"
)
var (
version = "dev"
commit = ""
date = time.Now().String()
)
func main() {
app := cli.NewApp()
app.Author = "Stein Fletcher"
app.Name = "github-org-clone"
app.Usage = "clone github team repos"
app.UsageText = "github-org-clone -o MyOrg -t MyTeam"
app.Version = version
app.EnableBashCompletion = true
app.Description = "A simple cli to clone all the repos managed by a github team"
app.Metadata = map[string]interface{}{
"commit": commit,
"date": date,
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "org, o",
Usage: "github organisation",
},
cli.StringFlag{
Name: "team, t",
Usage: "github team",
},
cli.StringFlag{
Name: "username, u",
Usage: "github username",
EnvVar: "GITHUB_USER,GITHUB_USERNAME",
},
cli.StringFlag{
Name: "token, k",
Usage: "github personal access token",
EnvVar: "GITHUB_TOKEN,GITHUB_API_KEY,GITHUB_PERSONAL_ACCESS_TOKEN",
},
cli.StringFlag{
Name: "dir, d",
Usage: "directory to clone into. Defaults to the org name or org/team name if defined",
},
cli.StringFlag{
Name: "api, a",
Value: "https://api.github.com",
Usage: "github api url",
},
}
app.Action = func(c *cli.Context) error {
username := c.String("username")
token := c.String("token")
team := c.String("team")
org := c.String("org")
dir := c.String("dir")
api := c.String("api")
if len(username) == 0 {
die("env var GITHUB_USERNAME or flag -u must be set", c)
}
if len(token) == 0 {
die("env var GITHUB_TOKEN or flag -k must be set", c)
}
if len(org) == 0 {
die("github organisation (-o) not set", c)
}
if len(dir) == 0 {
if len(team) == 0 {
dir = org
} else {
if _, err := os.Stat(org); os.IsNotExist(err) {
os.Mkdir(org, os.ModePerm)
}
dir = fmt.Sprintf("%s/%s", org, team)
}
}
sh := shell.NewShell()
githubCli := github.NewGithub(username, token, api)
cl := cloner.NewCloner(githubCli, sh, dir)
err := cl.Clone(org, team)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
return nil
}
app.Run(os.Args)
}
func die(msg string, c *cli.Context) {
cli.ShowAppHelp(c)
log.Fatal(msg)
}