-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (50 loc) · 1.29 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
package main
import (
"fmt"
"log"
"os"
"github.com/britter/gh-get/pkg/github"
"github.com/cli/go-gh"
)
func main() {
ghFolder := getGhFolder()
repositoryDefinition, err := getRepository()
if err != nil {
log.Fatal(err)
return
}
repository, err := github.Parse(repositoryDefinition)
if err != nil {
log.Fatal(err)
return
}
repoClone := []string{"repo", "clone", repositoryDefinition, ghFolder + "/" + repository.Owner + "/" + repository.Name}
stdOut, stdErr, err := gh.Exec(repoClone...)
if err != nil {
log.Fatal(err)
}
fmt.Println(stdOut.String())
fmt.Println(stdErr.String())
}
func getRepository() (string, error) {
args := os.Args[1:]
if len(args) < 1 {
return "", fmt.Errorf("No repository given for cloning. Please specify the repository to clone in '<owner>/<repository>' format.")
}
if len(args) > 1 {
return "", fmt.Errorf("To many arguments. Please specify a single repository to clone in '<owner>/<repository>' format.")
}
return args[0], nil
}
func getGhFolder() string {
repostoriesFolder := getenv("GH_GET_FOLDER", "github")
fallbackRoot := os.Getenv("HOME") + "/" + repostoriesFolder
return getenv("GH_GET_ROOT", fallbackRoot)
}
func getenv(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}