Skip to content

Commit 25e161c

Browse files
Refactored out tty-prompt dependency
1 parent a8b097d commit 25e161c

File tree

8 files changed

+120
-39
lines changed

8 files changed

+120
-39
lines changed

Gemfile.lock

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
PATH
22
remote: .
33
specs:
4-
ruby-nginx (1.0.0.pre.beta.2)
4+
ruby-nginx (1.0.0.pre.beta.3)
55
erb
66
thor
77
tty-command (~> 0.10, >= 0.10.1)
8-
tty-prompt (>= 0.3.0)
98

109
GEM
1110
remote: https://rubygems.org/
@@ -77,19 +76,9 @@ GEM
7776
tty-color (0.6.0)
7877
tty-command (0.10.1)
7978
pastel (~> 0.8)
80-
tty-cursor (0.7.1)
81-
tty-prompt (0.23.1)
82-
pastel (~> 0.8)
83-
tty-reader (~> 0.8)
84-
tty-reader (0.9.0)
85-
tty-cursor (~> 0.7)
86-
tty-screen (~> 0.8)
87-
wisper (~> 2.0)
88-
tty-screen (0.8.2)
8979
unicode-display_width (3.1.4)
9080
unicode-emoji (~> 4.0, >= 4.0.4)
9181
unicode-emoji (4.0.4)
92-
wisper (2.0.1)
9382

9483
PLATFORMS
9584
aarch64-linux
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# frozen_string_literal: true
2+
3+
module Ruby
4+
module Nginx
5+
module Commands
6+
module Helpers
7+
module PromptHelper
8+
def yes?(question)
9+
return true if ENV["SKIP_PROMPT"]
10+
11+
retry_question = false
12+
13+
loop do
14+
response = ask_question(question)
15+
16+
if !response.nil?
17+
reset_line if retry_question
18+
return print_confirm(question, response)
19+
end
20+
21+
print_invalid_input && previous_line && reset_line
22+
retry_question = true
23+
end
24+
rescue Interrupt
25+
next_line && print_confirm(question, false)
26+
raise Ruby::Nginx::AbortError, "Operation aborted"
27+
end
28+
29+
private
30+
31+
def ask_question(question)
32+
print "[Ruby::Nginx] #{question} \e[90m(Y/n)\e[0m "
33+
34+
case $stdin.gets.chomp.downcase
35+
when "y", "yes"
36+
true
37+
when "n", "no"
38+
false
39+
end
40+
end
41+
42+
def print_confirm(question, allowed)
43+
previous_line && reset_line
44+
45+
print "[Ruby::Nginx] #{question} \e[32m#{allowed ? "yes" : "no"}\e[0m\n"
46+
allowed
47+
end
48+
49+
def reset_line
50+
# 2K = clear line, 1G = move to first column
51+
print "\e[2K\e[1G"
52+
true
53+
end
54+
55+
def previous_line
56+
print "\e[1A"
57+
true
58+
end
59+
60+
def next_line
61+
print "\n"
62+
true
63+
end
64+
65+
def print_invalid_input
66+
print "\e[31m>>\e[0m Invalid input."
67+
true
68+
end
69+
end
70+
end
71+
end
72+
end
73+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
require "tty/command"
4+
require_relative "prompt_helper"
5+
6+
module Ruby
7+
module Nginx
8+
module Commands
9+
module Helpers
10+
module SudoHelper
11+
include Ruby::Nginx::Commands::Helpers::PromptHelper
12+
13+
def sudoify(cmd, sudo, reason)
14+
return cmd unless sudo
15+
16+
if sudo_access? || yes?(reason)
17+
"sudo #{cmd}"
18+
else
19+
raise Ruby::Nginx::AbortError, "Operation aborted"
20+
end
21+
end
22+
23+
private
24+
25+
def sudo_access?
26+
TerminalCommand.new(cmd: "sudo -n true").run.success?
27+
end
28+
end
29+
end
30+
end
31+
end
32+
end

lib/ruby/nginx/commands/install_mkcert.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ def initialize
1414
def run
1515
return if installed?
1616

17+
puts "\e[0;33m#{cmd}\e[0m"
18+
1719
if yes?("Would you like to install mkcert?")
1820
super
1921
else

lib/ruby/nginx/commands/install_nginx.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ def initialize
1414
def run
1515
return if installed?
1616

17-
if yes?("Would you like to install Nginx?")
17+
puts "\e[0;33m#{cmd}\e[0m"
18+
19+
if yes?("Would you like to install NGINX?")
1820
super
1921
else
20-
raise Ruby::Nginx::AbortError, "Nginx is required to continue. Please install Nginx."
22+
raise Ruby::Nginx::AbortError, "NGINX is required to continue. Please install NGINX."
2123
end
2224
end
2325

lib/ruby/nginx/commands/terminal_command.rb

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# frozen_string_literal: true
22

33
require "tty/command"
4-
require "tty/prompt"
4+
require_relative "helpers/prompt_helper"
5+
require_relative "helpers/sudo_helper"
56

67
module Ruby
78
module Nginx
89
module Commands
910
class TerminalCommand
11+
include Ruby::Nginx::Commands::Helpers::PromptHelper
12+
include Ruby::Nginx::Commands::Helpers::SudoHelper
13+
1014
attr_reader :cmd, :user, :error_type, :printer, :result
1115

1216
def initialize(cmd:, user: nil, raise: nil, printer: :null)
@@ -22,28 +26,8 @@ def run
2226
raise @error_type, @result.err if error_type && @result.failure?
2327

2428
@result
25-
end
26-
27-
protected
28-
29-
def yes?(question)
30-
ENV["SKIP_PROMPT"] || TTY::Prompt.new.yes?("[Ruby::Nginx] #{question}")
31-
end
32-
33-
def sudoify(cmd, sudo, reason)
34-
return cmd unless sudo
35-
36-
if sudo_access? || yes?(reason)
37-
"sudo #{cmd}"
38-
else
39-
raise Ruby::Nginx::AbortError, "Operation aborted"
40-
end
41-
end
42-
43-
private
44-
45-
def sudo_access?
46-
TerminalCommand.new(cmd: "sudo -n true").run.success?
29+
rescue Interrupt
30+
raise Ruby::Nginx::AbortError, "Operation aborted"
4731
end
4832
end
4933
end

lib/ruby/nginx/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module Ruby
44
module Nginx
5-
VERSION = "1.0.0-beta.2"
5+
VERSION = "1.0.0-beta.3"
66

77
Version = Gem::Version
88
end

ruby-nginx.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ Gem::Specification.new do |spec|
2828
spec.add_dependency "erb"
2929
spec.add_dependency "thor"
3030
spec.add_dependency "tty-command", "~> 0.10", ">= 0.10.1"
31-
spec.add_dependency "tty-prompt", ">= 0.3.0"
3231

3332
# For more information and examples about making a new gem, checkout our
3433
# guide at: https://bundler.io/guides/creating_gem.html

0 commit comments

Comments
 (0)