Skip to content

Commit

Permalink
Update all commands to reuse container/service logic
Browse files Browse the repository at this point in the history
  • Loading branch information
hopsoft committed May 19, 2024
1 parent 5ed7836 commit fbfdde6
Showing 16 changed files with 52 additions and 35 deletions.
27 changes: 17 additions & 10 deletions lib/containers/cli.rb
Original file line number Diff line number Diff line change
@@ -33,20 +33,20 @@ def service_name(container_name)
end

def service_names
`containers list -s`.split("\n").reject do |line|
@service_names ||= `containers list -s`.split("\n").reject do |line|
line.nil? || line.empty? || line.include?(Containers::Commandable::PREFIX)
end
end

def container_names
`containers list`.split("\n").reject do |line|
@container_names ||= `containers list`.split("\n").reject do |line|
line.nil? || line.empty? || line.include?(Containers::Commandable::PREFIX)
end
end

def requested_container_names(*args)
# explicitly requested container option
list = options[:container] if options[:container]
list = options[:container].split(" ").select(&:present?) if options[:container]

# determine container names based on service names
list ||= requested_service_names(*args).map { |name| container_name name }
@@ -60,13 +60,16 @@ def requested_container_names(*args)

def requested_service_names(*args)
# explicitly requested service option
list = options[:service] if options[:service]
list = options[:service].split(" ").select(&:present?) if options[:service]

# extract service names from passed arguments
list ||= extract_service_names(*args).map { |name| service_name name }

# determine service names based on container names
list ||= requested_container_names(*args).map { |name| service_name name } if options[:container]
list = requested_container_names(*args).map { |name| service_name name } if options[:container] && list.none?

# fallback to default service name
list = [docker_default_service].compact if list.none?

# fallback to all known service names
list = service_names if list.none?
@@ -75,18 +78,22 @@ def requested_service_names(*args)
list
end

def sanitize_args(*args)
services = extract_service_names(*args)
args.shift while args.any? && services.include?(args.first)
args.select(&:present?)
def arguments(*args)
sanitize_args(*args).join " "
end

private

def extract_service_names(*args)
services = []
services.push args.shift until args.none? || args.first.start_with?("-")
services.push args.shift until args.none? || service_names.exclude?(args.first)
services
end

def sanitize_args(*args)
services = extract_service_names(*args)
args.shift while services.include?(args.first)
args.select(&:present?)
end
end
end
5 changes: 3 additions & 2 deletions lib/containers/commands/attach.rb
Original file line number Diff line number Diff line change
@@ -5,7 +5,8 @@ class Containers::CLI < Thor
method_option :container, type: :string, aliases: "-c", desc: "The container name"
method_option :service, type: :string, aliases: "-s", desc: "The service name"
def attach(*args)
container = options[:container] || container_name(options[:service])
execute_command "docker attach #{container} #{args.join " "}"
requested_container_names(*args) do |container|
execute_command "docker attach #{container} #{arguments(*args)}"
end
end
end
5 changes: 3 additions & 2 deletions lib/containers/commands/bash.rb
Original file line number Diff line number Diff line change
@@ -5,7 +5,8 @@ class Containers::CLI < Thor
method_option :container, type: :string, aliases: "-c", desc: "The container name"
method_option :service, type: :string, aliases: "-s", desc: "The service name"
def bash(*args)
container = options[:container] || container_name(options[:service])
execute_command "containers exec -c #{container} bash #{args.join " "}"
requested_container_names(*args) do |container|
execute_command "containers exec -c #{container} bash #{arguments(*args)}"
end
end
end
5 changes: 3 additions & 2 deletions lib/containers/commands/bin.rb
Original file line number Diff line number Diff line change
@@ -5,7 +5,8 @@ class Containers::CLI < Thor
method_option :container, type: :string, aliases: "-c", desc: "The container name"
method_option :service, type: :string, aliases: "-s", desc: "The service name"
def bin(*args)
container = options[:container] || container_name(options[:service])
execute_command "containers exec -c #{container} bin/#{args.join " "}"
requested_container_names(*args) do |container|
execute_command "containers exec -c #{container} bin/#{arguments(*args)}"
end
end
end
5 changes: 3 additions & 2 deletions lib/containers/commands/exec.rb
Original file line number Diff line number Diff line change
@@ -6,8 +6,9 @@ class Containers::CLI < Thor
method_option :container, type: :string, aliases: "-c", desc: "The container name"
method_option :service, type: :string, aliases: "-s", desc: "The service name"
def exec(*args)
container = options[:container] || container_name(options[:service])
execute_command "docker exec -it #{container} #{args.join " "}"
requested_container_names(*args) do |container|
execute_command "docker exec -it #{container} #{arguments(*args)}"
end
end

map "x" => :exec
2 changes: 1 addition & 1 deletion lib/containers/commands/inspect.rb
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ class Containers::CLI < Thor
method_option :service, type: :string, aliases: "-s", desc: "The service name"
def inspect(*args)
requested_container_names(*args) do |container|
execute_command "docker inspect #{args.join " "} #{container}"
execute_command "docker inspect #{arguments(*args)} #{container}"
end
end
end
5 changes: 3 additions & 2 deletions lib/containers/commands/nodejs/npm.rb
Original file line number Diff line number Diff line change
@@ -5,7 +5,8 @@ class Containers::CLI < Thor
method_option :container, type: :string, aliases: "-c", desc: "The container name"
method_option :service, type: :string, aliases: "-s", desc: "The service name"
def npm(*args)
container = options[:container] || container_name(options[:service])
execute_command "containers exec -c #{container} npm #{args.join " "}"
requested_container_names(*args) do |container|
execute_command "containers exec -c #{container} npm #{arguments(*args)}"
end
end
end
5 changes: 3 additions & 2 deletions lib/containers/commands/nodejs/npx.rb
Original file line number Diff line number Diff line change
@@ -5,7 +5,8 @@ class Containers::CLI < Thor
method_option :container, type: :string, aliases: "-c", desc: "The container name"
method_option :service, type: :string, aliases: "-s", desc: "The service name"
def npx(*args)
container = options[:container] || container_name(options[:service])
execute_command "containers exec -c #{container} npx #{args.join " "}"
requested_container_names(*args) do |container|
execute_command "containers exec -c #{container} npx #{arguments(*args)}"
end
end
end
5 changes: 3 additions & 2 deletions lib/containers/commands/nodejs/yarn.rb
Original file line number Diff line number Diff line change
@@ -5,7 +5,8 @@ class Containers::CLI < Thor
method_option :container, type: :string, aliases: "-c", desc: "The container name"
method_option :service, type: :string, aliases: "-s", desc: "The service name"
def yarn(*args)
container = options[:container] || container_name(options[:service])
execute_command "containers exec -c #{container} yarn #{args.join " "}"
requested_container_names(*args) do |container|
execute_command "containers exec -c #{container} yarn #{arguments(*args)}"
end
end
end
2 changes: 1 addition & 1 deletion lib/containers/commands/restart.rb
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ class Containers::CLI < Thor
method_option :service, type: :array, aliases: "-s", desc: "A list of service names (space delimited)"
def restart(*args)
requested_container_names(*args) do |container|
execute_command "docker restart #{sanitize_args(*args).join " "} #{container}", replace_current_process: false
execute_command "docker restart #{arguments(*args)} #{container}", replace_current_process: false
end
end
end
5 changes: 3 additions & 2 deletions lib/containers/commands/ruby/bundle.rb
Original file line number Diff line number Diff line change
@@ -5,7 +5,8 @@ class Containers::CLI < Thor
method_option :container, type: :string, aliases: "-c", desc: "The container name"
method_option :service, type: :string, aliases: "-s", desc: "The service name"
def bundle(*args)
container = options[:container] || container_name(options[:service])
execute_command "containers exec -c #{container} bundle #{args.join " "}"
requested_container_names(*args) do |container|
execute_command "containers exec -c #{container} bundle #{arguments(*args)}"
end
end
end
5 changes: 3 additions & 2 deletions lib/containers/commands/ruby/rails/rails.rb
Original file line number Diff line number Diff line change
@@ -5,7 +5,8 @@ class Containers::CLI < Thor
method_option :container, type: :string, aliases: "-c", desc: "The container name"
method_option :service, type: :string, aliases: "-s", desc: "The service name"
def rails(*args)
container = options[:container] || container_name(options[:service])
execute_command "containers bundle -c #{container} exec rails #{args.join " "}"
requested_container_names(*args) do |container|
execute_command "containers bundle -c #{container} exec rails #{arguments(*args)}"
end
end
end
5 changes: 3 additions & 2 deletions lib/containers/commands/ruby/rake.rb
Original file line number Diff line number Diff line change
@@ -5,7 +5,8 @@ class Containers::CLI < Thor
method_option :container, type: :string, aliases: "-c", desc: "The container name"
method_option :service, type: :string, aliases: "-s", desc: "The service name"
def rake(*args)
container = options[:container] || container_name(options[:service])
execute_command "containers exec -c #{container} rake #{args.join " "}"
requested_container_names(*args) do |container|
execute_command "containers exec -c #{container} rake #{arguments(*args)}"
end
end
end
2 changes: 1 addition & 1 deletion lib/containers/commands/start.rb
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ class Containers::CLI < Thor
method_option :service, type: :array, aliases: "-s", desc: "A list of service names (space delimited)"
def start(*args)
requested_container_names(*args) do |container|
execute_command "docker start #{sanitize_args(*args).join " "} #{container}", replace_current_process: false
execute_command "docker start #{arguments(*args)} #{container}", replace_current_process: false
end
end
end
2 changes: 1 addition & 1 deletion lib/containers/commands/stop.rb
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ class Containers::CLI < Thor
method_option :service, type: :array, aliases: "-s", desc: "A list of service names (space delimited)"
def stop(*args)
requested_container_names(*args) do |container|
execute_command "docker stop #{sanitize_args(*args).join " "} #{container}", replace_current_process: false
execute_command "docker stop #{arguments(*args)} #{container}", replace_current_process: false
end
end
end
2 changes: 1 addition & 1 deletion lib/containers/commands/tail.rb
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ class Containers::CLI < Thor
method_option :service, type: :array, aliases: "-s", desc: "A list of service names (space delimited)"
def tail(*args)
args << "--since 5m" unless args.include?("--since")
execute_command "docker compose #{docker_compose_files.map { |f| "-f #{f}" }.join " "} logs #{sanitize_args(*args).join " "} -f #{requested_service_names(*args).join " "}"
execute_command "docker compose #{docker_compose_files.map { |f| "-f #{f}" }.join " "} logs #{arguments(*args)} -f #{requested_service_names(*args).join " "}"
end

map "logs" => :tail

0 comments on commit fbfdde6

Please sign in to comment.