# frozen_string_literal: true require "thor" require "jive" require "pathname" module Jive module Cli class App < Thor def self.exit_on_failure? true end desc "clone /", "git clone to ~/src/github.com//" 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}"] ]) end desc "setup", "provide instructions to integrate into shell" def setup say <<~MESSAGE Include the following in your ~/.bash_profile 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