Add basic win_winget module
This commit is contained in:
96
library/win_winget.ps1
Normal file
96
library/win_winget.ps1
Normal file
@@ -0,0 +1,96 @@
|
||||
#!powershell
|
||||
|
||||
#AnsibleRequires -CSharpUtil Ansible.Basic
|
||||
#AnsibleRequires -PowerShell Ansible.ModuleUtils.CommandUtil
|
||||
|
||||
$module = [Ansible.Basic.AnsibleModule]::Create($args, @{
|
||||
options = @{
|
||||
name = @{
|
||||
type = "str"
|
||||
default = $null
|
||||
}
|
||||
state = @{
|
||||
type = "str"
|
||||
default = "present"
|
||||
choices = ("absent", "latest", "present")
|
||||
}
|
||||
}
|
||||
supports_check_mode = $false
|
||||
})
|
||||
|
||||
$name = $module.Params.name
|
||||
$state = $module.Params.state
|
||||
$winget = Get-ExecutablePath "winget"
|
||||
$noPackageString = "No installed package found matching input criteria."
|
||||
|
||||
if ($name -and $id) {
|
||||
$module.FailJson("name `"$name`" and id `"$id`" must not both be provided")
|
||||
}
|
||||
|
||||
function Test-Installed {
|
||||
$command = "`"$winget`" list `"$name`""
|
||||
$result = Run-Command -command $command
|
||||
if ($result.rc -eq 0) {
|
||||
return $true
|
||||
}
|
||||
return $false
|
||||
}
|
||||
|
||||
function Test-UpgradeAvailable {
|
||||
if (!(Test-Installed)) {
|
||||
return $true
|
||||
}
|
||||
$command = "`"$winget`" list --upgrade-available `"$name`""
|
||||
$result = Run-Command -command $command
|
||||
if ($result.stdout.Contains($noPackageString)) {
|
||||
return $false
|
||||
}
|
||||
return $true
|
||||
}
|
||||
|
||||
switch ($state) {
|
||||
"absent" {
|
||||
if (Test-Installed) {
|
||||
$command = "`"$winget`" uninstall `"$name`""
|
||||
$result = Run-Command -command $command
|
||||
if ($result.rc -ne 0) {
|
||||
$module.Result.rc = $result.rc
|
||||
$module.Result.stdout = $result.stdout
|
||||
$module.FailJson("Failed to uninstall package `"$name`"")
|
||||
}
|
||||
$module.Result.stdout = $result.stdout
|
||||
$module.Result.changed = $true
|
||||
}
|
||||
}
|
||||
|
||||
"latest" {
|
||||
if (Test-UpgradeAvailable) {
|
||||
$command = "`"$winget`" install `"$name`""
|
||||
$result = Run-Command -command $command
|
||||
if ($result.rc -ne 0) {
|
||||
$module.Result.rc = $result.rc
|
||||
$module.Result.stdout = $result.stdout
|
||||
$module.FailJson("Failed to install package `"$name`"")
|
||||
}
|
||||
$module.Result.stdout = $result.stdout
|
||||
$module.Result.changed = $true
|
||||
}
|
||||
}
|
||||
|
||||
"present" {
|
||||
if (!(Test-Installed)) {
|
||||
$command = "`"$winget`" install `"$name`""
|
||||
$result = Run-Command -command $command
|
||||
if ($result.rc -ne 0) {
|
||||
$module.Result.rc = $result.rc
|
||||
$module.Result.stdout = $result.stdout
|
||||
$module.FailJson("Failed to install package `"$name`"")
|
||||
}
|
||||
$module.Result.stdout = $result.stdout
|
||||
$module.Result.changed = $true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$module.Result.rc = 0
|
||||
$module.ExitJson()
|
||||
Reference in New Issue
Block a user