class Compote::CLI

Constants

COMPOSE_COMMANDS
COMPOTE_COMMANDS

Public Instance Methods

command( key, *arguments ) click to toggle source
# File lib/compote/cli/command.rb, line 7
def command ( key, *arguments )

  config = load_config

  commands = config.commands

  command = commands[ key ]

  service_name, command_name = key.include?( ':' ) ? key.split( ':' ) : [ nil, key ]

  raise UndefinedCommandError.new service_name: service_name, command_name: command_name, config: config unless command

  arguments = [ *Shellwords.split( command ), *arguments ]

  say "Running compote command \"#{ key }\":"

  if service_name

    run_arguments, command_arguments = split_arguments arguments

    arguments = [ 'run', *run_arguments, service_name, *command_arguments ]

    say Shellwords.join [ 'compote', *arguments ]

    run_compose *arguments

  else

    say Shellwords.join arguments

    environment = config.compose_environment

    exec environment, *arguments

  end

rescue Error => error

  exit_with_error error

end
commands() click to toggle source
# File lib/compote/cli/commands.rb, line 7
def commands

  config = load_config

  commands = config.commands

  if commands.empty?

    say 'No commands specified'

  else

    say commands.map { | key, command | "#{ key } -> #{ command }" }.join "\n"

  end

rescue Error => error

  exit_with_error error

end
env() click to toggle source
# File lib/compote/cli/env.rb, line 7
def env

  compote_config = load_config

  compose_environment = compote_config.compose_environment

  say YAML.dump compose_environment

rescue Error => error

  exit_with_error error

end
help( *arguments ) click to toggle source
Calls superclass method
# File lib/compote/cli/help.rb, line 7
def help ( *arguments )

  if COMPOSE_COMMANDS.include? arguments.first

    exec 'docker-compose', 'help', *arguments

  else

    super *arguments

  end

end
version() click to toggle source
# File lib/compote/cli/version.rb, line 9
def version

  compote_version = "compote version: #{ Compote::VERSION }"

  compose_version = if options[ :short ]

    "docker-compose version: #{ `docker-compose version --short` }"

  else

    `docker-compose version`

  end

  say compote_version

  say compose_version

end

Protected Instance Methods

exit_with_error( error ) click to toggle source
# File lib/compote/cli.rb, line 64
def exit_with_error ( error )

  say error.message, :red

  exit 1

end
load_config() click to toggle source
# File lib/compote/cli.rb, line 26
def load_config

  path = ENV.fetch 'COMPOTE_FILE', './docker-compote.yml'

  config = Config.load_config path

  config

end
run_compose( command, *arguments ) click to toggle source
# File lib/compote/cli.rb, line 36
def run_compose ( command, *arguments )

  compote_config = load_config

  compose_environment = compote_config.compose_environment

  compose_file = compote_config.compose_file

  system compose_environment, 'docker-compose', '-f', compose_file.path, command, *arguments

  exit_status = $?.exitstatus

  compose_file.unlink

  exit exit_status

rescue Error => error

  compose_file&.unlink

  exit_with_error error

ensure

  compose_file&.unlink

end
split_arguments( arguments ) click to toggle source
# File lib/compote/cli/command.rb, line 51
def split_arguments ( arguments )

  is_run_option = true

  run_arguments, command_arguments = arguments.partition do | argument |

    is_run_option &&= argument.match? /\A-/

    is_run_option

  end

  [ run_arguments, command_arguments ]

end