-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
51 lines (44 loc) · 1.25 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
// Copyright 2021 Jeffrey M Hodges.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"log"
"net/url"
"os"
"path/filepath"
"strings"
"golang.org/x/tools/go/vcs"
)
func main() {
log.SetFlags(0)
if len(os.Args) <= 1 || len(os.Args) > 2 {
log.Fatal("usage: grab REPO_URL")
}
importPath := os.Args[1]
// We parse as a URL to see if we need to strip out the leading scheme for
// the vcs library. The vcs library works on "import paths" a la Go, not URLs.
u, err := url.Parse(os.Args[1])
// If we can't parse it as a URL, it might still mean the vcs library knows
// how to handle it.
if err == nil {
u.Scheme = ""
importPath = strings.TrimLeft(u.String(), "/")
}
repoRoot, err := vcs.RepoRootForImportPath(importPath, false)
if err != nil {
log.Fatalf("unable to figure out the repo root from the given url: %s", err)
}
home := strings.TrimSpace(os.Getenv("GRAB_HOME"))
if home == "" {
userHome, err := os.UserHomeDir()
if err != nil {
log.Fatalf("unable to get current user's home directory: %s", err)
}
home = filepath.Join(userHome, "src")
}
root := filepath.Join(home, repoRoot.Root)
err = repoRoot.VCS.Create(root, repoRoot.Repo)
if err != nil {
log.Fatalf("unable to download %#v into %#v", repoRoot.Repo, root)
}
}