Commit f9990fb

mo khan <mo@mokhan.ca>
2021-02-06 23:47:22
refactor: move cd command to git.rb
1 parent 2d6e113
Changed files (2)
lib/jive/cli.rb
@@ -13,10 +13,9 @@ module Jive
 
       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]
-        ])
+        runner.run_safely do
+          Git.new(runner).cd(slug)
+        end
       end
 
       desc "clone <org>/<project>", "git clone to ~/src/github.com/<org>/<project>"
lib/jive/git.rb
@@ -9,15 +9,26 @@ module Jive
     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}"]
-      ])
+      dir = target_dir_for(slug)
+      unless dir.exist?
+        shell.run_each([
+          [:mkdir, "-p", dir.parent.to_s],
+          [:git, "clone", "git@github.com:#{slug}.git", dir]
+        ])
+      end
+      cd(slug)
+    end
+
+    def cd(slug)
+      dir = target_dir_for(slug)
+      if dir.exist?
+        shell.after_run([
+          ["cd", dir],
+          ["setenv", "JIVE_LAST_RUN=#{Time.now.to_i}"]
+        ])
+      else
+        clone(slug)
+      end
     end
 
     private