Commit 2d6e113

mo khan <mo@mokhan.ca>
2021-02-06 23:32:08
feat: add cd command
1 parent a141c30
Changed files (3)
lib/jive/cli.rb
@@ -11,12 +11,18 @@ module Jive
         true
       end
 
+      desc "cd <org>/<project>", "cd to ~/src/github.com/<org>/<project>"
+      def cd(slug)
+        target_dir = Pathname.new(Dir.home).join("src/github.com/#{slug}")
+        runner.after_run([
+          [:cd, target_dir.to_s]
+        ])
+      end
+
       desc "clone <org>/<project>", "git clone to ~/src/github.com/<org>/<project>"
       def clone(slug)
-        shell = ::Jive::Shell.new
-        shell.run_safely do
-          git = Git.new(shell)
-          git.clone(slug)
+        runner.run_safely do
+          Git.new(runner).clone(slug)
         end
       end
 
@@ -28,6 +34,12 @@ module Jive
             source #{::Jive.root.join("jive.sh")}
         MESSAGE
       end
+
+      private
+
+      def runner
+        @runner ||= ::Jive::Shell.new
+      end
     end
   end
 end
lib/jive/shell.rb
@@ -20,9 +20,12 @@ module Jive
     end
 
     def after_run(tasks)
-      finalizer_fd = 9
+      finalizer_fd = 42
       pipe = IO.new(finalizer_fd)
       pipe.puts(tasks.map { |x| x.join(":") }.join("\n"))
+    rescue Errno::EBADF => e
+      puts e
+      exit 1
     end
 
     def expand(command)
@@ -35,7 +38,7 @@ module Jive
     def run_safely
       yield
     rescue StandardError => e
-      say e
+      puts e
       after_run([%w[noop noop]])
     end
   end
jive.sh
@@ -43,7 +43,7 @@ __jive_open_pipe() {
   local tmpfile
   tmpfile="$(\mktemp -u)"
 
-  exec 9>"${tmpfile}" # Open the tempfile for writing on FD 9.
+  exec 42>"${tmpfile}" # Open the tempfile for writing on FD 42.
   exec 8<"${tmpfile}" # Open the tempfile for reading on FD 8.
   \rm -f "${tmpfile}" # Unlink the tempfile. (we've already opened it).
 }
@@ -76,7 +76,7 @@ __jive_flush_tasks() {
 
 __jive_close_pipe() {
   exec 8<&- # close FD 8.
-  exec 9<&- # close FD 9.
+  exec 42<&- # close FD 42.
 }
 
 jive() {