bin/zoho_hub in zoho_hub-0.2.0 vs bin/zoho_hub in zoho_hub-0.3.0
- old
+ new
@@ -1,62 +1,43 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'bundler/setup'
-require 'zoho_hub'
-require 'zoho_hub/auth'
-require 'zoho_hub/oauth_callback_server'
+begin
+ require 'dotenv'
+ Dotenv.load
+rescue LoadError
+end
-require 'launchy'
-require 'optparse'
+require 'zoho_hub/cli/callback_server'
+require 'zoho_hub/cli/read_modules'
-default_port = ZohoHub::OauthCallbackServer.settings.port
+command = ARGV[0]
-options = {}
-parser = OptionParser.new do |op|
- op.banner = 'Usage: zoho_hub -c CLIENT_ID -s SECRET [options]'
+def print_commands
+ commands = [
+ ['callback-server', 'run a http server to serve as oauth callback to get a refresh token'],
+ ['read-modules', 'read the configuration from Zoho CRM and save it locally']
+ ]
- op.on('-c', '--client-id=client_id', 'The Zoho client ID') do |client|
- options[:client_id] = client
- end
+ largest_name = commands.map(&:first).max_by(&:length)
+ left_pad = largest_name.length + 5
- op.on('-s', '--secret=secret', 'The Zoho secret') do |secret|
- options[:secret] = secret
+ commands.each do |command|
+ $stdout.print " #{command.first}".ljust(left_pad)
+ $stdout.puts command.last
end
-
- op.on('-p', '--port=port', "The port for your callback (default #{default_port})") do |port|
- options[:port] = port
- end
-
- op.on_tail('--version', 'Show version') do
- puts "ZohoHub #{ZohoHub::VERSION}"
- exit
- end
end
-parser.parse!
+case command
+when 'callback-server'
+ ZohoHub::Cli::CallbackServer.new.run(ARGV[1..-1])
+when 'read-modules'
+ ZohoHub::Cli::ReadModules.new.run(ARGV[1..-1])
+else
+ $stdout.puts 'Usage:'
+ $stdout.puts " zoho_hub [command] [options]\n"
+ $stdout.puts 'where <command> is one of:'
-abort(parser.help) if options[:client_id].nil? || options[:secret].nil?
-
-callback_path = ZohoHub::OauthCallbackServer::CALLBACK_PATH
-
-ZohoHub::OauthCallbackServer.set(:port, options[:port]) if options[:port]
-
-bind_port = ZohoHub::OauthCallbackServer.settings.port
-bind_address = ZohoHub::OauthCallbackServer.settings.bind
-
-callback_url = "http://#{bind_address}:#{bind_port}/#{callback_path}"
-
-puts "Callback URL: #{callback_url}"
-
-ZohoHub.configure do |config|
- config.client_id = options[:client_id]
- config.secret = options[:secret]
- config.redirect_uri = callback_url
- config.debug = true
+ print_commands
end
-
-# Open the URL in the browser
-url = ZohoHub::Auth.auth_url
-puts "REQUESTING: #{url}"
-Launchy.open(url)