-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
104 lines (91 loc) · 2.15 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
package main
import (
"github.com/google/go-github/github"
"fmt"
"os"
"os/exec"
"github.com/thisisfineio/variant"
"time"
"flag"
"strings"
)
var (
sep = string(os.PathSeparator)
path string
gopath = os.Getenv("GOPATH")
org string
update bool
// inline versions for now, maybe future use build tool to code gen and read from json
cur = &variant.Version{
Major: 0,
Minor: 1,
Date: time.Unix(1482639633, 0),
ReleaseType: variant.Beta,
Description: "The initial beta releae of 'fetch', designed to make it easy for developers to grab all of thisisfine.io's code and start contributing.",
}
Versions = &variant.Versions{
Current: cur,
History: []*variant.Version{
cur,
},
}
v = flag.Bool("version", false, "Prints the current version of fetch")
)
func init(){
flag.StringVar(&path, "path", gopath + fmt.Sprintf("%ssrc%sgithub.com%sthisisfineio", sep, sep, sep), "Sets the path to download repos to")
flag.StringVar(&org, "org", "thisisfineio", "Sets the organization to clone from")
flag.BoolVar(&update, "u", false, "Attempt to update existing repositories - will not stash changes, will fail if unstaged things exist")
}
func main(){
parse()
err := clone()
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
}
func clone() error {
client := github.NewClient(nil)
publicRepos, _, err := client.Repositories.ListByOrg(org, nil)
if err != nil {
return err
}
if _, err := os.Stat(path); os.IsNotExist(err) {
os.MkdirAll(path, 0755)
}
err = os.Chdir(path)
if err != nil {
return err
}
cmds := make([]*exec.Cmd, 0)
for _, r := range publicRepos {
cmd := exec.Command("git", "clone", *r.CloneURL)
cmd.Env = os.Environ()
cmds = append(cmds, cmd)
}
for _, cmd := range cmds {
o, err := cmd.Output()
if err != nil {
if e, ok := err.(*exec.ExitError); ok {
if strings.Contains(string(e.Stderr), "already exists and is not an empty directory.") {
// put git pull here
continue
}
fmt.Println(string(e.Stderr))
} else {
fmt.Print(string(o))
}
}
}
return nil
}
func parse() {
flag.Parse()
if *v {
version()
}
}
func version() {
fmt.Println("fetch version:", cur.VersionString())
os.Exit(0)
}