Add scripts for changing aliae
config
This commit is contained in:
parent
975430d97e
commit
9d680cea15
3 changed files with 117 additions and 2 deletions
15
scripts/Common/Software/aliae/Constants.ps1
Normal file
15
scripts/Common/Software/aliae/Constants.ps1
Normal file
|
@ -0,0 +1,15 @@
|
|||
<#
|
||||
.SYNOPSIS
|
||||
Gets a powershell expression which points to the global `aliae` configuration.
|
||||
#>
|
||||
function Get-GlobalConfigExpression {
|
||||
return "`"$($IsWindows ? "`$env:ProgramData" : "/etc")/aliae/aliae.yml`"";
|
||||
}
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Gets the path to the global `aliae` configuration.
|
||||
#>
|
||||
function Get-GlobalConfigPath {
|
||||
return & ([scriptblock]::Create((Get-GlobalConfigExpression)));
|
||||
}
|
|
@ -3,13 +3,15 @@ param(
|
|||
[hashtable] $Arguments
|
||||
)
|
||||
|
||||
. "$PSScriptRoot/Constants.ps1";
|
||||
. "$PSScriptRoot/../PowerShell/Profile.ps1";
|
||||
. "$PSScriptRoot/../../Scripts/Software.ps1";
|
||||
|
||||
Start-SoftwareInstaller @PSBoundParameters `
|
||||
-Configurator {
|
||||
$pathExpression = "`"$($IsWindows ? "`$env:ProgramData" : "/etc")/aliae/aliae.yml`"";
|
||||
$path = & ([scriptblock]::Create($pathExpression));
|
||||
. "$PSScriptRoot/Constants.ps1";
|
||||
$pathExpression = Get-GlobalConfigExpression;
|
||||
$path = Get-GlobalConfigPath;
|
||||
$null = New-Item -Force -ItemType Directory (Split-Path -Parent $path);
|
||||
Copy-Item -Force "$PSScriptRoot/aliae.yml" $path;
|
||||
|
||||
|
|
98
scripts/Common/Software/aliae/Manage.ps1
Normal file
98
scripts/Common/Software/aliae/Manage.ps1
Normal file
|
@ -0,0 +1,98 @@
|
|||
$null = New-Module {
|
||||
. "$PSScriptRoot/Constants.ps1";
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Adds an alias to an existing `aliae` configuration.
|
||||
|
||||
.PARAMETER Name
|
||||
The name of the alias to add.
|
||||
|
||||
.PARAMETER Value
|
||||
The script the alias should point to.
|
||||
|
||||
.PARAMETER User
|
||||
The user to add the alias to.
|
||||
#>
|
||||
function Add-Alias {
|
||||
param(
|
||||
[string] $Name,
|
||||
[string] $Value,
|
||||
[string] $User
|
||||
)
|
||||
|
||||
Edit-Config `
|
||||
-Variables @{
|
||||
Name = "$Name";
|
||||
Value = "$Value";
|
||||
} `
|
||||
".alias |= . + [{ name: env.Name, value: env.Value }]" `
|
||||
-User $User;
|
||||
}
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Adds an environment variable to an existing `aliae` configuration.
|
||||
|
||||
.PARAMETER Name
|
||||
The name of the variable to add.
|
||||
|
||||
.PARAMETER Value
|
||||
The value of the variable.
|
||||
#>
|
||||
function Add-EnvironmentVariable {
|
||||
param(
|
||||
[string] $Name,
|
||||
[string] $Value,
|
||||
[string] $User
|
||||
)
|
||||
|
||||
Edit-Config `
|
||||
-Variables @{
|
||||
Name = "$Name";
|
||||
Value = "$Value";
|
||||
} `
|
||||
".env |= . + [{ name: env.Name, value: env.Value }]" `
|
||||
-User $User;
|
||||
}
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Edits the underlying `aliae` configuration.
|
||||
|
||||
.PARAMETER Script
|
||||
The yq script to run over the configuration.
|
||||
|
||||
.PARAMETER User
|
||||
The user to edit the configuration for.
|
||||
#>
|
||||
function Edit-Config {
|
||||
param(
|
||||
[string] $Script,
|
||||
[hashtable] $Variables,
|
||||
[string] $User
|
||||
)
|
||||
|
||||
if ($User) {
|
||||
$path = "$($IsWindows ? "~" : "$(sudo -u $User bash -c "realpath ~")")/.aliae.yaml";
|
||||
} else {
|
||||
$path = Get-GlobalConfigPath;
|
||||
}
|
||||
|
||||
Start-Job {
|
||||
$file = New-TemporaryFile;
|
||||
$variables = $using:Variables;
|
||||
|
||||
foreach ($key in $variables.Keys) {
|
||||
Set-Item "Env:\$key" $variables[$key];
|
||||
}
|
||||
|
||||
sudo -u $using:User cp $using:path $file;
|
||||
yq -yi $using:Script $file;
|
||||
sudo -u $using:User cp $file $using:path;
|
||||
Remove-Item $file;
|
||||
} | Receive-Job -Wait;
|
||||
}
|
||||
|
||||
Export-ModuleMember -Function Add-Alias;
|
||||
};
|
Loading…
Reference in a new issue