Skip to content

Commit a27f5db

Browse files
arschlesAaron Schlesinger
authored andcommitted
ref(gitreceive): move all gitreceive files under pkg/gitreceive
this prepares to merge all commands under a single go binary
1 parent 124bd21 commit a27f5db

4 files changed

Lines changed: 37 additions & 49 deletions

File tree

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package gitreceive
22

33
// #!/usr/bin/env bash
44
// #
@@ -198,3 +198,7 @@ package main
198198
// # cleanup
199199
// cd $REPO_DIR
200200
// git gc &>/dev/null
201+
202+
func build(conf *Config, newRev string) error {
203+
return nil
204+
}
Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
package main
1+
package gitreceive
22

3-
import (
4-
"github.com/kelseyhightower/envconfig"
5-
)
6-
7-
type config struct {
3+
type Config struct {
84
WorkflowHost string `envconfig:"deis_workflow_service_host"`
95
WorkflowPort string `envconfig:"deis_workflow_service_port"`
106
GitHome string `envconfig:"git_home"`
@@ -16,11 +12,3 @@ type config struct {
1612
App string `envconfing:"app"`
1713
Fingerprint string `envconfing:"fingerprint"`
1814
}
19-
20-
func getConfig(appName string) (*config, error) {
21-
conf := &config{}
22-
if err := envconfig(appName, conf); err != nil {
23-
return nil, err
24-
}
25-
return conf, nil
26-
}
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
package main
1+
package gitreceive
22

33
import (
44
"bytes"
5+
"encoding/json"
6+
"fmt"
57
"net/http"
68
)
79

@@ -21,26 +23,32 @@ import (
2123
// -d "{\"receive_user\": \"$username\", \"receive_repo\": \"$app\", \"sha\": \"$sha\", \"fingerprint\": \"$fingerprint\", \"ssh_connection\": \"$SSH_CONNECTION\", \"ssh_original_command\": \"$SSH_ORIGINAL_COMMAND\"}" \
2224
// --silent "http://$DEIS_WORKFLOW_SERVICE_HOST:$DEIS_WORKFLOW_SERVICE_PORT/v2/hooks/push" >/dev/null
2325

24-
func receive(conf *config, newRev string) error {
26+
func receive(conf *Config, newRev string) error {
2527
urlStr := fmt.Sprintf("http://%s:%s/v2/hooks/push", conf.WorkflowHost, conf.WorkflowPort)
2628
bodyMap := map[string]string{
27-
"receive_user": conf.User,
29+
"receive_user": conf.Username,
2830
"receive_repo": conf.App,
2931
"sha": conf.SHA,
3032
"fingerprint": conf.Fingerprint,
3133
"ssh_connection": conf.SSHConnection,
3234
"ssh_original_command": conf.SSHOriginalCommand,
3335
}
3436
var body bytes.Buffer
35-
if err := json.NewEncoder().Encode(&body, bodyMap); err != nil {
37+
if err := json.NewEncoder(&body).Encode(bodyMap); err != nil {
3638
return err
3739
}
3840
req, err := http.NewRequest("POST", urlStr, &body)
3941
if err != nil {
4042
return err
4143
}
42-
resp, err := http.Do(req)
44+
45+
// TODO: inspect resp for status code
46+
// TODO: use ctxhttp here (https://godoc.org/golang.org/x/net/context/ctxhttp)
47+
// resp, err := http.Do(req)
48+
_, err = http.DefaultClient.Do(req)
4349
if err != nil {
4450
return err
4551
}
52+
// TODO: inspect resp for status code
53+
return nil
4654
}
Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
package main
1+
package gitreceive
22

33
import (
44
"bufio"
55
"fmt"
6-
"github.com/helm/helm/log"
7-
"io"
86
"os"
7+
"strings"
98
)
109

1110
// #!/bin/bash
@@ -41,19 +40,6 @@ import (
4140
// fi
4241
// done
4342

44-
const newline = "\n"
45-
46-
// readLine reads from bio until it reaches a "\n". returns the line, not including the "\n"
47-
func getLine(bio *bufio.Reader) (string, error) {
48-
line, err := bio.ReadString(newline)
49-
if err != nil {
50-
return "", err
51-
}
52-
if strings.HasSuffix(line, newline) {
53-
return line[0 : len(line)-len(newline)]
54-
}
55-
}
56-
5743
func readLine(line string) (string, string, string, error) {
5844
spl := strings.Split(line, " ")
5945
if len(spl) != 3 {
@@ -62,22 +48,24 @@ func readLine(line string) (string, string, string, error) {
6248
return spl[0], spl[1], spl[2], nil
6349
}
6450

65-
func main() {
66-
conf, err := getConfig("gitreceive")
67-
if err != nil {
68-
log.Msg("config error [%s]", err)
69-
os.Exit(1)
70-
}
71-
bio := bufio.NewReader(os.Stdin)
72-
for line, err := getLine(bio); err != nil; {
73-
oldRev, newRev, refName, err := readLine(line)
51+
func Run(conf *Config) error {
52+
scanner := bufio.NewScanner(os.Stdin)
53+
for scanner.Scan() {
54+
line := scanner.Text()
55+
// oldRev, newRev, refName, err := readLine(line)
56+
_, newRev, _, err := readLine(line)
57+
if err != nil {
58+
return err
59+
}
7460
if err := receive(conf, newRev); err != nil {
75-
log.Die("failed on rev %s - push denied", newRev)
76-
os.Exit(1)
61+
return err
7762
}
7863
if err := build(conf, newRev); err != nil {
79-
log.Die("error building %s [%s]", newRev, err)
80-
os.Exit(1)
64+
return err
8165
}
8266
}
67+
if err := scanner.Err(); err != nil {
68+
return err
69+
}
70+
return nil
8371
}

0 commit comments

Comments
 (0)