Skip to content

Commit ba14fb2

Browse files
committedJun 23, 2016
Refactors
1 parent 763147a commit ba14fb2

12 files changed

+41
-45
lines changed
 

Diff for: ‎bitbucket_hook_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestBitbucketDeployPush(t *testing.T) {
11-
repo := &Repo{Branch: "master", Hook: HookConfig{Url: "/bitbucket_deploy"}}
11+
repo := &Repo{Branch: "master", Hook: HookConfig{URL: "/bitbucket_deploy"}}
1212
bbHook := BitbucketHook{}
1313

1414
for i, test := range []struct {

Diff for: ‎generic_hook_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestGenericDeployPush(t *testing.T) {
11-
repo := &Repo{Branch: "master", Hook: HookConfig{Url: "/generic_deploy"}}
11+
repo := &Repo{Branch: "master", Hook: HookConfig{URL: "/generic_deploy"}}
1212
gHook := GenericHook{}
1313

1414
for i, test := range []struct {

Diff for: ‎git_test.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"log"
66
"testing"
77
"time"
8-
"os"
98

109
"github.com/abiosoft/caddy-git/gittest"
1110
)
@@ -27,6 +26,7 @@ func TestInit(t *testing.T) {
2726
}
2827

2928
func TestHelpers(t *testing.T) {
29+
Init()
3030
f, err := writeScriptFile([]byte("script"))
3131
check(t, err)
3232
var b [6]byte
@@ -50,13 +50,6 @@ func TestHelpers(t *testing.T) {
5050
t.Errorf("Expected %v found %v", expectedWrapperScript, string(wScript))
5151
}
5252

53-
os.Setenv("TMPDIR", "/home/user/tmp")
54-
wScriptAltTmp := gitWrapperScript()
55-
if string(wScriptAltTmp) != expectedWrapperScriptAltTmp {
56-
t.Errorf("Expected %v found %v", expectedWrapperScriptAltTmp, string(wScriptAltTmp))
57-
}
58-
59-
6053
f, err = writeScriptFile(wScript)
6154
check(t, err)
6255

Diff for: ‎github_hook_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestGithubDeployPush(t *testing.T) {
11-
repo := &Repo{Branch: "master", Hook: HookConfig{ Url: "/github_deploy", Secret: "supersecret"} }
11+
repo := &Repo{Branch: "master", Hook: HookConfig{URL: "/github_deploy", Secret: "supersecret"}}
1212
ghHook := GithubHook{}
1313

1414
for i, test := range []struct {

Diff for: ‎gitlab_hook_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestGitlabDeployPush(t *testing.T) {
11-
repo := &Repo{Branch: "master", Hook: HookConfig{Url: "/gitlab_deploy"}}
11+
repo := &Repo{Branch: "master", Hook: HookConfig{URL: "/gitlab_deploy"}}
1212
glHook := GitlabHook{}
1313

1414
for i, test := range []struct {

Diff for: ‎gitos/gitos.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ type OS interface {
123123
// returns the resulting File.
124124
TempFile(string, string) (File, error)
125125

126+
// TempDir returns the default directory to use for temporary files.
127+
TempDir() string
128+
126129
// Sleep pauses the current goroutine for at least the duration d. A
127130
// negative or zero duration causes Sleep to return immediately.
128131
Sleep(time.Duration)
@@ -184,6 +187,11 @@ func (g GitOS) TempFile(dir, prefix string) (File, error) {
184187
return ioutil.TempFile(dir, prefix)
185188
}
186189

190+
// TempDir calls os.TempDir
191+
func (g GitOS) TempDir() string {
192+
return os.TempDir()
193+
}
194+
187195
// ReadDir calls ioutil.ReadDir.
188196
func (g GitOS) ReadDir(dirname string) ([]os.FileInfo, error) {
189197
return ioutil.ReadDir(dirname)
@@ -199,7 +207,7 @@ func (g GitOS) Sleep(d time.Duration) {
199207
time.Sleep(d)
200208
}
201209

202-
// New Ticker calls time.NewTicker.
210+
// NewTicker calls time.NewTicker.
203211
func (g GitOS) NewTicker(d time.Duration) Ticker {
204212
return &GitTicker{time.NewTicker(d)}
205213
}

Diff for: ‎gittest/gittest.go

+4
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ func (f fakeOS) TempFile(dir, prefix string) (gitos.File, error) {
186186
return &fakeFile{name: TempFileName, info: fakeInfo{name: TempFileName}}, nil
187187
}
188188

189+
func (f fakeOS) TempDir() string {
190+
return "/tmp"
191+
}
192+
189193
func (f fakeOS) ReadDir(dirname string) ([]os.FileInfo, error) {
190194
if f, ok := dirs[dirname]; ok {
191195
return f, nil

Diff for: ‎gogs_hook_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestGogsDeployPush(t *testing.T) {
11-
repo := &Repo{Branch: "master", Hook: HookConfig{Url: "/gogs_deploy"}}
11+
repo := &Repo{Branch: "master", Hook: HookConfig{URL: "/gogs_deploy"}}
1212
gsHook := GogsHook{}
1313

1414
for i, test := range []struct {

Diff for: ‎scripts.go

+16-25
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func Init() error {
4646
if _, err = gos.LookPath("bash"); err != nil {
4747
shell = "sh"
4848
if _, err = gos.LookPath("sh"); err != nil {
49-
return fmt.Errorf("git middleware requires either bash or sh.")
49+
return fmt.Errorf("git middleware requires either bash or sh")
5050
}
5151
}
5252
return nil
@@ -68,14 +68,6 @@ func writeScriptFile(content []byte) (file gitos.File, err error) {
6868
return file, file.Close()
6969
}
7070

71-
// replace tokens in a string
72-
func replaceString(s string, replacements map[string]string) string {
73-
for k, v := range replacements {
74-
s = strings.Replace(s, k, v, -1)
75-
}
76-
return s;
77-
}
78-
7971
// gitWrapperScript forms content for git.sh script
8072
func gitWrapperScript() []byte {
8173
scriptTemplate := `#!/bin/{shell}
@@ -109,17 +101,16 @@ fi
109101
{git_binary} "$@"
110102
111103
`
112-
replacements := map[string]string {
113-
"{shell}": shell,
114-
"{tmp_dir}": os.TempDir(),
115-
"{git_binary}": gitBinary,
116-
}
117-
118-
return []byte(replaceString(scriptTemplate, replacements))
104+
replacer := strings.NewReplacer(
105+
"{shell}", shell,
106+
"{tmp_dir}", strings.TrimSuffix(gos.TempDir(), "/"),
107+
"{git_binary}", gitBinary,
108+
)
109+
return []byte(replacer.Replace(scriptTemplate))
119110
}
120111

121112
// bashScript forms content of bash script to clone or update a repo using ssh
122-
func bashScript(gitSshPath string, repo *Repo, params []string) []byte {
113+
func bashScript(gitSSHPath string, repo *Repo, params []string) []byte {
123114
scriptTemplate := `#!/bin/{shell}
124115
125116
mkdir -p ~/.ssh;
@@ -128,12 +119,12 @@ ssh-keyscan -t rsa,dsa {repo_host} 2>&1 | sort -u - ~/.ssh/known_hosts > ~/.ssh/
128119
cat ~/.ssh/tmp_hosts >> ~/.ssh/known_hosts;
129120
{git_ssh_path} -i {ssh_key_path} {ssh_params};
130121
`
131-
replacements := map[string]string {
132-
"{shell}": shell,
133-
"{repo_host}": repo.Host,
134-
"{git_ssh_path}": gitSshPath,
135-
"{ssh_key_path}": repo.KeyPath,
136-
"{ssh_params}": strings.Join(params, " "),
137-
}
138-
return []byte(replaceString(scriptTemplate, replacements))
122+
replacer := strings.NewReplacer(
123+
"{shell}", shell,
124+
"{repo_host}", repo.Host,
125+
"{git_ssh_path}", gitSSHPath,
126+
"{ssh_key_path}", repo.KeyPath,
127+
"{ssh_params}", strings.Join(params, " "),
128+
)
129+
return []byte(replacer.Replace(scriptTemplate))
139130
}

Diff for: ‎setup.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func setup(c *caddy.Controller) error {
4646

4747
// If a HookUrl is set, we switch to event based pulling.
4848
// Install the url handler
49-
if repo.Hook.Url != "" {
49+
if repo.Hook.URL != "" {
5050

5151
hookRepos = append(hookRepos, repo)
5252

@@ -147,7 +147,7 @@ func parse(c *caddy.Controller) (Git, error) {
147147
if !c.NextArg() {
148148
return nil, c.ArgErr()
149149
}
150-
repo.Hook.Url = c.Val()
150+
repo.Hook.URL = c.Val()
151151

152152
// optional secret for validation
153153
if c.NextArg() {

Diff for: ‎setup_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func TestGitParse(t *testing.T) {
143143
KeyPath: "~/.key",
144144
URL: "ssh://git@bitbucket.org:2222/user/repo.git",
145145
Hook: HookConfig{
146-
Url: "/webhook",
146+
URL: "/webhook",
147147
Type: "gogs",
148148
},
149149
}},
@@ -155,7 +155,7 @@ func TestGitParse(t *testing.T) {
155155
KeyPath: "~/.key",
156156
URL: "ssh://git@bitbucket.org:2222/user/repo.git",
157157
Hook: HookConfig{
158-
Url: "/webhook",
158+
URL: "/webhook",
159159
Secret: "some-secrets",
160160
Type: "gogs",
161161
},

Diff for: ‎webhook.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type WebHook struct {
1616

1717
// HookConfig is a webhook handler configuration.
1818
type HookConfig struct {
19-
Url string // url to listen on for webhooks
19+
URL string // url to listen on for webhooks
2020
Secret string // secret to validate hooks
2121
Type string // type of Webhook
2222
}
@@ -83,7 +83,7 @@ func (h WebHook) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
8383

8484
for _, repo := range h.Repos {
8585

86-
if r.URL.Path == repo.Hook.Url {
86+
if r.URL.Path == repo.Hook.URL {
8787

8888
// if handler type is specified.
8989
if handler, ok := handlers[repo.Hook.Type]; ok {

0 commit comments

Comments
 (0)
Please sign in to comment.