From 589f990ab0fb853ffe95a829ee09115fe0688c70 Mon Sep 17 00:00:00 2001 From: Jake Faris Date: Fri, 21 Oct 2016 16:02:32 -0400 Subject: [PATCH 1/2] create .envious file if not exist --- lib/dotenvious/cli/envious_file_configuror.rb | 37 +++++++++++++++ lib/dotenvious/cli/main.rb | 2 + .../cli/envious_file_configuror_spec.rb | 46 +++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 lib/dotenvious/cli/envious_file_configuror.rb create mode 100644 spec/dotenvious/cli/envious_file_configuror_spec.rb diff --git a/lib/dotenvious/cli/envious_file_configuror.rb b/lib/dotenvious/cli/envious_file_configuror.rb new file mode 100644 index 0000000..a3b12c4 --- /dev/null +++ b/lib/dotenvious/cli/envious_file_configuror.rb @@ -0,0 +1,37 @@ +module Dotenvious + module CLI + class EnviousFileConfiguror + def run + unless envious_present? + puts "You don't have an .envious file present. Create one now? [y\\n]" + decision = STDIN.gets.strip.downcase + if decision == 'y' + create_blank_envious_file + end + end + end + + private + + def create_blank_envious_file + File.open('.envious', 'w') do |file| + file.write(configuration_skeletion) + end + end + + def configuration_skeletion + <<~CONFIG + ### Generated by dotenvious. Edit with care! + Dotenvious::Configuration.new do |config| + config.custom_variables = [] + config.optional_variables = [] + end + CONFIG + end + + def envious_present? + File.exist?('.envious') + end + end + end +end diff --git a/lib/dotenvious/cli/main.rb b/lib/dotenvious/cli/main.rb index 07d80d4..87f4813 100644 --- a/lib/dotenvious/cli/main.rb +++ b/lib/dotenvious/cli/main.rb @@ -1,11 +1,13 @@ require_relative 'env_file_consolidator' require_relative 'env_file_sorter' +require_relative 'envious_file_configuror' module Dotenvious module CLI class Main def run if ARGV[0].to_s.empty? + EnviousFileConfiguror.new.run EnvFileConsolidator.new.run elsif ARGV[0].to_s == '--sort' EnvFileSorter.new.run diff --git a/spec/dotenvious/cli/envious_file_configuror_spec.rb b/spec/dotenvious/cli/envious_file_configuror_spec.rb new file mode 100644 index 0000000..324952b --- /dev/null +++ b/spec/dotenvious/cli/envious_file_configuror_spec.rb @@ -0,0 +1,46 @@ +require 'spec_helper' + +describe Dotenvious::CLI::EnviousFileConfiguror do + describe '#run' do + context 'when no .envious file exists' do + before do + allow(File).to receive(:exist?).with('.envious').and_return false + end + + context 'and the user wants to create an .envious file' do + before do + expect(STDIN).to receive(:gets).and_return "y" + end + it 'creates the file' do + fake_file = double(File) + expect(File).to receive(:open).and_yield fake_file + expect(fake_file).to receive(:write) + + described_class.new.run + end + end + + context 'but the user does not want to create an .envious file' do + before do + expect(STDIN).to receive(:gets).and_return "n" + end + it 'does nothing' do + expect(File).not_to receive(:open) + + described_class.new.run + end + end + end + + context 'when an .envious file exists' do + before do + allow(File).to receive(:exist?).with('.envious').and_return true + end + it 'does not prompt or do anything' do + expect(STDIN).not_to receive(:gets) + + described_class.new.run + end + end + end +end From cfa3fa70d90567adc128744fdb7af393abec3c09 Mon Sep 17 00:00:00 2001 From: farisj Date: Sat, 22 Oct 2016 10:58:05 -0400 Subject: [PATCH 2/2] fix Main spec from stalling on STDIN --- spec/dotenvious/cli/main_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/dotenvious/cli/main_spec.rb b/spec/dotenvious/cli/main_spec.rb index b4fd966..e84458d 100644 --- a/spec/dotenvious/cli/main_spec.rb +++ b/spec/dotenvious/cli/main_spec.rb @@ -5,6 +5,7 @@ context 'with no flags' do before do allow(File).to receive(:read).and_return("") + allow(STDIN).to receive(:gets).and_return('n') end it 'begins the EnvFileConsolidator' do