This repository has been archived by the owner on Mar 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_uploader.go
93 lines (78 loc) · 2.55 KB
/
git_uploader.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
package main
import (
"fmt"
"log"
"math/rand"
"os"
"os/exec"
"time"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
)
func gitClone(repository string, branch string, directory string) {
log.Printf("git clone -b %s --single-branch %s %s", branch, repository, directory)
_, err := git.PlainClone(directory, false, &git.CloneOptions{
URL: repository,
ReferenceName: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", branch)),
SingleBranch: true,
Progress: os.Stdout,
})
if err == nil {
//Uploader call still on the same gorutine.
push2Pantheon(directory)
} else {
log.Print(err)
}
}
func getUploader() {
if _, err := os.Stat("./pantheon.py"); os.IsNotExist(err) {
const uploader_url = "https://raw.githubusercontent.com/redhataccess/pantheon/master/uploader/pantheon.py"
args := []string{"-o", "./pantheon.py", uploader_url}
cmd := exec.Command("curl", args...)
out, err := cmd.Output()
if err != nil {
//Uploader call still on the same gorutine.
log.Print(err)
}
log.Print("Successfully downloaded the uploader." + string(out))
}
log.Print("Setting uploader executable permissions.")
os.Chmod("./pantheon.py", 0755)
}
func push2Pantheon(directory string) {
if _, err := os.Stat(directory + "/pantheon2.yml"); os.IsNotExist(err) {
log.Print("pantheon2.yml was not found in the root of the repo, skipping upload.")
} else {
log.Print("Found pantheon2.yml in the root of the repo, uploading.")
var user = os.Getenv("UPLOADER_USER")
var password = os.Getenv("UPLOADER_PASSWORD")
var server = os.Getenv("PANTHEON_SERVER")
args := []string{"pantheon.py", "push", "--user", user, "--password", password, "--directory", directory, "--server", server}
if user == "" || password == "" || server == "" {
log.Print("Environment variables not found, using uploader and pantheon.yml settings.")
args = []string{"pantheon.py", "push", "--directory", directory}
}
//Now call python
cmd := exec.Command("python3", args...)
out, err := cmd.Output()
log.Print(err)
log.Print(string(out))
}
// Cleanup also happens in the same thread.
cleanup(directory)
}
func cleanup(directory string) {
//Todo
log.Printf("Cleanup: deleting directory: %s", directory)
os.RemoveAll(directory)
}
func randomAlphaNumericString() string {
const chars = "0123456789abcdefghijklmnopqrstuvwxyzREDHAT"
var seed *rand.Rand = rand.New(
rand.NewSource(time.Now().UnixNano()))
byteSlice := make([]byte, 10)
for i := range byteSlice {
byteSlice[i] = chars[seed.Intn(len(chars))]
}
return string(byteSlice)
}