commit 242002c007059258e11c0f477fb8ba9bd892ece7 Author: Lucas Wilson-Richter Date: Tue May 14 18:56:13 2024 +1000 fcl script with opinions & fancy styles Depends on: - gum (https://github.com/charmbracelet/gum) - git diff --git a/fcl b/fcl new file mode 100755 index 0000000..1305d10 --- /dev/null +++ b/fcl @@ -0,0 +1,56 @@ +#!/usr/bin/env ruby + +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