Commit 78f788b

mo <mo.khan@gmail.com>
2019-07-24 19:42:21
extract methods
1 parent 81c1197
Changed files (1)
lib/examine.rb
@@ -1,7 +1,6 @@
 require "examine/version"
 
 require 'down'
-require 'mkmf'
 require 'socket'
 require 'thor'
 
@@ -15,17 +14,12 @@ module Examine
       method_option :clair_url, desc: 'clair url', default: 'http://localhost:6060', type: :string
       desc 'start', 'start a clair server'
       def start
-        db_pid = spawn 'docker run -d --name clair-db arminc/clair-db:latest'
-        command = 'docker ps --filter="name=clair-db" --filter="status=running" --filter="expose=5432/tcp" | grep -v CONT'
-        print '.' until system(command)
-        puts "clair-db started. (PID: #{db_pid})"
+        spawn 'docker run -d --name clair-db arminc/clair-db:latest'
+        print '.' until system('docker ps --filter="name=clair-db" --filter="status=running" --filter="expose=5432/tcp" | grep -v CONT')
 
-        clair_pid = spawn 'docker run --restart=unless-stopped -p 6060:6060 --link clair-db:postgres -d --name clair arminc/clair-local-scan:latest'
-
-        command = 'docker ps --filter="name=clair" --filter="status=running" --filter="expose=6060/tcp" | grep -v CONT'
-        print '.' until system(command)
+        spawn 'docker run --restart=unless-stopped -p 6060:6060 --link clair-db:postgres -d --name clair arminc/clair-local-scan:latest'
+        print '.' until system('docker ps --filter="name=clair" --filter="status=running" --filter="expose=6060/tcp" | grep -v CONT')
         print '.' until system("curl -s #{options[:clair_url]}/v1/namespaces > /dev/null")
-        puts "clair-local-scan started. (PID: #{clair_pid})"
       end
 
       method_option :ip, desc: 'ip address', default: nil, type: :string
@@ -36,17 +30,6 @@ module Examine
 
         ip = options[:ip] || Socket.ip_address_list[1].ip_address
         system "docker pull #{image}"
-        unless (clair_exe = find_executable('clair-scanner'))
-          download_path = case RUBY_PLATFORM
-          when 'x86_64-linux'
-            URI.join(DOWNLOAD_PATH, 'clair-scanner_linux_386').to_s
-          else
-            raise 'clair-scanner could not be found in your PATH. Download from https://github.com/arminc/clair-scanner/releases'
-          end
-          clair_exe = File.join(Dir.tmpdir, 'clair-scanner')
-          Down.download(download_path, destination: clair_exe)
-          `chmod +x #{clair_exe}`
-        end
         system "#{clair_exe} -c #{options[:clair_url]} --ip #{ip} #{image}"
       end
 
@@ -66,6 +49,33 @@ module Examine
       def started?
         status
       end
+
+      def clair_exe
+        @clair_exe ||= find_executable('clair-scanner') || download_clair
+      end
+
+      def find_executable(exe)
+        found = ENV['PATH'].split(':').find do |x|
+          File.exist?(File.join(x, exe))
+        end
+        found ? File.join(found, exe) : found
+      end
+
+      def download_clair
+        File.join(Dir.tmpdir, 'clair-scanner').tap do |exe|
+          Down.download(clair_download_path, destination: exe)
+          `chmod +x #{exe}`
+        end
+      end
+
+      def clair_download_path
+        case RUBY_PLATFORM
+        when 'x86_64-linux'
+          URI.join(DOWNLOAD_PATH, 'clair-scanner_linux_386').to_s
+        else
+          raise 'clair-scanner could not be found in your PATH. Download from https://github.com/arminc/clair-scanner/releases'
+        end
+      end
     end
 
     class Application < Thor