Last active
October 8, 2021 03:44
-
-
Save peromage/e12659a895ac5cc2e48d4910c732c1e9 to your computer and use it in GitHub Desktop.
Syncing my workspace repos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
A simple dirty and quick script to setup my repos | |
#> | |
param ($cmd) | |
$working_dir = Get-Location | |
$repos = @( | |
@("git@github.com:peromage/rice.git", "rice"), | |
@("git@github.com:peromage/poi.git", "poi"), | |
@("git@github.com:peromage/pew.git", "pew") | |
) | |
$idx_git = 0 | |
$idx_local = 1 | |
function loge { | |
Write-Host -ForegroundColor Red @args | |
} | |
function logw { | |
Write-Host -ForegroundColor Yellow @args | |
} | |
function logi { | |
Write-Host -ForegroundColor Cyan @args | |
} | |
function logd { | |
Write-Host @args | |
} | |
function git_check_status_clean { | |
git status --porcelain | |
} | |
function git_do_if_clean { | |
param ($local_dir) | |
if (-not (Test-Path $local_dir)) { | |
logw "Directory does not exist: $(Join-Path $working_dir $rice_local_dir), skipping" | |
return | |
} | |
Push-Location $local_dir | |
if ($null -eq $(git_check_status_clean)) { | |
git @args | |
} | |
else { | |
git status | |
} | |
Pop-Location | |
} | |
function git_clone { | |
param ($repo_url, $local_dir) | |
if (Test-Path $local_dir) { | |
loge "Directory already exists: $(Join-Path $working_dir $rice_local_dir), skipping" | |
return | |
} | |
git clone $repo_url $local_dir | |
} | |
$commands = @{} | |
$commands["init"] = { | |
foreach ($repo in $repos) { | |
logi "Initializing $($repo[$idx_local])" | |
git_clone $repo[$idx_git] $repo[$idx_local] | |
} | |
} | |
$commands["sync"] = { | |
foreach ($repo in $repos) { | |
logi "Syncing $($repo[$idx_local])" | |
git_do_if_clean $repo[$idx_local] pull | |
} | |
} | |
$commands["push"] = { | |
foreach ($repo in $repos) { | |
logi "Pushing $($repo[$idx_local])" | |
git_do_if_clean $repo[$idx_local] push | |
} | |
} | |
$commands["status"] = { | |
foreach ($repo in $repos) { | |
logi "Checking $($repo[$idx_local])" | |
git_do_if_clean $repo[$idx_local] status | |
} | |
} | |
if ($commands.ContainsKey($cmd)) { | |
&$commands[$cmd] @args | |
} else { | |
"${cmd}: Command not found!" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment