-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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"}
- Loading branch information
1 parent
e280012
commit f23a560
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package placeholder | ||
|
||
import ( | ||
"github.com/captainhook-go/captainhook/hooks/app" | ||
"github.com/captainhook-go/captainhook/io" | ||
) | ||
|
||
type StdIn struct { | ||
context *app.Context | ||
} | ||
|
||
func (r *StdIn) Replacement(options map[string]string) string { | ||
esc := "'" | ||
if !io.AnswerToBool(io.MappedStringOrDefault(options, "escaped", "true")) { | ||
esc = "" | ||
} | ||
return esc + r.context.IO().Option("input", "") + esc | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package placeholder | ||
|
||
import ( | ||
"github.com/captainhook-go/captainhook/configuration" | ||
"github.com/captainhook-go/captainhook/git" | ||
"github.com/captainhook-go/captainhook/hooks/app" | ||
"github.com/captainhook-go/captainhook/io" | ||
"testing" | ||
) | ||
|
||
func TestStdin(t *testing.T) { | ||
argMap := map[string]string{} | ||
optMap := map[string]string{"input": "foo bar"} | ||
expected := "'foo bar'" | ||
|
||
config := configuration.NewConfiguration("foo", false) | ||
repo, _ := git.NewRepository(".git") | ||
ctx := app.NewContext( | ||
io.NewDefaultIO(io.NORMAL, optMap, argMap), | ||
config, | ||
repo, | ||
) | ||
opts := map[string]string{} | ||
placeholder := &StdIn{context: ctx} | ||
result := placeholder.Replacement(opts) | ||
if result != expected { | ||
t.Errorf("Replacement didn't work, got: %s, want: %s.", result, expected) | ||
} | ||
} | ||
|
||
func TestStdinUnescaped(t *testing.T) { | ||
argMap := map[string]string{} | ||
optMap := map[string]string{"input": "foo bar"} | ||
expected := "foo bar" | ||
|
||
config := configuration.NewConfiguration("foo", false) | ||
repo, _ := git.NewRepository(".git") | ||
ctx := app.NewContext( | ||
io.NewDefaultIO(io.NORMAL, optMap, argMap), | ||
config, | ||
repo, | ||
) | ||
opts := map[string]string{"escaped": "false"} | ||
placeholder := &StdIn{context: ctx} | ||
result := placeholder.Replacement(opts) | ||
if result != expected { | ||
t.Errorf("Replacement didn't work, got: %s, want: %s.", result, expected) | ||
} | ||
} |