Skip to content

Commit

Permalink
✨ Allow to upload file contents
Browse files Browse the repository at this point in the history
Add now accepts file contents
  • Loading branch information
Jan Stevens committed Nov 2, 2017
1 parent a596594 commit 824970b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 34 deletions.
16 changes: 14 additions & 2 deletions lib/vaulty/cli/add.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ class Add < Command
attr_reader :catacomb, :data
# @param [Catacomb] catacomb instance
# @param [Hash] data
def initialize(catacomb:, data:)
# @param [Hash] files
def initialize(catacomb:, data: {}, files: {})
@catacomb = catacomb
@data = data
files_with_content = read_file_contents!(files)
@data = data.merge(files_with_content)
end

def call
Expand All @@ -27,6 +29,16 @@ def call
def matching_keys
@matching_keys ||= catacomb.matching_keys(data.keys)
end

def read_file_contents!(files)
files.each_with_object({}) do |(key, path), memo|
memo.merge!(key => read_file(path))
end
end

def read_file(path)
File.read(File.expand_path(path))
end
end
end
end
8 changes: 6 additions & 2 deletions lib/vaulty/cli_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ class CLIApp
arg(:path)
command :add do |c|
c.flag %i(secret s), desc: 'Key/Values to save', type: Array, multiple: true,
required: true, arg_name: 'key:secret'
required: true, arg_name: 'key:secret'

c.flag %i(file f), desc: 'Key/File to be uploaded', type: Array, multiple: true,
arg_name: 'key:/path/file.ext'

c.action do |_global_options, options, _args|
data = options[:secret].reduce({}, :merge)
files = options[:file].reduce({}, :merge)
catacomb = options[:catacomb]
Vaulty::CLI::Add.call(catacomb: catacomb, data: data)
Vaulty::CLI::Add.call(catacomb: catacomb, data: data, files: files)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/vaulty/output/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def initialize(data, header: DEFAULT_HEADER, highlight: {}, prompt:)

def render
table = TTY::Table.new(data, header: header, style: :markdown)
renderer = table.render(:ascii) do |render|
renderer = table.render(:ascii, column_widths: [30, 80]) do |render|
render.padding = [0, 2, 0, 2]
render.filter = @filter
end
Expand Down
59 changes: 30 additions & 29 deletions spec/cli/add_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
RSpec.describe Vaulty::CLI::Add do
let(:instance) { described_class.new(catacomb: catacomb, data: data) }
let(:instance) { described_class.new(catacomb: catacomb, data: data, files: files) }
let(:catacomb) { CatacombMock.new('secret/account') }
let(:data) { { key: 'value' } }
let(:files) { {} }
let(:confirmation) { true }
let(:existing_data) { {} }

Expand All @@ -27,13 +28,8 @@

it 'prints the resulting table' do
subject
expect(output).to include_output <<~TXT
+-------+---------+
| Key | Value |
+-------+---------+
| key | value |
+-------+---------+
TXT
expect(output).to include_output('key')
expect(output).to include_output('value')
end

context 'when existing data is present' do
Expand All @@ -43,13 +39,8 @@

it 'prints out the current table' do
subject
expect(output).to include_output <<~TXT
+------------+---------+
| Key | Value |
+------------+---------+
| existing | value |
+------------+---------+
TXT
expect(output).to include_output('existing')
expect(output).to include_output('value')
end

context 'when existing data matches the given data' do
Expand All @@ -59,13 +50,8 @@

it 'prints out the table with higlights' do
subject
expect(output).to include_output <<~TXT
+-------+---------+
| Key | Value |
+-------+---------+
| key | value |
+-------+---------+
TXT
expect(output).to include_output('key')
expect(output).to include_output('value')
end

context 'when answering no' do
Expand Down Expand Up @@ -93,16 +79,31 @@

it 'prints out the resulting table and higlights the values' do
subject
expect(output).to include_output <<~TXT
+-------+---------------+
| Key | Value |
+-------+---------------+
| key | other value |
+-------+---------------+
TXT
expect(output).to include_output('key')
expect(output).to include_output('other value')
end
end
end
end

context 'when files are provided' do
let(:files) { { file: [FIXTURE_PATH, 'secret.txt'].join('/') } }

it 'prints out a banner' do
subject
expect(output).to include_output('Current value "secret/account"')
end

it 'writes the file contents' do
expect(catacomb).to receive(:merge).with(key: 'value', file: "secret\n")
subject
end

it 'prints the resulting table' do
subject
expect(output).to include_output('file')
expect(output).to include_output("secret")
end
end
end
end
1 change: 1 addition & 0 deletions spec/fixtures/secret.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
secret
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

require 'vaulty'

FIXTURE_PATH = File.join([File.dirname(__FILE__), 'fixtures'])

RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
Expand Down

0 comments on commit 824970b

Please sign in to comment.