Skip to content

Commit

Permalink
refactor(config): Move configuration handling to core.ps1
Browse files Browse the repository at this point in the history
- Migrate ~\.scoop to ~\.config\scoop\config.json
- Remove hashtable and hashtable_val functions because ConvertFrom-Json is enough
- Add PowerShell 6 time conversion to tests
  • Loading branch information
r15ch13 committed May 10, 2019
1 parent 44975dc commit f7a9cd9
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 107 deletions.
95 changes: 0 additions & 95 deletions lib/config.ps1

This file was deleted.

78 changes: 78 additions & 0 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ $globaldir = $env:SCOOP_GLOBAL, "$env:ProgramData\scoop" | Select-Object -first
# Use at your own risk.
$cachedir = $env:SCOOP_CACHE, "$scoopdir\cache" | Select-Object -first 1

$configFile = "$env:USERPROFILE\.config\scoop\config.json"
if ((Test-Path "$env:USERPROFILE\.scoop") -and !(Test-Path $configFile)) {
New-Item -ItemType Directory "$env:USERPROFILE\.config\scoop" -ErrorAction Ignore | Out-Null
Move-Item "$env:USERPROFILE\.scoop" $configFile
warn "Scoops configurations has been migrated from '~/.scoop'"
warn "to '$configFile'"
}

# Note: Github disabled TLS 1.0 support on 2018-02-23. Need to enable TLS 1.2
# for all communication with api.github.com
function Optimize-SecurityProtocol {
Expand Down Expand Up @@ -58,6 +66,76 @@ function Show-DeprecatedWarning {
Write-Host " -> $($Invocation.PSCommandPath):$($Invocation.ScriptLineNumber):$($Invocation.OffsetInLine)" -ForegroundColor DarkGray
}

function load_cfg($file) {
if(!(Test-Path $file)) {
return $null
}

try {
return (Get-Content $file -Raw | ConvertFrom-Json -ErrorAction Stop)
} catch {
Write-Host "ERROR loading $file`: $($_.exception.message)"
}
}

$scoopConfig = load_cfg $configFile

function get_config($name, $default) {
if($null -eq $scoopConfig.$name -and $null -ne $default) {
return $default
}
return $scoopConfig.$name
}

function set_config($name, $val) {
if(!$scoopConfig) {
$scoopConfig = @{ $name = $val }
} else {
if($val -eq [bool]::TrueString -or $val -eq [bool]::FalseString) {
$val = [System.Convert]::ToBoolean($val)
}
$scoopConfig.$name = $val
}

if($null -eq $val) {
$scoopConfig.remove($name)
}

ConvertTo-Json $scoopConfig | Set-Content $configFile -Encoding utf8
}

function setup_proxy() {
# note: '@' and ':' in password must be escaped, e.g. 'p@ssword' -> p\@ssword'
$proxy = get_config 'proxy'
if(!$proxy) {
return
}
try {
$credentials, $address = $proxy -split '(?<!\\)@'
if(!$address) {
$address, $credentials = $credentials, $null # no credentials supplied
}

if($address -eq 'none') {
[net.webrequest]::defaultwebproxy = $null
} elseif($address -ne 'default') {
[net.webrequest]::defaultwebproxy = new-object net.webproxy "http://$address"
}

if($credentials -eq 'currentuser') {
[net.webrequest]::defaultwebproxy.credentials = [net.credentialcache]::defaultcredentials
} elseif($credentials) {
$username, $password = $credentials -split '(?<!\\):' | ForEach-Object { $_ -replace '\\([@:])','$1' }
[net.webrequest]::defaultwebproxy.credentials = new-object net.networkcredential($user, $pass)
}
} catch {
warn "Failed to use proxy '$proxy': $($_.exception.message)"
}
}

setup_proxy


# helper functions
function coalesce($a, $b) { if($a) { return $a } $b }

Expand Down
23 changes: 11 additions & 12 deletions test/Scoop-Config.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
. "$psscriptroot\..\lib\core.ps1"
. "$psscriptroot\..\lib\config.ps1"

describe "hashtable" -Tag 'Scoop' {
$json = '{ "one": 1, "two": [ { "a": "a" }, "b", 2 ], "three": { "four": 4 }, "five": true, "six": false, "seven": "\/Date(1529917395805)\/" }'
$json = '{ "one": 1, "two": [ { "a": "a" }, "b", 2 ], "three": { "four": 4 }, "five": true, "six": false, "seven": "\/Date(1529917395805)\/", "eight": "2019-03-18T15:22:09.3930000+01:00" }'

it "converts pscustomobject to hashtable" {
$obj = convertfrom-json $json
$ht = hashtable $obj
$obj = ConvertFrom-Json $json

$ht.one | should -beexactly 1
$ht.two[0].a | should -be "a"
$ht.two[1] | should -be "b"
$ht.two[2] | should -beexactly 2
$ht.three.four | should -beexactly 4
$ht.five | should -beexactly $true
$ht.six | should -beexactly $false
[System.DateTime]::Equals($ht.seven, $(New-Object System.DateTime (2018, 06, 25, 09, 03, 15, 805))) | should -betrue
$obj.one | should -beexactly 1
$obj.two[0].a | should -be "a"
$obj.two[1] | should -be "b"
$obj.two[2] | should -beexactly 2
$obj.three.four | should -beexactly 4
$obj.five | should -beexactly $true
$obj.six | should -beexactly $false
[System.DateTime]::Equals($obj.seven, $(New-Object System.DateTime (2018, 06, 25, 09, 03, 15, 805))) | should -betrue
[System.DateTime]::Equals([System.DateTime]::parse($obj.eight), $(New-Object System.DateTime (2019, 03, 18, 15, 22, 09, 393))) | should -betrue
}
}

0 comments on commit f7a9cd9

Please sign in to comment.