Skip to content

Commit 28c8482

Browse files
committed
Dry out the logging
1 parent 49c73e6 commit 28c8482

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

backup_github_repositories.ps1

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,20 @@ if (!$backupDirectory) {
7676
$backupDirectory = $(Join-Path -Path "$PSScriptRoot" -ChildPath $(Get-Date -UFormat "%Y-%m-%d"))
7777
}
7878

79+
# Log a message to the commandline.
80+
function Write-Message([string] $message, [string] $color = 'Yellow') {
81+
82+
Write-Host "${message}" -foregroundcolor $color
83+
}
84+
7985
#
8086
# Clone a remote GitHub repository into a local directory.
8187
#
8288
# @see https://git-scm.com/docs/git-clone#git-clone---mirror
8389
#
8490
function Backup-GitHubRepository([string] $fullName, [string] $directory) {
8591

86-
Write-Host "Starting backup of https://github.com/${fullName} to ${directory}..." -ForegroundColor DarkYellow
92+
Write-Message "Starting backup of https://github.com/${fullName} to ${directory}..." 'DarkYellow'
8793

8894
git clone --mirror "[email protected]:${fullName}.git" "${directory}"
8995
}
@@ -101,6 +107,7 @@ function Get-TotalRepositoriesSizeInMegabytes([object] $repositories) {
101107
$([math]::Round($totalSizeInKilobytes/1024))
102108
}
103109

110+
104111
# Measure the execution time of the backup script.
105112
$stopwatch = [System.Diagnostics.Stopwatch]::startNew()
106113

@@ -128,18 +135,22 @@ if($organisationName) {
128135
# @see https://developer.github.com/v3/auth/#basic-authentication
129136
#
130137
$basicAuthenticationCredentials = "${username}:${plainTextUserSecret}"
131-
$encodedBasicAuthenticationCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($basicAuthenticationCredentials))
138+
$encodedBasicAuthenticationCredentials = [System.Convert]::ToBase64String(
139+
[System.Text.Encoding]::ASCII.GetBytes($basicAuthenticationCredentials)
140+
)
132141
$requestHeaders = @{
133142
Authorization = "Basic $encodedBasicAuthenticationCredentials"
134143
}
135144

136145
# Request the GitHub API to get all repositories of a user or an organisation.
137-
Write-Host "Requesting '${gitHubRepositoriesUrl}'..." -foregroundcolor Yellow
138-
$repositories = Invoke-WebRequest -Uri $gitHubRepositoriesUrl -Headers $requestHeaders | Select-Object -ExpandProperty Content | ConvertFrom-Json
146+
Write-Message "Requesting '${gitHubRepositoriesUrl}'..."
147+
$repositories = Invoke-WebRequest -Uri $gitHubRepositoriesUrl -Headers $requestHeaders | `
148+
Select-Object -ExpandProperty Content | `
149+
ConvertFrom-Json
139150

140151
# Print a userfriendly message what will happen next.
141152
$totalSizeInMegabytes = Get-TotalRepositoriesSizeInMegabytes -repositories $repositories
142-
Write-Host "Cloning $($repositories.Count) repositories (~${totalSizeInMegabytes} MB) into '${backupDirectory}'..." -foregroundcolor Yellow
153+
Write-Message "Cloning $($repositories.Count) repositories (~${totalSizeInMegabytes} MB) into '${backupDirectory}'..."
143154

144155
# Clone each repository into the backup directory.
145156
ForEach ($repository in $repositories) {
@@ -150,4 +161,4 @@ ForEach ($repository in $repositories) {
150161

151162
$stopwatch.Stop()
152163
$durationInSeconds = $stopwatch.Elapsed.Seconds
153-
Write-Host "Successfully finished the backup in ${durationInSeconds} seconds..." -foregroundcolor Yellow
164+
Write-Message "Successfully finished the backup in ${durationInSeconds} seconds..."

0 commit comments

Comments
 (0)