From: Tatsuki Sugiura Date: Tue, 19 Jul 2016 10:41:19 +0000 (+0900) Subject: Add bandwidth limit option. X-Git-Tag: v0.1.5~1 X-Git-Url: http://git.osdn.net/view?p=osdn-codes%2Fosdn-cli.git;a=commitdiff_plain;h=42a8d6a815ce33b175de7247e1ca61fb9db35e38 Add bandwidth limit option. --- diff --git a/lib/osdn/cli.rb b/lib/osdn/cli.rb index fdc18d1..47962cd 100644 --- a/lib/osdn/cli.rb +++ b/lib/osdn/cli.rb @@ -30,8 +30,17 @@ module OSDN @@_show_progress = v end + @@_rate_limit = nil + def _rate_limit + @@_rate_limit + end + def _rate_limit=(v) + @@_rate_limit = v + end + module_function :client_id, :client_secret, - :_show_progress, :_show_progress= + :_show_progress, :_show_progress=, + :_rate_limit, :_rate_limit= module Command autoload :Login, 'osdn/cli/command/login' diff --git a/lib/osdn/cli/command/frs_upload.rb b/lib/osdn/cli/command/frs_upload.rb index 601964f..b6a473a 100644 --- a/lib/osdn/cli/command/frs_upload.rb +++ b/lib/osdn/cli/command/frs_upload.rb @@ -12,6 +12,7 @@ module OSDN; module CLI; module Command puts " --force-digest Calc local file digest forcely" puts " --progress Force to show upload progress" puts " --no-progress Force to hide upload progress" + puts " --bwlimit=RATE Limit bandwidth (in KB)" end def run @@ -25,6 +26,7 @@ module OSDN; module CLI; module Command [ '--force-digest', GetoptLong::NO_ARGUMENT], [ '--progress', GetoptLong::NO_ARGUMENT], [ '--no-progress', GetoptLong::NO_ARGUMENT], + [ '--bwlimit', GetoptLong::REQUIRED_ARGUMENT ], ) opts.each do |opt, arg| case opt @@ -51,6 +53,9 @@ module OSDN; module CLI; module Command @show_progress = true when '--no-progress' @show_progress = false + when '--bwlimit' + arg.to_i != 0 and + OSDN::CLI._rate_limit = arg.to_i * 1024 end end diff --git a/lib/osdn/cli/command/relfile.rb b/lib/osdn/cli/command/relfile.rb index ca2b4e8..b7ad732 100644 --- a/lib/osdn/cli/command/relfile.rb +++ b/lib/osdn/cli/command/relfile.rb @@ -17,6 +17,7 @@ module OSDN; module CLI; module Command puts " --force-digest Calc local file digest forcely" puts " --progress Force to show upload progress" puts " --no-progress Force to hide upload progress" + puts " --bwlimit=RATE Limit bandwidth (in KB)" end def self.description @@ -33,6 +34,7 @@ module OSDN; module CLI; module Command [ '--force-digest', GetoptLong::NO_ARGUMENT], [ '--progress', GetoptLong::NO_ARGUMENT], [ '--no-progress', GetoptLong::NO_ARGUMENT], + [ '--bwlimit', GetoptLong::REQUIRED_ARGUMENT ], ) opts.each do |opt, arg| case opt @@ -60,6 +62,9 @@ module OSDN; module CLI; module Command @show_progress = true when '--no-progress' @show_progress = false + when '--bwlimit' + arg.to_i != 0 and + OSDN::CLI._rate_limit = arg.to_i * 1024 end end end diff --git a/lib/osdn/cli/overrides.rb b/lib/osdn/cli/overrides.rb index 6bb7b42..25fea5c 100644 --- a/lib/osdn/cli/overrides.rb +++ b/lib/osdn/cli/overrides.rb @@ -5,6 +5,7 @@ module Typhoeus alias_method :get_orig, :get def get(*args) easy = get_orig(*args) + if ENV['CURL_CA_BUNDLE'] Ethon::Curl.set_option(:cainfo, ENV['CURL_CA_BUNDLE'], easy.handle) else @@ -21,12 +22,18 @@ module Typhoeus Ethon::Curl.set_option(:cainfo, ca, easy.handle) end end + if OSDN::CLI._show_progress Ethon::Curl.set_option(:noprogress, false, easy.handle) - else Ethon::Curl.set_option(:noprogress, true, easy.handle) end + + if OSDN::CLI._rate_limit + Ethon::Curl.set_option(:max_send_speed_large, OSDN::CLI._rate_limit, easy.handle) + Ethon::Curl.set_option(:max_recv_speed_large, OSDN::CLI._rate_limit, easy.handle) + end + easy end end