From f23a5602ebfd41885c5e79d6ea4a52623d3ec25d Mon Sep 17 00:00:00 2001 From: Sebastian Feldmann Date: Mon, 9 Sep 2024 13:51:01 +0200 Subject: [PATCH] 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"} --- hooks/placeholder/placeholder.go | 3 ++ hooks/placeholder/stdin.go | 18 ++++++++++++ hooks/placeholder/stdin_test.go | 49 ++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 hooks/placeholder/stdin.go create mode 100644 hooks/placeholder/stdin_test.go diff --git a/hooks/placeholder/placeholder.go b/hooks/placeholder/placeholder.go index 5dbb2be..b95edcd 100644 --- a/hooks/placeholder/placeholder.go +++ b/hooks/placeholder/placeholder.go @@ -28,6 +28,9 @@ var ( files, _ := aContext.Repository().StagedFiles() return &FileList{name: "STAGED_FILES", context: aContext, files: files} }, + "STDIN": func(aContext *app.Context) Replacer { + return &StdIn{context: aContext} + }, } ) diff --git a/hooks/placeholder/stdin.go b/hooks/placeholder/stdin.go new file mode 100644 index 0000000..b6f4f6e --- /dev/null +++ b/hooks/placeholder/stdin.go @@ -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 +} diff --git a/hooks/placeholder/stdin_test.go b/hooks/placeholder/stdin_test.go new file mode 100644 index 0000000..e26dd34 --- /dev/null +++ b/hooks/placeholder/stdin_test.go @@ -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) + } +}