Files
repo-tools/fcl
2024-05-15 17:42:45 +10:00

72 lines
2.1 KiB
Ruby
Executable File

#!/usr/bin/env ruby
## fcl: Fantastic CLoner
#
# Clones git repos into a predictable directory structure, according to local
# customs (HTTPS instead of SSH for Buildkite repos, for example)
#
# Makes educated guesses about incomplete repo identifiers.
#
# Also with some CLI pretty thanks to `gum`.
#
# TODO LIST
#
# [ ] Make a way to turn off the pretty, in case it's getting in the way
#
DEFAULT_ORG = "buildkite"
REPO_BASE = "#{ENV['HOME']}/repos"
def clone(repo_url, destination)
spinner_command = %Q[gum spin --show-output --spinner dot]
git_command = ['git', 'clone', repo_url, destination].join(' ')
system("#{spinner_command} -- #{git_command}")
end
def print_error(message)
system(%Q[gum style --bold --border=double --padding='0 1' --foreground 212 "#{message}"])
end
def repo_url(repo_str)
## GitHub HTTPS. We like these, use it as-is.
if repo_str =~ /^(git|https)\:\/\/github\.com\//
repo_str
## GitHub SSH. Let's convert this to HTTPS.
elsif repo_str =~ /^git@github\.com\:.+\.git$/
repo_str.sub(/^git@github\.com:/, 'https://github.com/')
# Host, user/org and repo. Assume it's HTTPS.
elsif repo_str =~ /^[^\/\s]+\/[^\/\s]+\/[^\/\s]+/
"https://#{repo_str}#{repo_str.end_with?('.git') ? '' : '.git'}"
# Just a user/org name and repo name. Assume it's on GitHub and make an HTTPS URL.
elsif repo_str =~ /^[\w\-_]+\/[\w\-_\.]+$/
"https://github.com/#{repo_str}.git"
# Just a repo name. Assume it's a DEFAULT_ORG repo on GitHub and make an HTTPS URL.
elsif repo_str =~ /^[\w\-_\.]+$/
"https://github.com/#{DEFAULT_ORG}/#{repo_str}.git"
else
raise "I can't make a repo URL out of '#{repo_str}'."
end
end
def destination_path(repo_str)
repo_elms = repo_str.split('/')
repo = repo_elms.pop.sub(/\.git$/, '')
org = repo_elms.pop || DEFAULT_ORG
forge = repo_elms.pop || 'github.com'
File.join(forge, org, repo)
end
destination = ARGV[1] || destination_path(ARGV[0])
full_dest = File.join(REPO_BASE, destination)
clone_url = repo_url(ARGV[0])
if Dir.exist?(full_dest)
log_error "#{destination} already exists. Doing nothing."
else
clone(clone_url, full_dest)
end