-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.go
More file actions
93 lines (74 loc) · 1.74 KB
/
fetch.go
File metadata and controls
93 lines (74 loc) · 1.74 KB
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
// parse domain
// construct clone URL (https://[domain][user][project])
// set path to $GITROOT/src/[domain]/[user]/[project] (reverse domain)
// (don't need) mkdir -p to path
// execute git clone into path
// pipe output to stdout
package main
import (
"context"
"fmt"
"flag"
"log"
"os"
"os/exec"
"path"
"strings"
"syscall"
"time"
)
const Version = "0.2.0"
var dirFlag = flag.Bool("d", false, "Print local directory of repo")
var versionFlag = flag.Bool("v", false, "Print version of goget")
func main() {
flag.Parse()
if *versionFlag {
fmt.Printf("goget v%s\n", Version)
return
}
if len(flag.Args()) < 1 {
fmt.Println("No package specified")
return
}
gitpath := os.Getenv("GITPATH")
if gitpath == "" {
fmt.Println("No GITPATH specified")
return
}
name := flag.Args()[0]
remoteurl := "https://" + name
pieces := strings.Split(name, "/")
withroot := append([]string{gitpath, "src"}, pieces...)
localdir := path.Join(withroot...)
if *dirFlag {
fmt.Print(localdir)
return
}
ctx, cancel := context.WithTimeout(
context.Background(),
30*time.Minute,
)
defer cancel()
cmd := exec.CommandContext(ctx, "git", "clone", remoteurl, localdir)
if err := cmd.Start(); err != nil {
log.Fatalf("cmd.Start: %v", err)
}
// Exit status capturing reference:
// https://stackoverflow.com/a/10385867/2684355
if err := cmd.Wait(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
switch status.ExitStatus() {
case 128:
fmt.Println("Error: destination path already exists")
default:
log.Fatalf("Failed for unaccounted reason")
}
}
} else {
log.Fatalf("Failed for unaccounted reason")
}
} else {
fmt.Print(localdir)
}
}