Commit a141c30

mo khan <mo@mokhan.ca>
2021-02-06 19:16:08
refactor: extract classes for git and shell
1 parent c700a78
Changed files (4)
lib/jive/cli.rb
@@ -13,15 +13,11 @@ module Jive
 
       desc "clone <org>/<project>", "git clone to ~/src/github.com/<org>/<project>"
       def clone(slug)
-        target_dir = Pathname.new(Dir.home).join("src/github.com/#{slug}")
-        run_each([
-          [:mkdir, "-p", target_dir.parent.to_s],
-          [:git, "clone", "git@github.com:#{slug}.git", target_dir]
-        ])
-        after_run([
-          ["cd", target_dir],
-          ["setenv", "JIVE_LAST_RUN=#{Time.now.to_i}"]
-        ])
+        shell = ::Jive::Shell.new
+        shell.run_safely do
+          git = Git.new(shell)
+          git.clone(slug)
+        end
       end
 
       desc "setup", "provide instructions to integrate into shell"
@@ -32,38 +28,6 @@ module Jive
             source #{::Jive.root.join("jive.sh")}
         MESSAGE
       end
-
-      private
-
-      COMMAND_MAP = {
-        cd: "/usr/bin/cd",
-        echo: "/usr/bin/echo",
-        git: "/usr/bin/git",
-        mkdir: "/usr/bin/mkdir"
-      }.freeze
-
-      def run_each(tasks)
-        tasks.each do |task|
-          break unless execute(task)
-        end
-      end
-
-      def execute(command, env: {})
-        system(env, expand(command))
-      end
-
-      def after_run(tasks)
-        finalizer_fd = 9
-        pipe = IO.new(finalizer_fd)
-        pipe.puts(tasks.map { |x| x.join(":") }.join("\n"))
-      end
-
-      def expand(command)
-        Array(command)
-          .flatten
-          .map { |x| COMMAND_MAP.fetch(x, x).to_s }
-          .join(" ")
-      end
     end
   end
 end
lib/jive/git.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+module Jive
+  class Git
+    attr_reader :shell
+
+    def initialize(shell)
+      @shell = shell
+    end
+
+    def clone(slug)
+      target_dir = target_dir_for(slug)
+      shell.run_each([
+        [:mkdir, "-p", target_dir.parent.to_s],
+        [:git, "clone", "git@github.com:#{slug}.git", target_dir]
+      ])
+      shell.after_run([
+        ["cd", target_dir],
+        ["setenv", "JIVE_LAST_RUN=#{Time.now.to_i}"]
+      ])
+    end
+
+    private
+
+    def target_dir_for(slug)
+      Pathname.new(Dir.home).join("src/github.com/#{slug}")
+    end
+  end
+end
lib/jive/shell.rb
@@ -0,0 +1,42 @@
+# frozen_string_literal: true
+
+module Jive
+  class Shell
+    COMMAND_MAP = {
+      cd: "/usr/bin/cd",
+      echo: "/usr/bin/echo",
+      git: "/usr/bin/git",
+      mkdir: "/usr/bin/mkdir"
+    }.freeze
+
+    def run_each(tasks)
+      tasks.each do |task|
+        break unless execute(task)
+      end
+    end
+
+    def execute(command, env: {})
+      system(env, expand(command))
+    end
+
+    def after_run(tasks)
+      finalizer_fd = 9
+      pipe = IO.new(finalizer_fd)
+      pipe.puts(tasks.map { |x| x.join(":") }.join("\n"))
+    end
+
+    def expand(command)
+      Array(command)
+        .flatten
+        .map { |x| COMMAND_MAP.fetch(x, x).to_s }
+        .join(" ")
+    end
+
+    def run_safely
+      yield
+    rescue StandardError => e
+      say e
+      after_run([%w[noop noop]])
+    end
+  end
+end
lib/jive.rb
@@ -4,8 +4,10 @@ require "fileutils"
 require "open3"
 
 require "jive/batch_runner"
+require "jive/git"
 require "jive/popen"
 require "jive/runner"
+require "jive/shell"
 require "jive/version"
 
 module Jive