| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright: (c) 2020, Jamie Magee <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
#AnsibleRequires -CSharpUtil Ansible.Basic |
| 7 |
|
| 8 |
$spec = @{ |
| 9 |
options = @{ |
| 10 |
name = @{ type = "str"; required = $true } |
| 11 |
repo = @{ type = "str" } |
| 12 |
state = @{ type = "str"; default = "present"; choices = "present", "absent" } |
| 13 |
} |
| 14 |
supports_check_mode = $true |
| 15 |
} |
| 16 |
|
| 17 |
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) |
| 18 |
|
| 19 |
$name = $module.Params.name |
| 20 |
$repo = $module.Params.repo |
| 21 |
$state = $module.Params.state |
| 22 |
|
| 23 |
# Kept for backwards compatibility |
| 24 |
$module.Result.rc = 0 |
| 25 |
|
| 26 |
function Get-Scoop { |
| 27 |
# Scoop doesn't have refreshenv like Chocolatey |
| 28 |
# Let's try to update PATH first |
| 29 |
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User") |
| 30 |
|
| 31 |
$scoop_app = Get-Command -Name scoop.ps1 -Type ExternalScript -ErrorAction SilentlyContinue |
| 32 |
|
| 33 |
if ($module.CheckMode -and $null -eq $scoop_app) { |
| 34 |
$module.Result.skipped = $true |
| 35 |
$module.Result.msg = "Skipped check mode run on win_scoop_bucket as scoop.ps1 cannot be found on the system" |
| 36 |
$module.ExitJson() |
| 37 |
} |
| 38 |
|
| 39 |
if ($null -eq $scoop_app -or -not (Test-Path -LiteralPath $scoop_app.Path)) { |
| 40 |
$module.FailJson("Failed to find scoop.ps1, make sure it is added to the PATH") |
| 41 |
} |
| 42 |
|
| 43 |
return $scoop_app.Path |
| 44 |
} |
| 45 |
|
| 46 |
function Get-ScoopBucket { |
| 47 |
param( |
| 48 |
[Parameter(Mandatory = $true)] [string]$scoop_path |
| 49 |
) |
| 50 |
|
| 51 |
&$scoop_path bucket list |
| 52 |
} |
| 53 |
|
| 54 |
function Uninstall-ScoopBucket { |
| 55 |
param( |
| 56 |
[Parameter(Mandatory = $true)] [string]$scoop_path, |
| 57 |
[Parameter(Mandatory = $true)] [String]$bucket |
| 58 |
) |
| 59 |
|
| 60 |
$arguments = @( |
| 61 |
"bucket", |
| 62 |
"rm" |
| 63 |
$bucket |
| 64 |
if ($repo) { $repo } |
| 65 |
) |
| 66 |
if (-not $module.CheckMode) { |
| 67 |
$res = (&$scoop_path @arguments) -join "`n" |
| 68 |
if (-not $?) { |
| 69 |
$module.Result.stdout = $res |
| 70 |
$module.Result.rc = 1 |
| 71 |
$module.FailJson("Failed to remove scoop bucket, see stdout for more details") |
| 72 |
} |
| 73 |
elseif ($module.Verbosity -gt 1) { |
| 74 |
$module.Result.stdout = $res |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
$module.Result.changed = $true |
| 79 |
} |
| 80 |
|
| 81 |
function Install-ScoopBucket { |
| 82 |
param( |
| 83 |
[Parameter(Mandatory = $true)] [string]$scoop_path, |
| 84 |
[Parameter(Mandatory = $true)] [String]$bucket, |
| 85 |
[String]$repo |
| 86 |
) |
| 87 |
|
| 88 |
$arguments = @( |
| 89 |
"bucket" |
| 90 |
"add" |
| 91 |
$bucket |
| 92 |
if ($repo) { $repo } |
| 93 |
) |
| 94 |
if (-not $module.CheckMode) { |
| 95 |
$res = (&$scoop_path @arguments) -join "`n" |
| 96 |
if (-not $?) { |
| 97 |
$module.Result.stdout = $res |
| 98 |
$module.Result.rc = 1 |
| 99 |
$module.FailJson("Failed to add scoop bucket, see stdout for more details") |
| 100 |
} |
| 101 |
elseif ($module.Verbosity -gt 1) { |
| 102 |
$module.Result.stdout = $res |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
$module.Result.changed = $true |
| 107 |
} |
| 108 |
|
| 109 |
$scoop_path = Get-Scoop |
| 110 |
|
| 111 |
$installed_buckets = Get-ScoopBucket -scoop_path $scoop_path |
| 112 |
|
| 113 |
if ($state -in @("absent")) { |
| 114 |
if ($installed_buckets.Name -contains $name) { |
| 115 |
Uninstall-ScoopBucket -scoop_path $scoop_path -bucket $name |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
if ($state -in @("present")) { |
| 120 |
if ($installed_buckets.Name -notcontains $name) { |
| 121 |
Install-ScoopBucket -scoop_path $scoop_path -bucket $name -repo $repo |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
$module.ExitJson() |