$cfgpath = "~/.scoop" function hashtable($obj) { $h = @{ } $obj.psobject.properties | ForEach-Object { $h[$_.name] = hashtable_val $_.value } return $h } function hashtable_val($obj) { if($null -eq $obj) { return $null } if($obj -is [array]) { $arr = @() $obj | ForEach-Object { $val = hashtable_val $_ if($val -is [array]) { $arr += ,@($val) } else { $arr += $val } } return ,$arr } if($obj.gettype().name -eq 'pscustomobject') { # -is is unreliable return hashtable $obj } if($obj -eq [bool]::TrueString -or $obj -eq [bool]::FalseString) { $obj = [System.Convert]::ToBoolean($obj) } return $obj # assume primitive } function load_cfg { if(!(test-path $cfgpath)) { return $null } try { hashtable (Get-Content $cfgpath -raw | convertfrom-json -ea stop) } catch { write-host "ERROR loading $cfgpath`: $($_.exception.message)" } } function get_config($name, $default) { if($null -eq $cfg.$name -and $null -ne $default) { return $default } return $cfg.$name } function set_config($name, $val) { if(!$cfg) { $cfg = @{ $name = $val } } else { if($val -eq [bool]::TrueString -or $val -eq [bool]::FalseString) { $val = [System.Convert]::ToBoolean($val) } $cfg.$name = $val } if($null -eq $val) { $cfg.remove($name) } convertto-json $cfg | set-content $cfgpath -encoding utf8 } $cfg = load_cfg # setup proxy # note: '@' and ':' in password must be escaped, e.g. 'p@ssword' -> p\@ssword' $p = get_config 'proxy' if($p) { try { $cred, $address = $p -split '(?