OSDN Git Service

Update progress display function.
authorTatsuki Sugiura <sugi@nemui.org>
Wed, 16 Mar 2016 10:54:52 +0000 (19:54 +0900)
committerTatsuki Sugiura <sugi@nemui.org>
Wed, 16 Mar 2016 10:54:52 +0000 (19:54 +0900)
lib/osdn/cli.rb
lib/osdn/cli/command/frs_upload.rb
lib/osdn/cli/common.rb [deleted file]
lib/osdn/cli/overrides.rb [new file with mode: 0644]

index fc4d853..66b8c64 100644 (file)
@@ -1,5 +1,6 @@
 require "osdn/cli/version"
 require "osdn/cli/runner"
+require "osdn/cli/overrides"
 require 'getoptlong'
 require 'logger'
 require 'pathname'
@@ -8,6 +9,7 @@ require 'fileutils'
 require 'osdn-client'
 require 'hashie'
 require 'json'
+require 'digest'
 
 module OSDN
   module CLI
@@ -19,7 +21,17 @@ module OSDN
     def client_secret
       @@client_secret
     end
-    module_function :client_id, :client_secret
+
+    @@_show_progress = false
+    def _show_progress
+      @@_show_progress
+    end
+    def _show_progress=(v)
+      @@_show_progress = v
+    end
+
+    module_function :client_id, :client_secret,
+                    :_show_progress, :_show_progress=
     
     module Command
       autoload :Login,     'osdn/cli/command/login'
index 868775b..f28fd80 100644 (file)
@@ -1,46 +1,3 @@
-require 'digest'
-require 'ethon'
-
-module OSDN; module CLI; module Command
-  class FrsUpload < Base
-    @@_show_progress = false
-    def self._show_progress
-      @@_show_progress
-    end
-    def self._show_progress=(v)
-      need_reset = (!!@@_show_progress != !!v)
-      @@_show_progress = v
-      need_reset and _reset_all_typhoeus_pool
-    end
-
-    def self._reset_all_typhoeus_pool
-      first_easyid = nil
-      while true
-        e = Typhoeus::Pool.get
-        Typhoeus::Pool.release(e)
-        e.__id__ == first_easyid and break
-        first_easyid ||= e.__id__
-      end
-    end
-  end
-end; end; end
-
-module Ethon
-  class Easy
-    module Callbacks
-      alias_method :set_callbacks_orig, :set_callbacks
-      def set_callbacks
-        set_callbacks_orig
-        if OSDN::CLI::Command::FrsUpload._show_progress
-          Curl.set_option(:noprogress, false, handle)
-        else
-          Curl.set_option(:noprogress, true, handle)
-        end
-      end
-    end
-  end
-end
-
 module OSDN; module CLI; module Command
   class FrsUpload < Base
     def help
@@ -138,12 +95,12 @@ module OSDN; module CLI; module Command
                 finfo = Hashie::Mash.new id: '(dry-run)', url: '(dry-run)'
               else
                 logger.level <= Logger::INFO and
-                  self.class._show_progress = true
+                  OSDN::CLI._show_progress = true
                 fio = file.open
                 logger.info "Starting upload #{file}..."
                 finfo = api.create_release_file target_proj, target_package(rdir), target_release(rdir), fio, visibility: @visibility
                 fio.close
-                self.class._show_progress = false
+                OSDN::CLI._show_progress = false
                 if digests.find { |type, dig| dig != finfo.send("digest_#{type}") }
                   logger.error "File digests are mismatch! Upload file #{file} may be broken! Please check."
                 else
diff --git a/lib/osdn/cli/common.rb b/lib/osdn/cli/common.rb
deleted file mode 100644 (file)
index 975341a..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-module OSDN
-  module CLI
-    module Common
-    end
-  end
-end
diff --git a/lib/osdn/cli/overrides.rb b/lib/osdn/cli/overrides.rb
new file mode 100644 (file)
index 0000000..6990268
--- /dev/null
@@ -0,0 +1,16 @@
+require 'typhoeus/easy_factory'
+
+module Typhoeus
+  class EasyFactory
+    alias_method :get_orig, :get
+    def get(*args)
+      easy = get_orig(*args)
+      if OSDN::CLI._show_progress
+        Ethon::Curl.set_option(:noprogress, false, easy.handle)
+      else
+        Ethon::Curl.set_option(:noprogress, true, easy.handle)
+      end
+      easy
+    end
+  end
+end