This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pull.go
102 lines (87 loc) · 2.12 KB
/
pull.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
package main
import (
"fmt"
"os"
"path"
"github.com/go2c/optparse"
"github.com/onodera-punpun/go-utils/array"
"github.com/onodera-punpun/prt/git"
)
// pullCommand pulls in ports.
func pullCommand(input []string) error {
// Define valid arguments.
o := optparse.New()
argh := o.Bool("help", 'h', false)
// Parse arguments.
vals, err := o.Parse(input)
if err != nil {
return fmt.Errorf("invaild argument, use `-h` for a list of arguments")
}
// Print help.
if *argh {
fmt.Println("Usage: prt pull [arguments] [repos]")
fmt.Println("")
fmt.Println("arguments:")
fmt.Println(" -h, --help print help and exit")
return nil
}
// Count repos that need to be pulled.
var t int
if len(vals) == 0 {
t = len(config.Repo)
} else {
t = len(vals)
}
// TODO: Actually learn git and check if all these commands are needed.
var i int
for n, r := range config.Repo {
// Skip repos if needed.
if len(vals) != 0 {
if !array.ContainsString(vals, n) {
continue
}
}
i++
fmt.Printf("Pulling in repo %d/%d, %s.\n", i, t, light(n))
l := path.Join(config.PrtDir, n)
g := git.Repo{
Location: l,
URL: r.URL,
Branch: r.Branch,
}
// Check if location exists, clone if it doesn't.
if _, err := os.Stat(l); err != nil {
if err := g.Clone(); err != nil {
fmt.Fprintf(os.Stderr, "%s%s\n", warning(config.WarningChar),
err)
}
continue
}
if err := g.Checkout(); err != nil {
fmt.Fprintf(os.Stderr, "%s%s\n", warning(config.WarningChar), err)
continue
}
if err := g.Fetch(); err != nil {
fmt.Fprintf(os.Stderr, "%s%s\n", warning(config.WarningChar), err)
continue
}
// Print changes.
dl, err := g.Diff()
if err != nil {
fmt.Fprintf(os.Stderr, "%s%s\n", warning(config.WarningChar), err)
continue
}
for _, d := range dl {
fmt.Printf("%s%s\n", dark(config.IndentChar), d)
}
if err := g.Clean(); err != nil {
fmt.Fprintf(os.Stderr, "%s%s\n", warning(config.WarningChar), err)
continue
}
if err := g.Reset(); err != nil {
fmt.Fprintf(os.Stderr, "%s%s\n", warning(config.WarningChar), err)
continue
}
}
return nil
}