module Tr3llo::Application

Constants

DEFAULT_CONFIG_FILE_PATH

Public Instance Methods

build_option_parser() click to toggle source
# File lib/3llo/application.rb, line 71
def build_option_parser()
  OptionParser.new do |parser|
    parser.program_name = "3llo v#{Tr3llo::VERSION}"
    parser.on("-h", "--help", "show this message")
    parser.on("-i", "--init=INIT", String, "the init command to run when the program starts")
    parser.on("-c", "--config=CONFIG", String, "file path to the config file")
    parser.on("--configure", "set up configuration")
  end
end
execute_configure!(interface) click to toggle source
# File lib/3llo/application.rb, line 43
def execute_configure!(interface)
  interface.puts("Please key in the following information:")
  interface.puts("Tips: Visit https://trello.com/app-key to obtain the credentials.\n\n")

  key = interface.input.ask("API key:", required: true)
  token = interface.input.ask("API token:", required: true)

  if API::Token.verify(key, token)
    file_content = JSON.dump({"key" => key, "token" => token})
    File.write(File.expand_path(DEFAULT_CONFIG_FILE_PATH), file_content)

    message = "Configuration has been saved to" + Utils.paint(DEFAULT_CONFIG_FILE_PATH, "green") + "."
    interface.puts(message)
  else
    error_message = "Either key or token is invalid. Please try again."
    interface.print_error(error_message)
  end
end
execute_help!(parser, interface) click to toggle source
# File lib/3llo/application.rb, line 39
def execute_help!(parser, interface)
  interface.puts(parser.help)
end
fetch_board!() click to toggle source
# File lib/3llo/application.rb, line 151
def fetch_board!()
  $application.resolve(:board)
rescue ::Container::KeyNotFoundError
  raise BoardNotSelectedError
end
fetch_client!() click to toggle source
# File lib/3llo/application.rb, line 169
def fetch_client!()
  $application.resolve(:api_client)
end
fetch_configuration!() click to toggle source
# File lib/3llo/application.rb, line 161
def fetch_configuration!()
  $application.resolve(:configuration)
end
fetch_interface!() click to toggle source
# File lib/3llo/application.rb, line 165
def fetch_interface!()
  $application.resolve(:interface)
end
fetch_registry!() click to toggle source
# File lib/3llo/application.rb, line 173
def fetch_registry!()
  $application.resolve(:registry)
end
fetch_user!() click to toggle source
# File lib/3llo/application.rb, line 157
def fetch_user!()
  $application.resolve(:user)
end
get_config_entry(config, key, env_key) click to toggle source
# File lib/3llo/application.rb, line 128
def get_config_entry(config, key, env_key)
  config.fetch(key) do
    if ENV.has_key?(env_key)
      Utils.deprecate!(
        "Setting #{env_key.inspect} as an environment variable is deprecated. " \
          "It will be removed in the future versions of 3llo. Please use config file instead."
      )

      ENV.fetch(env_key)
    else
      raise KeyError.new(key: key)
    end
  end
end
load_configuration!(config_file) click to toggle source
# File lib/3llo/application.rb, line 98
def load_configuration!(config_file)
  config_path = File.expand_path(config_file)

  config =
    if File.exist?(config_path)
      JSON.load(File.read(config_path))
    else
      {}
    end

  configuration = Tr3llo::Configuration.new

  configuration.api_key = get_config_entry(config, "key", "TRELLO_KEY")
  configuration.api_token = get_config_entry(config, "token", "TRELLO_TOKEN")

  configuration.finalize!()

  $application.register(:configuration, configuration)
rescue KeyError => exception
  command_string = "3llo --configure"

  abort(
    Utils.paint(
      "#{exception.key.inspect} has not been configured. " \
        "Please run #{command_string.inspect} to set up configuration.",
      "red"
    )
  )
end
load_user!(interface) click to toggle source
# File lib/3llo/application.rb, line 177
def load_user!(interface)
  user = Tr3llo::API::User.find("me")
  decorated_username = Utils.format_highlight("@#{user.username}")

  interface.print_frame do
    interface.puts("You're logged in as #{decorated_username}")
  end

  $application.register(:user, user)
end
parse_cli_args!(parser, args) click to toggle source
# File lib/3llo/application.rb, line 62
def parse_cli_args!(parser, args)
  options = {}
  parser.parse!(args, into: options)

  options
rescue OptionParser::InvalidArgument, OptionParser::InvalidOption
  {}
end
print_help!(interface) click to toggle source
register_api_client!() click to toggle source
# File lib/3llo/application.rb, line 87
def register_api_client!()
  remote_server = Tr3llo::RemoteServer.new("https://api.trello.com/1")

  $application.register(:api_client, remote_server)
end
register_board!(board) click to toggle source
# File lib/3llo/application.rb, line 147
def register_board!(board)
  $application.register(:board, board)
end
register_interface!() click to toggle source
# File lib/3llo/application.rb, line 93
def register_interface!()
  prompt = TTY::Prompt.new()
  $application.register(:interface, Tr3llo::Interface.new(prompt, $stdout))
end
register_registry!() click to toggle source
# File lib/3llo/application.rb, line 143
def register_registry!()
  $application.register(:registry, Tr3llo::Registry.new)
end
start(args) click to toggle source
# File lib/3llo/application.rb, line 7
def start(args)
  $application = Container.new()

  interface = register_interface!()
  option_parser = build_option_parser()
  register_api_client!()

  options = parse_cli_args!(option_parser, args)

  if options.has_key?(:help)
    execute_help!(option_parser, interface)
    exit
  end

  if options.has_key?(:configure)
    execute_configure!(interface)
    exit
  end

  init_command = options.fetch(:init, "")
  config_file = options.fetch(:config, DEFAULT_CONFIG_FILE_PATH)

  print_help!(interface)

  load_configuration!(config_file)
  register_registry!()

  load_user!(interface)

  Controller.start(init_command)
end