2024-08-23 21:44:51 +00:00
|
|
|
$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";
|
|
|
|
} `
|
2024-08-23 22:08:58 +00:00
|
|
|
".alias |= [((. // [])[] | select(.name != env.Name))] + [{ name: env.Name, value: env.Value }]" `
|
2024-08-23 21:44:51 +00:00
|
|
|
-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";
|
|
|
|
} `
|
2024-08-23 22:08:58 +00:00
|
|
|
".env |= [((. // [])[] | select(.name != env.Name))] + [{ name: env.Name, value: env.Value }]" `
|
2024-08-23 21:44:51 +00:00
|
|
|
-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
|
|
|
|
)
|
|
|
|
|
2024-09-27 23:24:28 +00:00
|
|
|
$flags = @();
|
|
|
|
|
2024-08-23 21:44:51 +00:00
|
|
|
if ($User) {
|
2024-09-27 23:24:28 +00:00
|
|
|
$flags = @("-u", "$User")
|
|
|
|
$path = "$($IsWindows ? "~" : "$(sudo @flags bash -c "realpath ~")")/.aliae.yaml";
|
2024-08-23 21:44:51 +00:00
|
|
|
} else {
|
|
|
|
$path = Get-GlobalConfigPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
Start-Job {
|
|
|
|
$file = New-TemporaryFile;
|
|
|
|
$variables = $using:Variables;
|
|
|
|
|
|
|
|
foreach ($key in $variables.Keys) {
|
|
|
|
Set-Item "Env:\$key" $variables[$key];
|
|
|
|
}
|
|
|
|
|
2024-09-27 23:24:28 +00:00
|
|
|
sudo @using:flags pwsh -CommandWithArgs 'Get-Content -Raw $args[0]' $using:path | Set-Content $file;
|
2024-08-23 21:44:51 +00:00
|
|
|
yq -yi $using:Script $file;
|
2024-09-27 23:24:28 +00:00
|
|
|
sudo @using:flags pwsh -CommandWithArgs 'Set-Content $args[0] -Value $args[1]' $using:path (Get-Content -Raw $file);
|
2024-08-23 21:44:51 +00:00
|
|
|
Remove-Item $file;
|
|
|
|
} | Receive-Job -Wait;
|
|
|
|
}
|
|
|
|
|
2024-08-23 21:50:13 +00:00
|
|
|
Export-ModuleMember -Function Add-Alias,Add-EnvironmentVariable;
|
2024-08-23 21:44:51 +00:00
|
|
|
};
|