From 7e9dffe1b93d304d8500cc0a379a8a2f4e35a7a2 Mon Sep 17 00:00:00 2001 From: Mihai22125 Date: Tue, 9 Aug 2022 19:52:19 +0300 Subject: [PATCH] Add Key field to CustomCommandPrompt struct Add Form field to CustomCommandObjects struct Write user prompts responses to Form field Ensure that map keys exists Add form prompts integration test Remove redundant index --- pkg/config/user_config.go | 2 + .../custom_commands/handler_creator.go | 14 ++- .../tests/custom_commands/form_prompts.go | 88 ++++++++++++++++++ pkg/integration/tests/tests.go | 1 + pkg/utils/template.go | 2 +- .../expected/repo/.git_keep/COMMIT_EDITMSG | 1 + .../expected/repo/.git_keep/FETCH_HEAD | 0 .../form_prompts/expected/repo/.git_keep/HEAD | 1 + .../expected/repo/.git_keep/config | 8 ++ .../expected/repo/.git_keep/description | 1 + .../expected/repo/.git_keep/index | Bin 0 -> 65 bytes .../expected/repo/.git_keep/info/exclude | 6 ++ .../expected/repo/.git_keep/logs/HEAD | 1 + .../repo/.git_keep/logs/refs/heads/master | 1 + .../4b/825dc642cb6eb9a060e54bf8d69288fbee4904 | Bin 0 -> 15 bytes .../6c/d61dc75eb17cf3e01d4d5f8f2b38a73ed9be90 | Bin 0 -> 116 bytes .../expected/repo/.git_keep/refs/heads/master | 1 + .../form_prompts/expected/repo/myfile | 1 + 18 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 pkg/integration/tests/custom_commands/form_prompts.go create mode 100644 test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/COMMIT_EDITMSG create mode 100644 test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/FETCH_HEAD create mode 100644 test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/HEAD create mode 100644 test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/config create mode 100644 test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/description create mode 100644 test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/index create mode 100644 test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/info/exclude create mode 100644 test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/logs/HEAD create mode 100644 test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/logs/refs/heads/master create mode 100644 test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 create mode 100644 test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/objects/6c/d61dc75eb17cf3e01d4d5f8f2b38a73ed9be90 create mode 100644 test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/refs/heads/master create mode 100644 test/integration_new/custom_commands/form_prompts/expected/repo/myfile diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index 7e6a21656..f3ff1befb 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -314,6 +314,8 @@ type CustomCommand struct { } type CustomCommandPrompt struct { + Key string `yaml:"key"` + // one of 'input', 'menu', 'confirm', or 'menuFromCommand' Type string `yaml:"type"` diff --git a/pkg/gui/services/custom_commands/handler_creator.go b/pkg/gui/services/custom_commands/handler_creator.go index 6ac9fb733..839f70adf 100644 --- a/pkg/gui/services/custom_commands/handler_creator.go +++ b/pkg/gui/services/custom_commands/handler_creator.go @@ -45,8 +45,9 @@ func (self *HandlerCreator) call(customCommand config.CustomCommand) func() erro return func() error { sessionState := self.sessionStateLoader.call() promptResponses := make([]string, len(customCommand.Prompts)) + form := make(map[string]string) - f := func() error { return self.finalHandler(customCommand, sessionState, promptResponses) } + f := func() error { return self.finalHandler(customCommand, sessionState, promptResponses, form) } // if we have prompts we'll recursively wrap our confirm handlers with more prompts // until we reach the actual command @@ -60,10 +61,11 @@ func (self *HandlerCreator) call(customCommand config.CustomCommand) func() erro wrappedF := func(response string) error { promptResponses[idx] = response + form[prompt.Key] = response return g() } - resolveTemplate := self.getResolveTemplateFn(promptResponses, sessionState) + resolveTemplate := self.getResolveTemplateFn(form, promptResponses, sessionState) resolvedPrompt, err := self.resolver.resolvePrompt(&prompt, resolveTemplate) if err != nil { return self.c.Error(err) @@ -154,19 +156,21 @@ func (self *HandlerCreator) menuPromptFromCommand(prompt *config.CustomCommandPr type CustomCommandObjects struct { *SessionState PromptResponses []string + Form map[string]string } -func (self *HandlerCreator) getResolveTemplateFn(promptResponses []string, sessionState *SessionState) func(string) (string, error) { +func (self *HandlerCreator) getResolveTemplateFn(form map[string]string, promptResponses []string, sessionState *SessionState) func(string) (string, error) { objects := CustomCommandObjects{ SessionState: sessionState, PromptResponses: promptResponses, + Form: form, } return func(templateStr string) (string, error) { return utils.ResolveTemplate(templateStr, objects) } } -func (self *HandlerCreator) finalHandler(customCommand config.CustomCommand, sessionState *SessionState, promptResponses []string) error { - resolveTemplate := self.getResolveTemplateFn(promptResponses, sessionState) +func (self *HandlerCreator) finalHandler(customCommand config.CustomCommand, sessionState *SessionState, promptResponses []string, form map[string]string) error { + resolveTemplate := self.getResolveTemplateFn(form, promptResponses, sessionState) cmdStr, err := resolveTemplate(customCommand.Command) if err != nil { return self.c.Error(err) diff --git a/pkg/integration/tests/custom_commands/form_prompts.go b/pkg/integration/tests/custom_commands/form_prompts.go new file mode 100644 index 000000000..e4712dbcc --- /dev/null +++ b/pkg/integration/tests/custom_commands/form_prompts.go @@ -0,0 +1,88 @@ +package custom_commands + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var FormPrompts = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Using a custom command reffering prompt responses by name", + ExtraCmdArgs: "", + Skip: false, + SetupRepo: func(shell *Shell) { + shell.EmptyCommit("blah") + }, + SetupConfig: func(cfg *config.AppConfig) { + cfg.UserConfig.CustomCommands = []config.CustomCommand{ + { + Key: "a", + Context: "files", + Command: `echo "{{.Form.FileContent}}" > {{.Form.FileName}}`, + Prompts: []config.CustomCommandPrompt{ + { + Key: "FileName", + Type: "input", + Title: "Enter a file name", + }, + { + Key: "FileContent", + Type: "menu", + Title: "Choose file content", + Options: []config.CustomCommandMenuOption{ + { + Name: "foo", + Description: "Foo", + Value: "FOO", + }, + { + Name: "bar", + Description: "Bar", + Value: "BAR", + }, + { + Name: "baz", + Description: "Baz", + Value: "BAZ", + }, + }, + }, + { + Type: "confirm", + Title: "Are you sure?", + Body: "Are you REALLY sure you want to make this file? Up to you buddy.", + }, + }, + }, + } + }, + Run: func( + shell *Shell, + input *Input, + assert *Assert, + keys config.KeybindingConfig, + ) { + assert.WorkingTreeFileCount(0) + + input.PressKeys("a") + + assert.InPrompt() + assert.MatchCurrentViewTitle(Equals("Enter a file name")) + input.Type("myfile") + input.Confirm() + + assert.InMenu() + assert.MatchCurrentViewTitle(Equals("Choose file content")) + assert.MatchSelectedLine(Contains("foo")) + input.NextItem() + assert.MatchSelectedLine(Contains("bar")) + input.Confirm() + + assert.InConfirm() + assert.MatchCurrentViewTitle(Equals("Are you sure?")) + input.Confirm() + + assert.WorkingTreeFileCount(1) + assert.MatchSelectedLine(Contains("myfile")) + assert.MatchMainViewContent(Contains("BAR")) + }, +}) diff --git a/pkg/integration/tests/tests.go b/pkg/integration/tests/tests.go index 9280347be..eb2fd83fa 100644 --- a/pkg/integration/tests/tests.go +++ b/pkg/integration/tests/tests.go @@ -36,6 +36,7 @@ var tests = []*components.IntegrationTest{ bisect.FromOtherBranch, cherry_pick.CherryPick, cherry_pick.CherryPickConflicts, + custom_commands.FormPrompts, } func GetTests() []*components.IntegrationTest { diff --git a/pkg/utils/template.go b/pkg/utils/template.go index 6c147cd38..41388ae3a 100644 --- a/pkg/utils/template.go +++ b/pkg/utils/template.go @@ -7,7 +7,7 @@ import ( ) func ResolveTemplate(templateStr string, object interface{}) (string, error) { - tmpl, err := template.New("template").Parse(templateStr) + tmpl, err := template.New("template").Option("missingkey=error").Parse(templateStr) if err != nil { return "", err } diff --git a/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/COMMIT_EDITMSG b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/COMMIT_EDITMSG new file mode 100644 index 000000000..907b30816 --- /dev/null +++ b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/COMMIT_EDITMSG @@ -0,0 +1 @@ +blah diff --git a/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/FETCH_HEAD b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/FETCH_HEAD new file mode 100644 index 000000000..e69de29bb diff --git a/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/HEAD b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/HEAD new file mode 100644 index 000000000..cb089cd89 --- /dev/null +++ b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/HEAD @@ -0,0 +1 @@ +ref: refs/heads/master diff --git a/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/config b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/config new file mode 100644 index 000000000..596ebaeb3 --- /dev/null +++ b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/config @@ -0,0 +1,8 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = false + logallrefupdates = true +[user] + email = CI@example.com + name = CI diff --git a/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/description b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/description new file mode 100644 index 000000000..498b267a8 --- /dev/null +++ b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/description @@ -0,0 +1 @@ +Unnamed repository; edit this file 'description' to name the repository. diff --git a/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/index b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/index new file mode 100644 index 0000000000000000000000000000000000000000..65d675154f23ffb2d0196e017d44a5e7017550f5 GIT binary patch literal 65 zcmZ?q402{*U|<4bhL9jvS0E+HV4z^Y<=qr}%;|LA&IJiiy? 1660591942 +0000 commit (initial): blah diff --git a/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/logs/refs/heads/master b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/logs/refs/heads/master new file mode 100644 index 000000000..153f2ddc4 --- /dev/null +++ b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/logs/refs/heads/master @@ -0,0 +1 @@ +0000000000000000000000000000000000000000 6cd61dc75eb17cf3e01d4d5f8f2b38a73ed9be90 CI 1660591942 +0000 commit (initial): blah diff --git a/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 new file mode 100644 index 0000000000000000000000000000000000000000..adf64119a33d7621aeeaa505d30adb58afaa5559 GIT binary patch literal 15 Wcmb40ga783c@fD06pgwdlzIAH%k_z2tD-~*>p>VhDeFv^UV{S<1jF#)%xn- z>3->_8IYzB*TNKvvZ-Wbv`D(8l_jLKsTm29%b&it6XpdT^YXNBuJ6NcrPT>MGe+@3 W0j@Z3!;VR}KXa!?-rWzaL@TsV7dVap literal 0 HcmV?d00001 diff --git a/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/refs/heads/master b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/refs/heads/master new file mode 100644 index 000000000..4c2ab0871 --- /dev/null +++ b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/refs/heads/master @@ -0,0 +1 @@ +6cd61dc75eb17cf3e01d4d5f8f2b38a73ed9be90 diff --git a/test/integration_new/custom_commands/form_prompts/expected/repo/myfile b/test/integration_new/custom_commands/form_prompts/expected/repo/myfile new file mode 100644 index 000000000..ba578e48b --- /dev/null +++ b/test/integration_new/custom_commands/form_prompts/expected/repo/myfile @@ -0,0 +1 @@ +BAR