lib/3scale_toolbox/cli.rb in 3scale_toolbox-0.4.0 vs lib/3scale_toolbox/cli.rb in 3scale_toolbox-0.5.0
- old
+ new
@@ -1,78 +1,22 @@
-require 'optparse'
+require '3scale_toolbox'
+require '3scale_toolbox/commands'
-module ThreeScaleToolbox
- module CLI
- Options = Struct.new(:command)
+module ThreeScaleToolbox::CLI
+ def self.root_command
+ ThreeScaleToolbox::Commands::ThreeScaleCommand
+ end
- class Parser
- def self.parse(options)
- args = Options.new(nil)
+ def self.add_command(command)
+ root_command.add_subcommand(command)
+ end
- opt_parser = OptionParser.new do |opts|
- opts.banner = "Usage: 3scale <command> [options]"
+ def self.load_builtin_commands
+ ThreeScaleToolbox::Commands::BUILTIN_COMMANDS.each(&method(:add_command))
+ end
-
- opts.on("-h", "--help", "Prints this help") do
- puts opts
- exit
- end
- end
-
- begin
- opt_parser.order!(options)
- rescue OptionParser::InvalidOption => e
- p e
- end
-
- return args
- end
- end
-
- def self.parse(argv = ARGV)
- options = Parser.parse(argv)
- options.command = argv.shift
- options.command = subcommands.find { |subcommand| subcommand.name == options.command }
-
- [ options, argv ]
- end
-
- def self.print_help!
- Parser.parse %w[--help]
- end
-
- def self.plugins
- Gem.loaded_specs.select{ |name, _| name.start_with?('3scale') }.values
- end
-
- def self.current_command
- File.expand_path($0, Dir.pwd)
- end
-
- def self.subcommands
- plugins
- .flat_map { |spec| spec.executables.flat_map{ |bin| Subcommand.new(bin, spec) } }
- .reject { |subcommand| subcommand.full_path == current_command || subcommand.name.nil? }
- end
-
- class Subcommand
- attr_reader :executable, :spec
-
- def initialize(executable, spec)
- @executable = executable
- @spec = spec
- end
-
- def to_s
- name
- end
-
- def name
- executable.split('-', 2)[1]
- end
-
- def full_path
- spec.bin_file(executable)
- end
- end
+ def self.run(args)
+ load_builtin_commands
+ ThreeScaleToolbox.load_plugins
+ root_command.build_command.run args
end
end