module Kontena

Public Class Methods

browserless?() click to toggle source
# File lib/kontena_cli.rb, line 102
def self.browserless?
  !!(RUBY_PLATFORM =~ /linux|(?:free|net|open)bsd|solaris|aix|hpux/ && ENV['DISPLAY'].to_s.empty?)
end
cli_root(*joinables) click to toggle source
# File lib/kontena_cli.rb, line 143
def self.cli_root(*joinables)
  if joinables.empty?
    File.join(Kontena.root, 'lib/kontena/cli')
  else
    File.join(Kontena.root, 'lib/kontena/cli', *joinables)
  end
end
debug?() click to toggle source
# File lib/kontena_cli.rb, line 167
def self.debug?
  !['', 'false'].include?(ENV['DEBUG'].to_s)
end
home() click to toggle source
# File lib/kontena_cli.rb, line 82
def self.home
  return @home if @home
  @home = File.join(Dir.home, '.kontena')
  Dir.mkdir(@home, 0700) unless File.directory?(@home)
  @home
end
log_target() click to toggle source
# File lib/kontena_cli.rb, line 66
def self.log_target
  return @log_target if @log_target

  @log_target = ENV['LOG_TARGET']

  if debug?
    @log_target ||= $stderr
  elsif @log_target.nil?
    @log_target = File.join(home, 'kontena.log')
  end
end
logger() click to toggle source
# File lib/kontena_cli.rb, line 151
def self.logger
  return @logger if @logger
  if log_target.respond_to?(:tty?) && log_target.tty?
    logger = Logger.new(log_target)
    require 'kontena/cli/log_formatters/compact'
    logger.formatter = Kontena::Cli::LogFormatter::Compact.new
  else
    logger = Logger.new(log_target, 1, 1_048_576)
    require 'kontena/cli/log_formatters/strip_color'
    logger.formatter = Kontena::Cli::LogFormatter::StripColor.new
  end
  logger.level = debug? ? Logger::DEBUG : Logger::INFO
  logger.progname = 'CLI'
  @logger = logger
end
minor_version() click to toggle source

@return [String] x.y

# File lib/kontena_cli.rb, line 90
def self.minor_version
  Kontena::Cli::VERSION.split('.')[0..1].join('.')
end
on_windows?() click to toggle source
# File lib/kontena_cli.rb, line 98
def self.on_windows?
  ENV['OS'] == 'Windows_NT' && RUBY_PLATFORM !~ /cygwin/
end
pastel() click to toggle source
# File lib/kontena_cli.rb, line 110
def self.pastel
  return @pastel if @pastel
  require 'pastel'
  @pastel = Pastel.new(enabled: !simple_terminal?)
end
prompt() click to toggle source
# File lib/kontena_cli.rb, line 116
def self.prompt
  return @prompt if @prompt
  if simple_terminal?
    require_relative 'kontena/light_prompt'
    klass = Kontena::LightPrompt
  else
    require 'tty-prompt'
    klass = TTY::Prompt
  end

  @prompt = klass.new(
    active_color: :cyan,
    help_color: :white,
    error_color: :red,
    interrupt: :exit,
    prefix: pastel.green('> ')
  )
end
reset_logger() click to toggle source
# File lib/kontena_cli.rb, line 78
def self.reset_logger
  @log_target, @logger = nil
end
reset_prompt() click to toggle source
# File lib/kontena_cli.rb, line 135
def self.reset_prompt
  @prompt = nil
end
root() click to toggle source
# File lib/kontena_cli.rb, line 139
def self.root
  File.dirname(__dir__)
end
run(*cmdline) click to toggle source

Run a kontena command and return true if the command did not raise or exit with a non-zero exit code. Raises nothing. @param [String,Array<String>] command_line @return [TrueClass,FalseClass] success

# File lib/kontena_cli.rb, line 57
def self.run(*cmdline)
  result = run!(*cmdline)
  result.nil? ? true : result
rescue SystemExit => ex
  ex.status.zero?
rescue
  false
end
run!(*cmdline) click to toggle source

Run a kontena command like it was launched from the command line. Re-raises any exceptions, except a SystemExit with status 0, which is considered a success.

@param [String,Array<String>] command_line @return command result or nil

# File lib/kontena_cli.rb, line 32
def self.run!(*cmdline)
  if cmdline.first.kind_of?(Array)
    command = cmdline.first
  elsif cmdline.size == 1 && cmdline.first.include?(' ')
    command = cmdline.first.shellsplit
  else
    command = cmdline
  end
  logger.debug { "Running Kontena.run(#{command.inspect})" }
  result = Kontena::MainCommand.new(File.basename(__FILE__)).run(command)
  logger.debug { "Command completed, result: #{result.inspect} status: 0" }
  result
rescue SystemExit => ex
  logger.debug { "Command caused SystemExit, status: #{ex.status}" }
  return true if ex.status.zero?
  raise ex
rescue => ex
  logger.error { "Command #{cmdline.inspect} exception" }
  logger.error { ex }
  raise ex
end
simple_terminal?() click to toggle source
# File lib/kontena_cli.rb, line 106
def self.simple_terminal?
  ENV['KONTENA_SIMPLE_TERM'] || !$stdout.tty?
end
version() click to toggle source
# File lib/kontena_cli.rb, line 94
def self.version
  "kontena-cli/#{Kontena::Cli::VERSION}"
end