lib/sym/app/cli.rb in sym-2.2.1 vs lib/sym/app/cli.rb in sym-2.3.0
- old
+ new
@@ -1,22 +1,23 @@
require 'slop'
require 'sym'
require 'colored2'
require 'yaml'
-require 'forwardable'
require 'openssl'
+require 'highline'
+
require 'sym/application'
require 'sym/errors'
+
require 'sym/app/commands'
require 'sym/app/keychain'
require 'sym/app/private_key/handler'
-require 'highline'
-require_relative 'output/file'
-require_relative 'output/file'
-require_relative 'output/stdout'
-require_relative 'cli_slop'
+require 'sym/app/output/base'
+require 'sym/app/output/file'
+require 'sym/app/output/stdout'
+require 'sym/app/cli_slop'
module Sym
module App
# This is the main interface class for the CLI application.
# It is responsible for parsing user's input, providing help, examples,
@@ -53,13 +54,10 @@
class CLI
# brings in #parse(Array[String] args)
include CLISlop
- extend Forwardable
- def_delegators :@application, :command
-
attr_accessor :opts, :application, :outputs, :output_proc
def initialize(argv_original)
env_args = ENV[ENV_ARGS_VARIABLE_NAME]
begin
@@ -73,38 +71,43 @@
error exception: e
return
end
command_no_color(argv_original) if opts[:no_color]
-
self.application = ::Sym::Application.new(opts)
-
select_output_stream
end
def execute
return Sym::App.exit_code if Sym::App.exit_code != 0
-
result = application.execute
- if result.is_a?(Hash)
- self.output_proc = ::Sym::App::Args.new({}).output_class
- error(result)
- else
- self.output_proc.call(result)
+ case result
+ when Hash
+ self.output_proc = ::Sym::App::Args.new({}).output_class
+ error(result)
+ else
+ self.output_proc.call(result)
end
+ Sym::App.exit_code
end
+ def command
+ @command ||= self.application&.command
+ end
+
private
def command_dictionary
options = opts.parser.unused_options + opts.parser.used_options
puts options.map(&:to_s).sort.map { |o| "-#{o[1]}" }.join(' ')
exit 0
end
def error(hash)
- Sym::App.error(hash.merge(config: (opts ? opts.to_hash : {})))
+ hash.merge!(config: opts.to_hash) if opts
+ hash.merge!(command: @command) if @command
+ Sym::App.error(**hash)
end
def select_output_stream
output_klass = application.args.output_class
unless output_klass && output_klass.is_a?(Class)