Skip to content

Add CLI to use in any repo #1

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

Merged
merged 6 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0



## [Unreleased]

## Added

- CLI: Use diffcrypt from command line of any project without requiring ruby integration
- CLI: `diffcrypt encrypt` Directly encrypt any file and output the contents
- CLI: `diffcrypt decrypt` Directly decrypt any file and output the contents



## [0.2.0] - 2020-06-28

### Added
Expand Down
8 changes: 8 additions & 0 deletions bin/diffcrypt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'thor'

require_relative '../lib/diffcrypt/cli'

Diffcrypt::CLI.start(ARGV)
5 changes: 3 additions & 2 deletions diffcrypt.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ Gem::Specification.new do |spec|
spec.files = Dir.chdir(File.expand_path(__dir__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
end
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.bindir = 'bin'
spec.executables = %w[diffcrypt]
spec.require_paths = ['lib']

spec.add_runtime_dependency 'activesupport', '~> 6.0.0'
spec.add_runtime_dependency 'thor', '~> 1.0.1'
end
43 changes: 43 additions & 0 deletions lib/diffcrypt/cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

require_relative './encryptor'
require_relative './version'

module Diffcrypt
class CLI < Thor
desc 'decrypt <path>', 'Decrypt a file'
method_option :key, aliases: %i[k], required: true
def decrypt(path)
ensure_file_exists(path)
contents = File.read(path)
puts encryptor.decrypt(contents)
end

desc 'encrypt <path>', 'Encrypt a file'
method_option :key, aliases: %i[k], required: true
def encrypt(path)
ensure_file_exists(path)
contents = File.read(path)
puts encryptor.encrypt(contents)
end

desc 'version', 'Show client version'
def version
say Diffcrypt::VERSION
end

no_commands do
def key
options[:key]
end

def encryptor
@encryptor ||= Encryptor.new(key)
end

def ensure_file_exists(path)
abort('[ERROR] File does not exist') unless File.exist?(path)
end
end
end
end
2 changes: 2 additions & 0 deletions lib/diffcrypt/encryptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

require 'active_support/message_encryptor'

require_relative './version'

module Diffcrypt
class Encryptor
CIPHER = 'aes-128-gcm'
Expand Down
12 changes: 12 additions & 0 deletions test/diffcrypt/cli_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require 'test_helper'
require 'thor'

require_relative '../../lib/diffcrypt/cli'

class Diffcrypt::CLITest < Minitest::Test
def test_it_extends_thor
assert Diffcrypt::CLI < Thor
end
end