Skip to content

Commit

Permalink
Merge pull request #2055 from rolandasb/user-input-prompt-option
Browse files Browse the repository at this point in the history
Add custom prompt support for user input
  • Loading branch information
leehambley authored Apr 19, 2020
2 parents 8f3aca4 + 50dd0ee commit 46e00de
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/documentation/getting-started/user-input/index.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ server 'example.com', user: 'ssh_user_name', port: 22, password: fetch(:password
```


You can also show your own message by using `prompt` option:

```ruby
ask(:breakfast, "pancakes", prompt: "What's for breakfast?")
```

**Important!** `ask` will not prompt the user immediately. The question is
deferred until the first time `fetch` is used to obtain the setting. That means
you can `ask` for many variables, but only the variables used by your task(s)
Expand Down
10 changes: 9 additions & 1 deletion lib/capistrano/configuration/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ def gets
end

def question
if default.nil?
if prompt && default.nil?
I18n.t(:question_prompt, key: prompt, scope: :capistrano)
elsif prompt
I18n.t(:question_prompt_default, key: prompt, default_value: default, scope: :capistrano)
elsif default.nil?
I18n.t(:question, key: key, scope: :capistrano)
else
I18n.t(:question_default, key: key, default_value: default, scope: :capistrano)
Expand All @@ -63,6 +67,10 @@ def echo?
def stdin
(options || {}).fetch(:stdin, $stdin)
end

def prompt
(options || {}).fetch(:prompt, nil)
end
end
end
end
2 changes: 2 additions & 0 deletions lib/capistrano/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
written_file: "create %{file}",
question: "Please enter %{key}: ",
question_default: "Please enter %{key} (%{default_value}): ",
question_prompt: "%{key}: ",
question_prompt_default: "%{key} (%{default_value}): ",
keeping_releases: "Keeping %{keep_releases} of %{releases} deployed releases on %{host}",
skip_cleanup: "Skipping cleanup of invalid releases on %{host}; unexpected foldername found (should be timestamp)",
wont_delete_current_release: "Current release was marked for being removed but it's going to be skipped on %{host}",
Expand Down
18 changes: 18 additions & 0 deletions spec/lib/capistrano/configuration/question_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class Configuration
let(:question) { Question.new(key, default, stdin: stdin) }
let(:question_without_echo) { Question.new(key, default, echo: false, stdin: stdin) }
let(:question_without_default) { Question.new(key, nil, stdin: stdin) }
let(:question_prompt) { Question.new(key, default, stdin: stdin, prompt: "Your favorite branch") }
let(:question_prompt_without_default) { Question.new(key, nil, stdin: stdin, prompt: "Your favorite branch") }
let(:default) { :default }
let(:key) { :branch }
let(:stdin) { stub(tty?: true) }
Expand Down Expand Up @@ -43,6 +45,22 @@ class Configuration

expect(question_without_default.call).to eq(branch)
end

it "uses prompt and returns the value" do
$stdout.expects(:print).with("Your favorite branch (default): ")
stdin.expects(:gets).returns(branch)
stdin.expects(:noecho).never

expect(question_prompt.call).to eq(branch)
end

it "uses prompt and returns the value but has no default between parenthesis" do
$stdout.expects(:print).with("Your favorite branch: ")
stdin.expects(:gets).returns(branch)
stdin.expects(:noecho).never

expect(question_prompt_without_default.call).to eq(branch)
end
end

context "value is not entered" do
Expand Down

0 comments on commit 46e00de

Please sign in to comment.