Skip to content

Commit aea8af9

Browse files
authored
Add CLI to use in any repo (#1)
This opens up the tool to be used with any repo/project, rather than just rails. ## Usage ```shell diffcrypt decrypt -k $(cat test/fixtures/master.key) test/fixtures/example.yml.enc > tmp/decrypted.yml diffcrypt encrypt -k $(cat test/fixtures/master.key) test/fixtures/example.yml > tmp/encrypted.yml ```
1 parent 7064e6d commit aea8af9

File tree

6 files changed

+78
-2
lines changed

6 files changed

+78
-2
lines changed

Diff for: CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88

99

10+
## [Unreleased]
11+
12+
## Added
13+
14+
- CLI: Use diffcrypt from command line of any project without requiring ruby integration
15+
- CLI: `diffcrypt encrypt` Directly encrypt any file and output the contents
16+
- CLI: `diffcrypt decrypt` Directly decrypt any file and output the contents
17+
18+
19+
1020
## [0.2.0] - 2020-06-28
1121

1222
### Added

Diff for: bin/diffcrypt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require 'thor'
5+
6+
require_relative '../lib/diffcrypt/cli'
7+
8+
Diffcrypt::CLI.start(ARGV)

Diff for: diffcrypt.gemspec

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ Gem::Specification.new do |spec|
2525
spec.files = Dir.chdir(File.expand_path(__dir__)) do
2626
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
2727
end
28-
spec.bindir = 'exe'
29-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28+
spec.bindir = 'bin'
29+
spec.executables = %w[diffcrypt]
3030
spec.require_paths = ['lib']
3131

3232
spec.add_runtime_dependency 'activesupport', '~> 6.0.0'
33+
spec.add_runtime_dependency 'thor', '~> 1.0.1'
3334
end

Diff for: lib/diffcrypt/cli.rb

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
require_relative './encryptor'
4+
require_relative './version'
5+
6+
module Diffcrypt
7+
class CLI < Thor
8+
desc 'decrypt <path>', 'Decrypt a file'
9+
method_option :key, aliases: %i[k], required: true
10+
def decrypt(path)
11+
ensure_file_exists(path)
12+
contents = File.read(path)
13+
puts encryptor.decrypt(contents)
14+
end
15+
16+
desc 'encrypt <path>', 'Encrypt a file'
17+
method_option :key, aliases: %i[k], required: true
18+
def encrypt(path)
19+
ensure_file_exists(path)
20+
contents = File.read(path)
21+
puts encryptor.encrypt(contents)
22+
end
23+
24+
desc 'version', 'Show client version'
25+
def version
26+
say Diffcrypt::VERSION
27+
end
28+
29+
no_commands do
30+
def key
31+
options[:key]
32+
end
33+
34+
def encryptor
35+
@encryptor ||= Encryptor.new(key)
36+
end
37+
38+
def ensure_file_exists(path)
39+
abort('[ERROR] File does not exist') unless File.exist?(path)
40+
end
41+
end
42+
end
43+
end

Diff for: lib/diffcrypt/encryptor.rb

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
require 'active_support/message_encryptor'
1010

11+
require_relative './version'
12+
1113
module Diffcrypt
1214
class Encryptor
1315
CIPHER = 'aes-128-gcm'

Diff for: test/diffcrypt/cli_test.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
require 'thor'
5+
6+
require_relative '../../lib/diffcrypt/cli'
7+
8+
class Diffcrypt::CLITest < Minitest::Test
9+
def test_it_extends_thor
10+
assert Diffcrypt::CLI < Thor
11+
end
12+
end

0 commit comments

Comments
 (0)