7
7
[ValidateSet (' repository' , ' psresource' , ' repositorylist' , ' psresourcelist' )]
8
8
[string ]$ResourceType ,
9
9
[Parameter (Mandatory = $true )]
10
- [ValidateSet (' get' , ' set' , ' test' , ' export' )]
10
+ [ValidateSet (' get' , ' set' , ' test' , ' delete ' , ' export' )]
11
11
[string ]$Operation ,
12
12
[Parameter (ValueFromPipeline )]
13
13
$stdinput
14
14
)
15
15
16
+ enum Scope {
17
+ CurrentUser
18
+ AllUsers
19
+ }
20
+
21
+ class PSResource {
22
+ [string ]$name
23
+ [string ]$version
24
+ [Scope ]$scope
25
+ [string ]$repositoryName
26
+ [bool ]$preRelease
27
+ [bool ]$_exist
28
+ [bool ]$_inDesiredState
29
+
30
+ PSResource([string ]$name , [string ]$version , [Scope ]$scope , [string ]$repositoryName , [bool ]$preRelease ) {
31
+ $this.name = $name
32
+ $this.version = $version
33
+ $this.scope = $scope
34
+ $this.repositoryName = $repositoryName
35
+ $this.preRelease = $preRelease
36
+ $this._exist = $true
37
+ }
38
+ }
39
+
40
+ class PSResourceList {
41
+ [string ]$repositoryName
42
+ [Scope ]$scope
43
+ [PSResource []]$resources
44
+
45
+ PSResourceList([string ]$repositoryName , [Scope ]$scope , [PSResource []]$resources ) {
46
+ $this.repositoryName = $repositoryName
47
+ $this.scope = $scope
48
+ $this.resources = $resources
49
+ }
50
+ }
51
+
52
+ class Repository {
53
+ [string ]$name
54
+ [string ]$uri
55
+ [bool ]$trusted
56
+ [int ]$priority
57
+ [string ]$repositoryType
58
+ [bool ]$_exist
59
+
60
+ Repository([string ]$name ) {
61
+ $this.name = $name
62
+ $this._exist = $false
63
+ }
64
+
65
+ Repository([string ]$name , [string ]$uri , [bool ]$trusted , [int ]$priority , [string ]$repositoryType ) {
66
+ $this.name = $name
67
+ $this.uri = $uri
68
+ $this.trusted = $trusted
69
+ $this.priority = $priority
70
+ $this.repositoryType = $repositoryType
71
+ $this._exist = $true
72
+ }
73
+
74
+ Repository([PSCustomObject ]$repositoryInfo ) {
75
+ $this.name = $repositoryInfo.Name
76
+ $this.uri = $repositoryInfo.Uri
77
+ $this.trusted = $repositoryInfo.Trusted
78
+ $this.priority = $repositoryInfo.Priority
79
+ $this.repositoryType = $repositoryInfo.ApiVersion
80
+ $this._exist = $true
81
+ }
82
+
83
+ Repository([string ]$name , [bool ]$exist ) {
84
+ $this.name = $name
85
+ $this._exist = $exist
86
+ }
87
+ }
88
+
16
89
function Write-Trace {
17
90
param (
18
91
[string ]$message ,
@@ -25,8 +98,12 @@ function Write-Trace {
25
98
$level.ToLower () = $message
26
99
} | ConvertTo-Json - Compress
27
100
28
- $host.ui.WriteInformation ($trace )
29
-
101
+ if ($env: SKIP_TRACE ) {
102
+ $host.ui.WriteVerboseLine ($trace )
103
+ }
104
+ else {
105
+ $host.ui.WriteErrorLine ($trace )
106
+ }
30
107
}
31
108
32
109
# catch any un-caught exception and write it to the error stream
@@ -46,14 +123,30 @@ function GetOperation {
46
123
47
124
switch ($ResourceType ) {
48
125
' repository' {
49
- $rep = Get-PSResourceRepository - Name $inputObj.Name - ErrorVariable err - ErrorAction SilentlyContinue
126
+ $inputRepository = [ Repository ]::new( $inputObj )
50
127
51
- if ($err.FullyQualifiedErrorId -eq ' ErrorGettingSpecifiedRepo,Microsoft.PowerShell.PSResourceGet.Cmdlets.GetPSResourceRepository' ) {
52
- return PopulateRepositoryObject - RepositoryInfo $null
128
+ $rep = Get-PSResourceRepository - Name $inputRepository.Name - ErrorVariable err - ErrorAction SilentlyContinue
129
+
130
+ $ret = if ($err.FullyQualifiedErrorId -eq ' ErrorGettingSpecifiedRepo,Microsoft.PowerShell.PSResourceGet.Cmdlets.GetPSResourceRepository' ) {
131
+ Write-Trace - message " Repository not found: $ ( $inputRepository.Name ) . Returning _exist = false"
132
+ [Repository ]::new(
133
+ $InputRepository.Name ,
134
+ $false
135
+ )
136
+ }
137
+ else {
138
+ [Repository ]::new(
139
+ $rep.Name ,
140
+ $rep.Uri ,
141
+ $rep.Trusted ,
142
+ $rep.Priority ,
143
+ $rep.ApiVersion
144
+ )
145
+
146
+ Write-Trace - message " Returning repository object for: $ ( $ret.Name ) "
53
147
}
54
148
55
- $ret = PopulateRepositoryObject - RepositoryInfo $rep
56
- return $ret
149
+ return ( $ret | ConvertTo-Json - Compress )
57
150
}
58
151
59
152
' repositorylist' { throw [System.NotImplementedException ]::new(" Get operation is not implemented for RepositoryList resource." ) }
@@ -107,11 +200,23 @@ function GetOperation {
107
200
function ExportOperation {
108
201
switch ($ResourceType ) {
109
202
' repository' {
110
- $rep = Get-PSResourceRepository - ErrorAction Stop
203
+ $rep = Get-PSResourceRepository - ErrorAction SilentlyContinue
204
+
205
+ if (-not $rep ) {
206
+ Write-Trace - message " No repositories found. Returning empty array." - level info
207
+ return @ ()
208
+ }
111
209
112
210
$rep | ForEach-Object {
113
- PopulateRepositoryObject - RepositoryInfo $_
211
+ [Repository ]::new(
212
+ $_.Name ,
213
+ $_.Uri ,
214
+ $_.Trusted ,
215
+ $_.Priority ,
216
+ $_.ApiVersion
217
+ ) | ConvertTo-Json - Compress
114
218
}
219
+
115
220
}
116
221
117
222
' repositorylist' { throw [System.NotImplementedException ]::new(" Get operation is not implemented for RepositoryList resource." ) }
@@ -256,6 +361,39 @@ function SetOperation {
256
361
default { throw " Unknown ResourceType: $ResourceType " }
257
362
}
258
363
}
364
+
365
+ function DeleteOperation {
366
+ param (
367
+ [string ]$ResourceType
368
+ )
369
+
370
+ $inputObj = $stdinput | ConvertFrom-Json - ErrorAction Stop
371
+ switch ($ResourceType ) {
372
+ ' repository' {
373
+ if (-not $inputObj._exist -ne $false ) {
374
+ throw " _exist property is not set to false for the repository. Cannot delete."
375
+ }
376
+
377
+ $rep = Get-PSResourceRepository - Name $inputObj.Name - ErrorAction SilentlyContinue
378
+
379
+ if ($null -ne $rep ) {
380
+ Unregister-PSResourceRepository - Name $inputObj.Name
381
+ }
382
+ else {
383
+ Write-Trace - message " Repository not found: $ ( $inputObj.Name ) . Nothing to delete." - level info
384
+ }
385
+
386
+ return GetOperation - ResourceType $ResourceType
387
+ }
388
+
389
+ ' repositorylist' { throw [System.NotImplementedException ]::new(" Delete operation is not implemented for RepositoryList resource." ) }
390
+ ' psresource' { throw [System.NotImplementedException ]::new(" Delete operation is not implemented for PSResource resource." ) }
391
+ ' psresourcelist' { throw [System.NotImplementedException ]::new(" Delete operation is not implemented for PSResourceList resource." ) }
392
+ default { throw " Unknown ResourceType: $ResourceType " }
393
+ }
394
+ }
395
+
396
+
259
397
function FilterPSResourcesByRepository {
260
398
param (
261
399
$allPSResources ,
@@ -346,39 +484,12 @@ function PopulatePSResourcesObject {
346
484
}
347
485
}
348
486
349
- function PopulateRepositoryObject {
350
- param (
351
- $RepositoryInfo
352
- )
353
-
354
- $repository = if (-not $RepositoryInfo ) {
355
- Write-Trace - message " RepositoryInfo is null or empty. Returning _exist = false"
356
-
357
- $inputJson = $stdinput | ConvertFrom-Json - ErrorAction Stop
358
487
359
- [pscustomobject ]@ {
360
- name = $inputJson.Name
361
- _exist = $false
362
- }
363
- }
364
- else {
365
- Write-Trace - message " Populating repository object for: $ ( $RepositoryInfo.Name ) "
366
- [pscustomobject ]@ {
367
- name = $RepositoryInfo.Name
368
- uri = $RepositoryInfo.Uri
369
- trusted = $RepositoryInfo.Trusted
370
- priority = $RepositoryInfo.Priority
371
- repositoryType = $RepositoryInfo.ApiVersion
372
- _exist = $true
373
- }
374
- }
375
-
376
- return ($repository | ConvertTo-Json - Compress)
377
- }
378
488
379
489
switch ($Operation.ToLower ()) {
380
490
' get' { return (GetOperation - ResourceType $ResourceType ) }
381
491
' set' { return (SetOperation - ResourceType $ResourceType ) }
382
492
' export' { return (ExportOperation - ResourceType $ResourceType ) }
493
+ ' delete' { return (DeleteOperation - ResourceType $ResourceType ) }
383
494
default { throw " Unknown Operation: $Operation " }
384
495
}
0 commit comments