Skip to content

Commit

Permalink
Fix 👮 offences
Browse files Browse the repository at this point in the history
  • Loading branch information
bibendi committed Sep 25, 2018
1 parent 360ffd9 commit 76b1c6b
Show file tree
Hide file tree
Showing 26 changed files with 91 additions and 40 deletions.
13 changes: 13 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
AllCops:
Exclude:
- tmp/**/*
- vendor/**/*
TargetRubyVersion: 2.5

Documentation:
Expand Down Expand Up @@ -34,9 +35,21 @@ Security/MarshalLoad:
Style/DoubleNegation:
Enabled: false

Style/GuardClause:
Enabled: false

Style/StringLiterals:
Enabled: false

Style/WordArray:
Enabled: false

Naming/PredicateName:
Enabled: false

Naming/MemoizedInstanceVariableName:
Enabled: false

Layout/DotPosition:
EnforcedStyle: trailing

Expand Down
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ language: ruby
cache: bundler
rvm:
- 2.5.1
before_install: gem install bundler -v 1.16.3

before_install: gem install bundler

script:
- bundle exec rspec
- bundle exec rubocop
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

# Specify your gem's dependencies in dip.gemspec
gemspec
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task default: :spec
1 change: 1 addition & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "dip"
Expand Down
11 changes: 7 additions & 4 deletions dip.gemspec
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true

lib = File.expand_path("../lib", __FILE__)
lib = File.expand_path("lib", __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "dip/version"

# rubocop:disable Metrics/BlockLength
Gem::Specification.new do |spec|
spec.name = "dip"
spec.license = "MIT"
Expand All @@ -11,8 +13,8 @@ Gem::Specification.new do |spec|
spec.email = ["merkushin.m.s@gmail.com"]

spec.summary = "Ruby gem CLI tool for better interacting docker-compose files."
spec.description = %q{DIP - docker-compose interaction process.
CLI tool for better development experience when interacting with docker-compose.}
spec.description = "DIP - Docker Interaction Process." \
"CLI tool for better development experience when interacting with docker and docker-compose."
spec.homepage = "https://github.com/bibendi/dip"

# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
Expand All @@ -37,7 +39,8 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "pry-byebug", "~> 3"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "rubocop", "~> 0.58"
spec.add_development_dependency "rubocop", "~> 0.59"
spec.add_development_dependency "simplecov", '~> 0.16'
spec.add_development_dependency "test-unit", "~> 3"
end
# rubocop:enable Metrics/BlockLength
5 changes: 3 additions & 2 deletions exe/dip
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# frozen_string_literal: true

lib_path = File.expand_path('../lib', __dir__)
$:.unshift(lib_path) if !$:.include?(lib_path)
$LOAD_PATH.unshift(lib_path) unless $LOAD_PATH.include?(lib_path)

require 'dip'
require 'dip/cli'
require 'pry-byebug' if Dip.debug?
Expand All @@ -18,7 +19,7 @@ begin
ARGV.each do |arg|
if !run_vars.frozen? && arg.include?("=")
key, val = arg.split("=", 2)
run_vars << "#{"--x-dip-run-vars=" if run_vars.empty?}#{key}:#{val}"
run_vars << "#{'--x-dip-run-vars=' if run_vars.empty?}#{key}:#{val}"
else
run_vars.freeze
end
Expand Down
8 changes: 5 additions & 3 deletions lib/dip/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@ class CLI < Thor
class << self
def retrieve_command_name(args)
meth = args.first.to_sym unless args.empty?
args.unshift("run") if ::Dip.config.interaction.key?(meth.to_sym) if meth
args.unshift("run") if meth && ::Dip.config.interaction.key?(meth.to_sym)

super(args)
end

# Hackery. Take the run method away from Thor so that we can redefine it.
def is_thor_reserved_word?(word, type)
return false if word == "run"

super
end
end

desc 'version', 'dip version'
def version
require_relative 'version'
puts "#{Dip::VERSION}"
puts Dip::VERSION
end
map %w(--version -v) => :version

Expand All @@ -41,7 +42,8 @@ def compose(cmd, *argv)
desc 'CMD or dip run CMD [OPTIONS]', 'Run configured command in a docker-compose service'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
method_option :x_dip_run_vars, type: :hash,
method_option :x_dip_run_vars,
type: :hash,
desc: "Enforce environment variables into container, recommended run like 'dip FOO=bar cmd'"
def run(cmd, subcmd = nil, *argv)
if options[:help]
Expand Down
2 changes: 2 additions & 0 deletions lib/dip/cli/dns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class DNS < Thor
desc: 'Docker image name'
method_option :domain, aliases: '-d', type: :string, default: "docker",
desc: 'Top level domain'
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def up
if options[:help]
invoke :help, ['up']
Expand All @@ -36,6 +37,7 @@ def up
).execute
end
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength

desc "dns down", "Stop dnsdock container"
method_option :help, aliases: '-h', type: :boolean,
Expand Down
2 changes: 2 additions & 0 deletions lib/dip/cli/nginx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Nginx < Thor
desc: 'Docker image name'
method_option :domain, aliases: '-d', type: :string, default: "docker",
desc: 'Top level domain'
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def up
if options[:help]
invoke :help, ['up']
Expand All @@ -36,6 +37,7 @@ def up
).execute
end
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength

desc "nginx down", "Stop nginx container"
method_option :help, aliases: '-h', type: :boolean,
Expand Down
7 changes: 4 additions & 3 deletions lib/dip/cli/ssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ def up
if options[:help]
invoke :help, ['up']
else
Dip::Commands::SSH::Up.new(key: options.fetch(:key),
volume: options.fetch(:volume),
interactive: options.nonteractive? ? false : options.interactive?
Dip::Commands::SSH::Up.new(
key: options.fetch(:key),
volume: options.fetch(:volume),
interactive: options.nonteractive? ? false : options.interactive?
).execute
end
end
Expand Down
13 changes: 6 additions & 7 deletions lib/dip/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ module Dip
class Command
extend Forwardable

def_delegators self, :shell,
:subshell
def_delegators self, :shell, :subshell

class ExecRunner
def self.call(cmd, argv, env: {}, **options)
Expand All @@ -16,21 +15,21 @@ def self.call(cmd, argv, env: {}, **options)
end

class SubshellRunner
def self.call(cmd, argv, env: {}, **options)
::Kernel.system(env, cmd, *argv, options)
def self.call(cmd, argv, env: {}, panic: true, **options)
return if ::Kernel.system(env, cmd, *argv, options)
raise Dip::Error, "Command '#{([cmd] + argv).join(' ')}' executed with error." if panic
end
end

class << self
def shell(cmd, argv = [], subshell: false, panic: true, **options)
def shell(cmd, argv = [], subshell: false, **options)
cmd = Dip.env.interpolate(cmd)
argv = argv.map { |arg| Dip.env.interpolate(arg) }

puts [Dip.env.vars, cmd, argv].inspect if Dip.debug?

runner = subshell ? SubshellRunner : ExecRunner
return if runner.call(cmd, argv, env: Dip.env.vars, **options)
raise Dip::Error, "Command '#{([cmd] + argv).join(' ')}' executed with error." if panic
runner.call(cmd, argv, env: Dip.env.vars, **options)
end

def subshell(*args, **kwargs)
Expand Down
3 changes: 2 additions & 1 deletion lib/dip/commands/compose.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ def find_files
return unless (files = @config[:files])

if files.is_a?(Array)
result = files.each_with_object([]) do |file_name, memo|
files.each_with_object([]) do |file_name, memo|
file_name = ::Dip.env.interpolate(file_name)
next unless File.exist?(file_name)

memo << "--file"
memo << file_name
end
Expand Down
9 changes: 5 additions & 4 deletions lib/dip/commands/dns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ def initialize(name:, net:)
end

def execute(**options)
subshell("docker", "inspect " \
"--format '{{ .NetworkSettings.Networks.#{@net}.IPAddress }}' " \
"#{@name}".shellsplit,
**options)
subshell("docker",
"inspect " \
"--format '{{ .NetworkSettings.Networks.#{@net}.IPAddress }}' " \
"#{@name}".shellsplit,
**options)
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions lib/dip/commands/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ def initialize(cmd, subcmd = nil, argv = [], run_vars: nil)
@config = ::Dip.config.interaction
end

# TODO: Refactor
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def execute
command = @config.fetch(@cmd)
command[:subcommands] ||= {}

if (subcommand = command[:subcommands].fetch(@subcmd, {})).any?
subcommand[:command] ||= nil
command.merge!(subcommand)
else
@argv.unshift(@subcmd.to_s) if @subcmd
elsif @subcmd
@argv.unshift(@subcmd.to_s)
end

Dip.env.merge(command[:environment]) if command[:environment]
Expand All @@ -40,6 +42,7 @@ def execute

Dip::Commands::Compose.new(compose_method, compose_argv).execute
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength

def prepare_compose_run_options(value)
return [] unless value
Expand Down
4 changes: 1 addition & 3 deletions lib/dip/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

module Dip
class Config
DEFAULT_CONFIG = {}

def initialize(config_path)
load_or_default(config_path)
end
Expand Down Expand Up @@ -41,7 +39,7 @@ def load_or_default(config_path)
symbolize_names: true
)
else
DEFAULT_CONFIG
{}
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/dip/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Dip
VERSION = "3.0.0"
end
4 changes: 3 additions & 1 deletion spec/lib/commands/dns_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@
describe Dip::Commands::DNS::IP do
context "when without arguments" do
before { cli.start "ip".shellsplit }
it { expected_subshell("docker", array_including("inspect", "--format", /Networks.frontend.IPAddress/, "dnsdock")) }
it do
expected_subshell("docker", array_including("inspect", "--format", /Networks.frontend.IPAddress/, "dnsdock"))
end
end

context "when option `name` is present" do
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/commands/run_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@
before { cli.start "run rails refresh-test-db".shellsplit }

it do
expected_exec("docker-compose", ["run", "--rm", "app",
"rake", "db:drop", "db:tests:prepare", "db:migrate"],
env: hash_including("RAILS_ENV" => "test"))
expected_exec("docker-compose",
["run", "--rm", "app", "rake", "db:drop", "db:tests:prepare", "db:migrate"],
env: hash_including("RAILS_ENV" => "test"))
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions spec/lib/commands/ssh_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@

it { expected_subshell("docker", array_including("volume", "create")) }
it { expected_subshell("docker", array_including("run", "--name=ssh-agent", "whilp/ssh-agent")) }
it { expected_subshell("docker", array_including("run", "--volume", "/user:/user", "--interactive", "--tty",
"whilp/ssh-agent", "ssh-add", "/user/.ssh/id_rsa")) }
it do
expected_subshell("docker",
array_including("run", "--volume", "/user:/user", "--interactive", "--tty",
"whilp/ssh-agent", "ssh-add", "/user/.ssh/id_rsa"))
end
end

context "when option `key` is present" do
Expand Down
2 changes: 2 additions & 0 deletions spec/lib/dip_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

describe Dip do
it "has a version number" do
expect(Dip::VERSION).not_to be nil
Expand Down
1 change: 0 additions & 1 deletion spec/lib/environment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
let(:vars) { {} }
subject { described_class.new(vars) }


context "when vars is empty" do
it { is_expected.to be_truthy }
end
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "bundler/setup"

ENV["DIP_ENV"] = "test"
Expand Down
2 changes: 2 additions & 0 deletions spec/support/shared_contexts/config.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

shared_context "dip config", config: true do
before do
Dip.config.merge(config)
Expand Down
2 changes: 2 additions & 0 deletions spec/support/shared_contexts/environment.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

shared_context "replace environment vars", env: true do
around(:each) do |ex|
original = {}
Expand Down
Loading

0 comments on commit 76b1c6b

Please sign in to comment.