Commit b722b62a authored by Brett Walker's avatar Brett Walker

Use Readline.readline for feature_flag binary

parent 65b7fc1a
...@@ -9,6 +9,7 @@ require 'optparse' ...@@ -9,6 +9,7 @@ require 'optparse'
require 'yaml' require 'yaml'
require 'fileutils' require 'fileutils'
require 'uri' require 'uri'
require 'readline'
require_relative '../lib/feature/shared' unless defined?(Feature::Shared) require_relative '../lib/feature/shared' unless defined?(Feature::Shared)
...@@ -114,8 +115,7 @@ class FeatureFlagOptionParser ...@@ -114,8 +115,7 @@ class FeatureFlagOptionParser
$stdout.puts ">> Specify the group introducing the feature flag, like `group::apm`:" $stdout.puts ">> Specify the group introducing the feature flag, like `group::apm`:"
loop do loop do
$stdout.print "?> " group = Readline.readline('?> ', false)&.strip
group = $stdin.gets.strip
group = nil if group.empty? group = nil if group.empty?
return group if group.nil? || group.start_with?('group::') return group if group.nil? || group.start_with?('group::')
...@@ -137,9 +137,7 @@ class FeatureFlagOptionParser ...@@ -137,9 +137,7 @@ class FeatureFlagOptionParser
end end
loop do loop do
$stdout.print "?> " type = Readline.readline('?> ', false)&.strip&.to_sym
type = $stdin.gets.strip.to_sym
return type if TYPES[type] && !TYPES[type][:deprecated] return type if TYPES[type] && !TYPES[type][:deprecated]
$stderr.puts "Invalid type specified '#{type}'" $stderr.puts "Invalid type specified '#{type}'"
...@@ -151,8 +149,7 @@ class FeatureFlagOptionParser ...@@ -151,8 +149,7 @@ class FeatureFlagOptionParser
$stdout.puts ">> URL of the MR introducing the feature flag (enter to skip):" $stdout.puts ">> URL of the MR introducing the feature flag (enter to skip):"
loop do loop do
$stdout.print "?> " introduced_by_url = Readline.readline('?> ', false)&.strip
introduced_by_url = $stdin.gets.strip
introduced_by_url = nil if introduced_by_url.empty? introduced_by_url = nil if introduced_by_url.empty?
return introduced_by_url if introduced_by_url.nil? || introduced_by_url.start_with?('https://') return introduced_by_url if introduced_by_url.nil? || introduced_by_url.start_with?('https://')
...@@ -184,8 +181,7 @@ class FeatureFlagOptionParser ...@@ -184,8 +181,7 @@ class FeatureFlagOptionParser
$stdout.puts ">> URL of the rollout issue (enter to skip):" $stdout.puts ">> URL of the rollout issue (enter to skip):"
loop do loop do
$stdout.print "?> " created_url = Readline.readline('?> ', false)&.strip
created_url = $stdin.gets.strip
created_url = nil if created_url.empty? created_url = nil if created_url.empty?
return created_url if created_url.nil? || created_url.start_with?('https://') return created_url if created_url.nil? || created_url.start_with?('https://')
...@@ -218,7 +214,7 @@ class FeatureFlagCreator ...@@ -218,7 +214,7 @@ class FeatureFlagCreator
assert_name! assert_name!
assert_existing_feature_flag! assert_existing_feature_flag!
# Read type from $stdin unless is already set # Read type from stdin unless is already set
options.type ||= FeatureFlagOptionParser.read_type options.type ||= FeatureFlagOptionParser.read_type
options.ee ||= FeatureFlagOptionParser.read_ee_only(options) options.ee ||= FeatureFlagOptionParser.read_ee_only(options)
options.group ||= FeatureFlagOptionParser.read_group options.group ||= FeatureFlagOptionParser.read_group
......
...@@ -25,7 +25,7 @@ RSpec.describe 'bin/feature-flag' do ...@@ -25,7 +25,7 @@ RSpec.describe 'bin/feature-flag' do
allow(File).to receive(:write).and_return(true) allow(File).to receive(:write).and_return(true)
# ignore stdin # ignore stdin
allow($stdin).to receive(:gets).and_raise('EOF') allow(Readline).to receive(:readline).and_raise('EOF')
end end
subject { creator.execute } subject { creator.execute }
...@@ -135,8 +135,8 @@ RSpec.describe 'bin/feature-flag' do ...@@ -135,8 +135,8 @@ RSpec.describe 'bin/feature-flag' do
let(:type) { 'deprecated' } let(:type) { 'deprecated' }
it 'shows error message and retries' do it 'shows error message and retries' do
expect($stdin).to receive(:gets).and_return(type) expect(Readline).to receive(:readline).and_return(type)
expect($stdin).to receive(:gets).and_raise('EOF') expect(Readline).to receive(:readline).and_raise('EOF')
expect do expect do
expect { described_class.read_type }.to raise_error(/EOF/) expect { described_class.read_type }.to raise_error(/EOF/)
...@@ -154,8 +154,8 @@ RSpec.describe 'bin/feature-flag' do ...@@ -154,8 +154,8 @@ RSpec.describe 'bin/feature-flag' do
) )
end end
it 'reads type from $stdin' do it 'reads type from stdin' do
expect($stdin).to receive(:gets).and_return(type) expect(Readline).to receive(:readline).and_return(type)
expect do expect do
expect(described_class.read_type).to eq(:development) expect(described_class.read_type).to eq(:development)
end.to output(/Specify the feature flag type/).to_stdout end.to output(/Specify the feature flag type/).to_stdout
...@@ -165,8 +165,8 @@ RSpec.describe 'bin/feature-flag' do ...@@ -165,8 +165,8 @@ RSpec.describe 'bin/feature-flag' do
let(:type) { 'invalid' } let(:type) { 'invalid' }
it 'shows error message and retries' do it 'shows error message and retries' do
expect($stdin).to receive(:gets).and_return(type) expect(Readline).to receive(:readline).and_return(type)
expect($stdin).to receive(:gets).and_raise('EOF') expect(Readline).to receive(:readline).and_raise('EOF')
expect do expect do
expect { described_class.read_type }.to raise_error(/EOF/) expect { described_class.read_type }.to raise_error(/EOF/)
...@@ -180,8 +180,8 @@ RSpec.describe 'bin/feature-flag' do ...@@ -180,8 +180,8 @@ RSpec.describe 'bin/feature-flag' do
describe '.read_group' do describe '.read_group' do
let(:group) { 'group::memory' } let(:group) { 'group::memory' }
it 'reads type from $stdin' do it 'reads type from stdin' do
expect($stdin).to receive(:gets).and_return(group) expect(Readline).to receive(:readline).and_return(group)
expect do expect do
expect(described_class.read_group).to eq('group::memory') expect(described_class.read_group).to eq('group::memory')
end.to output(/Specify the group introducing the feature flag/).to_stdout end.to output(/Specify the group introducing the feature flag/).to_stdout
...@@ -191,8 +191,8 @@ RSpec.describe 'bin/feature-flag' do ...@@ -191,8 +191,8 @@ RSpec.describe 'bin/feature-flag' do
let(:type) { 'invalid' } let(:type) { 'invalid' }
it 'shows error message and retries' do it 'shows error message and retries' do
expect($stdin).to receive(:gets).and_return(type) expect(Readline).to receive(:readline).and_return(type)
expect($stdin).to receive(:gets).and_raise('EOF') expect(Readline).to receive(:readline).and_raise('EOF')
expect do expect do
expect { described_class.read_group }.to raise_error(/EOF/) expect { described_class.read_group }.to raise_error(/EOF/)
...@@ -205,8 +205,8 @@ RSpec.describe 'bin/feature-flag' do ...@@ -205,8 +205,8 @@ RSpec.describe 'bin/feature-flag' do
describe '.read_introduced_by_url' do describe '.read_introduced_by_url' do
let(:url) { 'https://merge-request' } let(:url) { 'https://merge-request' }
it 'reads type from $stdin' do it 'reads type from stdin' do
expect($stdin).to receive(:gets).and_return(url) expect(Readline).to receive(:readline).and_return(url)
expect do expect do
expect(described_class.read_introduced_by_url).to eq('https://merge-request') expect(described_class.read_introduced_by_url).to eq('https://merge-request')
end.to output(/URL of the MR introducing the feature flag/).to_stdout end.to output(/URL of the MR introducing the feature flag/).to_stdout
...@@ -216,7 +216,7 @@ RSpec.describe 'bin/feature-flag' do ...@@ -216,7 +216,7 @@ RSpec.describe 'bin/feature-flag' do
let(:url) { '' } let(:url) { '' }
it 'skips entry' do it 'skips entry' do
expect($stdin).to receive(:gets).and_return(url) expect(Readline).to receive(:readline).and_return(url)
expect do expect do
expect(described_class.read_introduced_by_url).to be_nil expect(described_class.read_introduced_by_url).to be_nil
end.to output(/URL of the MR introducing the feature flag/).to_stdout end.to output(/URL of the MR introducing the feature flag/).to_stdout
...@@ -227,8 +227,8 @@ RSpec.describe 'bin/feature-flag' do ...@@ -227,8 +227,8 @@ RSpec.describe 'bin/feature-flag' do
let(:url) { 'invalid' } let(:url) { 'invalid' }
it 'shows error message and retries' do it 'shows error message and retries' do
expect($stdin).to receive(:gets).and_return(url) expect(Readline).to receive(:readline).and_return(url)
expect($stdin).to receive(:gets).and_raise('EOF') expect(Readline).to receive(:readline).and_raise('EOF')
expect do expect do
expect { described_class.read_introduced_by_url }.to raise_error(/EOF/) expect { described_class.read_introduced_by_url }.to raise_error(/EOF/)
...@@ -242,8 +242,8 @@ RSpec.describe 'bin/feature-flag' do ...@@ -242,8 +242,8 @@ RSpec.describe 'bin/feature-flag' do
let(:options) { double('options', name: 'foo', type: :development) } let(:options) { double('options', name: 'foo', type: :development) }
let(:url) { 'https://issue' } let(:url) { 'https://issue' }
it 'reads type from $stdin' do it 'reads type from stdin' do
expect($stdin).to receive(:gets).and_return(url) expect(Readline).to receive(:readline).and_return(url)
expect do expect do
expect(described_class.read_rollout_issue_url(options)).to eq('https://issue') expect(described_class.read_rollout_issue_url(options)).to eq('https://issue')
end.to output(/URL of the rollout issue/).to_stdout end.to output(/URL of the rollout issue/).to_stdout
...@@ -253,8 +253,8 @@ RSpec.describe 'bin/feature-flag' do ...@@ -253,8 +253,8 @@ RSpec.describe 'bin/feature-flag' do
let(:type) { 'invalid' } let(:type) { 'invalid' }
it 'shows error message and retries' do it 'shows error message and retries' do
expect($stdin).to receive(:gets).and_return(type) expect(Readline).to receive(:readline).and_return(type)
expect($stdin).to receive(:gets).and_raise('EOF') expect(Readline).to receive(:readline).and_raise('EOF')
expect do expect do
expect { described_class.read_rollout_issue_url(options) }.to raise_error(/EOF/) expect { described_class.read_rollout_issue_url(options) }.to raise_error(/EOF/)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment