Skip to content

Commit f23a560

Browse files
Add STDNIN placeholder
In order to forward the original hook stdIn to other scripts the Cap'n now provides a STDIN placeholder. For example forwarding the stdIn to existing git-lfs hooks would look like this {"action": "echo {$STDIN} | hooks/git-lfs/pre-push"}
1 parent e280012 commit f23a560

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

hooks/placeholder/placeholder.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ var (
2828
files, _ := aContext.Repository().StagedFiles()
2929
return &FileList{name: "STAGED_FILES", context: aContext, files: files}
3030
},
31+
"STDIN": func(aContext *app.Context) Replacer {
32+
return &StdIn{context: aContext}
33+
},
3134
}
3235
)
3336

hooks/placeholder/stdin.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package placeholder
2+
3+
import (
4+
"github.com/captainhook-go/captainhook/hooks/app"
5+
"github.com/captainhook-go/captainhook/io"
6+
)
7+
8+
type StdIn struct {
9+
context *app.Context
10+
}
11+
12+
func (r *StdIn) Replacement(options map[string]string) string {
13+
esc := "'"
14+
if !io.AnswerToBool(io.MappedStringOrDefault(options, "escaped", "true")) {
15+
esc = ""
16+
}
17+
return esc + r.context.IO().Option("input", "") + esc
18+
}

hooks/placeholder/stdin_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package placeholder
2+
3+
import (
4+
"github.com/captainhook-go/captainhook/configuration"
5+
"github.com/captainhook-go/captainhook/git"
6+
"github.com/captainhook-go/captainhook/hooks/app"
7+
"github.com/captainhook-go/captainhook/io"
8+
"testing"
9+
)
10+
11+
func TestStdin(t *testing.T) {
12+
argMap := map[string]string{}
13+
optMap := map[string]string{"input": "foo bar"}
14+
expected := "'foo bar'"
15+
16+
config := configuration.NewConfiguration("foo", false)
17+
repo, _ := git.NewRepository(".git")
18+
ctx := app.NewContext(
19+
io.NewDefaultIO(io.NORMAL, optMap, argMap),
20+
config,
21+
repo,
22+
)
23+
opts := map[string]string{}
24+
placeholder := &StdIn{context: ctx}
25+
result := placeholder.Replacement(opts)
26+
if result != expected {
27+
t.Errorf("Replacement didn't work, got: %s, want: %s.", result, expected)
28+
}
29+
}
30+
31+
func TestStdinUnescaped(t *testing.T) {
32+
argMap := map[string]string{}
33+
optMap := map[string]string{"input": "foo bar"}
34+
expected := "foo bar"
35+
36+
config := configuration.NewConfiguration("foo", false)
37+
repo, _ := git.NewRepository(".git")
38+
ctx := app.NewContext(
39+
io.NewDefaultIO(io.NORMAL, optMap, argMap),
40+
config,
41+
repo,
42+
)
43+
opts := map[string]string{"escaped": "false"}
44+
placeholder := &StdIn{context: ctx}
45+
result := placeholder.Replacement(opts)
46+
if result != expected {
47+
t.Errorf("Replacement didn't work, got: %s, want: %s.", result, expected)
48+
}
49+
}

0 commit comments

Comments
 (0)