Commit ed288e9

mo khan <mo@mokhan.ca>
2021-02-05 23:38:08
feat: implement command to clone src from github
1 parent 5e84e90
Changed files (1)
lib
lib/jive/cli.rb
@@ -2,12 +2,52 @@
 
 require "thor"
 require "jive"
+require 'pathname'
 
 module Jive
   module Cli
     class App < Thor
+      def self.exit_on_failure?
+        true
+      end
+
       desc "clone <org>/<project>", "Clones the project from GitHub to ~/src/github.com/<org>/<project>"
       def clone(slug)
+        org, project = slug.split('/')
+        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],
+          [:cd, target_dir],
+        ])
+      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|
+          return unless execute(task)
+        end
+      end
+
+      def execute(command, env: {})
+        system(env, expand(command))
+      end
+
+      private
+
+      def expand(command)
+        Array(command)
+          .flatten
+          .map { |x| COMMAND_MAP.fetch(x, x).to_s }
+          .join(' ')
       end
     end
   end