Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create .envious file if not exist #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions 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
2 changes: 2 additions & 0 deletions 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
Expand Down
46 changes: 46 additions & 0 deletions 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
1 change: 1 addition & 0 deletions spec/dotenvious/cli/main_spec.rb
Expand Up @@ -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
Expand Down