Repository navigation
Expand file tree
/
Copy pathcli.ps1
More file actions
105 lines (92 loc) · 3.66 KB
/
Copy pathcli.ps1
File metadata and controls
105 lines (92 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<#
.SYNOPSIS
Headless front-end for github-setup.
.DESCRIPTION
Same engine as the GUI, for scripting and for reviewing exactly what the
buttons would do. Nothing here is duplicated logic: the plan, the apply and
the undo all come from engine.ps1.
.EXAMPLE
.\cli.ps1
Show the current state and what would change.
.EXAMPLE
.\cli.ps1 -Measure
Also time a GitHub round trip over HTTPS and over SSH.
.EXAMPLE
.\cli.ps1 -Apply -ScanRoot D:\code
Apply everything, including rewriting mirrored remotes under D:\code.
.EXAMPLE
.\cli.ps1 -Undo
Roll back the most recent -Apply using its backup.
#>
[CmdletBinding()]
param(
[switch]$Apply,
[switch]$Undo,
[switch]$Measure,
[string]$ScanRoot = '',
[string]$KeyPath = '',
[int]$Port = 443
)
. (Join-Path (Split-Path -Parent $PSCommandPath) 'engine.ps1')
if ($Undo) {
Write-Output '== undo =================================================='
foreach ($line in (Invoke-GitHubSetupUndo)) { Write-Output (' ' + $line) }
exit 0
}
$state = Get-GitHubSetupState -ScanRoot $ScanRoot
if ($KeyPath -ne '') { $state.KeyPath = $KeyPath; $state.KeyExists = (Test-Path $KeyPath) }
Write-Output '== state ================================================='
Write-Output (" key : {0} exists={1}" -f $state.KeyPath, $state.KeyExists)
Write-Output (" registered : {0} {1}" -f $state.Registered, $state.RegisterDetail)
Write-Output (" ssh block : present={0} host={1} port={2}" -f $state.SshBlock.Present, $state.SshBlock.HostName, $state.SshBlock.Port)
Write-Output (" url rewrite : {0}" -f $state.HasRewrite)
if ($ScanRoot -ne '') {
Write-Output (" remotes : {0} total, {1} need rewriting" -f $state.Remotes.Count, $state.NeedsFixCount)
foreach ($r in $state.Remotes) {
if ($r.NeedsFix) {
Write-Output (" {0} [{1}] {2}" -f (Split-Path $r.Repo -Leaf), $r.Name, $r.Url)
Write-Output (" -> {0}" -f $r.Target)
}
}
}
$plan = @(Get-GitHubSetupPlan -State $state)
Write-Output ''
Write-Output '== plan =================================================='
if ($plan.Count -eq 0) { Write-Output ' nothing to do' }
foreach ($p in $plan) { Write-Output (' - ' + $p.Text) }
if ($Measure) {
Write-Output ''
Write-Output '== latency ==============================================='
$m = Measure-GitHubTransport -KeyPath $state.KeyPath -Port $Port
Write-Output (" HTTPS ls-remote : {0,7} ms {1}" -f $m.HttpsMs, $(if ($m.HttpsOk) { 'ok' } else { 'failed' }))
if ($state.Registered) {
Write-Output (" SSH ls-remote : {0,7} ms {1}" -f $m.SshMs, $(if ($m.SshOk) { 'ok' } else { 'failed' }))
if ($m.SshOk -and $m.HttpsMs -gt 0) {
Write-Output (" SSH is {0}x faster" -f [math]::Round($m.HttpsMs / [math]::Max($m.SshMs, 1), 1))
}
} else {
Write-Output ' SSH ls-remote : skipped (key not registered)'
}
}
if ($Apply) {
Write-Output ''
Write-Output '== apply ================================================='
foreach ($line in (Invoke-GitHubSetupApply -State $state -ScanRoot $ScanRoot)) {
Write-Output (' ' + $line)
}
Write-Output ''
Write-Output '== verify ================================================'
$after = Get-GitHubSetupState -ScanRoot $ScanRoot
Write-Output (" ssh block : present={0} port={1}" -f $after.SshBlock.Present, $after.SshBlock.Port)
Write-Output (" url rewrite : {0}" -f $after.HasRewrite)
if ($after.Registered) {
$m = Measure-GitHubTransport -KeyPath $after.KeyPath -Port $Port
Write-Output (" git over SSH : {0} ms" -f $m.SshMs)
}
Write-Output ''
Write-Output ' undo with: .\cli.ps1 -Undo'
exit 0
}
Write-Output ''
Write-Output ' apply with: .\cli.ps1 -Apply -ScanRoot <dir>'
Write-Output ''