@@ -5,66 +5,114 @@ import { CatalogItemRichened, CatalogItemWithName } from "./types/catalog";
5
5
import { Secret , StoredSecret , Policy } from "./types/secrets" ;
6
6
7
7
namespace Secrets {
8
- export async function getSecrets ( client : v1 . DockerDesktopClient ) : Promise < Secret [ ] > {
9
- const response = await client . extension . host ?. cli . exec ( 'host-binary' , [ 'list' ] ) ;
10
- if ( ! response ) {
11
- client . desktopUI . toast . error ( 'Failed to get secrets. Could not get response from host-binary.' )
12
- }
13
- if ( response ?. stderr ) {
14
- client . desktopUI . toast . error ( 'Failed to get secrets: ' + JSON . stringify ( response ) )
15
- }
16
- return JSON . parse ( response ?. stdout || '[]' ) ;
8
+ export async function getSecrets (
9
+ client : v1 . DockerDesktopClient
10
+ ) : Promise < Secret [ ] > {
11
+ const response = await client . extension . host ?. cli . exec ( "host-binary" , [
12
+ "list" ,
13
+ ] ) ;
14
+ if ( ! response ) {
15
+ client . desktopUI . toast . error (
16
+ "Failed to get secrets. Could not get response from host-binary."
17
+ ) ;
17
18
}
18
-
19
- export async function addSecret ( client : v1 . DockerDesktopClient , secret : Secret ) : Promise < void > {
20
- try {
21
- const response = await client . extension . host ?. cli . exec ( 'host-binary' , [ '--name' , secret . name , '--value' , `'${ secret . value } '` ] ) ;
22
- client . desktopUI . toast . success ( 'Secret set successfully' )
23
- if ( ! response ) {
24
- client . desktopUI . toast . error ( 'Failed to set secret. Could not get response from host-binary.' )
25
- }
26
- if ( response ?. stderr ) {
27
- client . desktopUI . toast . error ( 'Failed to set secret: ' + JSON . stringify ( response ) )
28
- }
29
- } catch ( error ) {
30
- if ( ( error as any ) . stderr ) {
31
- client . desktopUI . toast . error ( 'Failed to set secret: ' + JSON . stringify ( error ) )
32
- } else {
33
- client . desktopUI . toast . error ( 'Failed to set secret: ' + error )
34
- }
35
- }
19
+ if ( response ?. stderr ) {
20
+ client . desktopUI . toast . error (
21
+ "Failed to get secrets: " + JSON . stringify ( response )
22
+ ) ;
36
23
}
24
+ return JSON . parse ( response ?. stdout || "[]" ) ;
25
+ }
37
26
38
- export async function deleteSecret ( client : v1 . DockerDesktopClient , name : string ) : Promise < void > {
39
- try {
40
- const response = await client . extension . host ?. cli . exec ( 'host-binary' , [ 'delete' , '--name' , name ] ) ;
41
- if ( ! response ) {
42
- client . desktopUI . toast . error ( 'Failed to delete secret. Could not get response from host-binary.' )
43
- throw new Error ( 'Failed to delete secret. Could not get response from host-binary.' )
44
- }
45
- if ( response ?. stderr ) {
46
- client . desktopUI . toast . error ( 'Failed to delete secret: ' + JSON . stringify ( response ) )
47
- throw new Error ( 'Failed to delete secret: ' + JSON . stringify ( response ) )
48
- }
49
- client . desktopUI . toast . success ( 'Secret deleted successfully' )
50
- } catch ( error ) {
51
- if ( ( error as any ) . stderr ) {
52
- client . desktopUI . toast . error ( 'Failed to delete secret: ' + JSON . stringify ( error ) )
53
- } else {
54
- client . desktopUI . toast . error ( 'Failed to delete secret: ' + error )
55
- }
56
- }
27
+ export async function addSecret (
28
+ client : v1 . DockerDesktopClient ,
29
+ secret : Secret
30
+ ) : Promise < void > {
31
+ try {
32
+ const response = await client . extension . host ?. cli . exec ( "host-binary" , [
33
+ "--name" ,
34
+ secret . name ,
35
+ "--value" ,
36
+ `'${ secret . value } '` ,
37
+ ] ) ;
38
+ if ( ! response ) {
39
+ client . desktopUI . toast . error (
40
+ "Failed to set secret. Could not get response from host-binary."
41
+ ) ;
42
+ }
43
+ if ( response ?. stderr ) {
44
+ client . desktopUI . toast . error (
45
+ "Failed to set secret: " + JSON . stringify ( response )
46
+ ) ;
47
+ }
48
+ } catch ( error ) {
49
+ if ( ( error as any ) . stderr ) {
50
+ client . desktopUI . toast . error (
51
+ "Failed to set secret: " + JSON . stringify ( error )
52
+ ) ;
53
+ } else {
54
+ client . desktopUI . toast . error ( "Failed to set secret: " + error ) ;
55
+ }
57
56
}
57
+ }
58
58
59
- // Get all relevant secrets for a given set of catalog items
60
- export function getAllSecretNames ( catalogItems : CatalogItemWithName [ ] ) : string [ ] {
61
- return catalogItems . map ( ( item ) => item . secrets || [ ] ) . flat ( ) . map ( ( secret ) => secret . name ) ;
59
+ export async function deleteSecret (
60
+ client : v1 . DockerDesktopClient ,
61
+ name : string
62
+ ) : Promise < void > {
63
+ try {
64
+ const response = await client . extension . host ?. cli . exec ( "host-binary" , [
65
+ "delete" ,
66
+ "--name" ,
67
+ name ,
68
+ ] ) ;
69
+ if ( ! response ) {
70
+ client . desktopUI . toast . error (
71
+ "Failed to delete secret. Could not get response from host-binary."
72
+ ) ;
73
+ throw new Error (
74
+ "Failed to delete secret. Could not get response from host-binary."
75
+ ) ;
76
+ }
77
+ if ( response ?. stderr ) {
78
+ client . desktopUI . toast . error (
79
+ "Failed to delete secret: " + JSON . stringify ( response )
80
+ ) ;
81
+ throw new Error ( "Failed to delete secret: " + JSON . stringify ( response ) ) ;
82
+ }
83
+ } catch ( error ) {
84
+ if ( ( error as any ) . stderr ) {
85
+ client . desktopUI . toast . error (
86
+ "Failed to delete secret: " + JSON . stringify ( error )
87
+ ) ;
88
+ } else {
89
+ client . desktopUI . toast . error ( "Failed to delete secret: " + error ) ;
90
+ }
62
91
}
92
+ }
63
93
64
- // Whether or not each secret has been assigned for a given catalog item
65
- export function getSecretsWithAssignment ( catalogItem : CatalogItemWithName , secrets : Secret [ ] ) : { name : string , assigned : boolean } [ ] {
66
- return catalogItem . secrets ?. map ( ( secret ) => ( { name : secret . name , assigned : secrets . some ( ( s ) => s . name === secret . name ) } ) ) || [ ] ;
67
- }
94
+ // Get all relevant secrets for a given set of catalog items
95
+ export function getAllSecretNames (
96
+ catalogItems : CatalogItemWithName [ ]
97
+ ) : string [ ] {
98
+ return catalogItems
99
+ . map ( ( item ) => item . secrets || [ ] )
100
+ . flat ( )
101
+ . map ( ( secret ) => secret . name ) ;
102
+ }
103
+
104
+ // Whether or not each secret has been assigned for a given catalog item
105
+ export function getSecretsWithAssignment (
106
+ catalogItem : CatalogItemWithName ,
107
+ secrets : Secret [ ]
108
+ ) : { name : string ; assigned : boolean } [ ] {
109
+ return (
110
+ catalogItem . secrets ?. map ( ( secret ) => ( {
111
+ name : secret . name ,
112
+ assigned : secrets . some ( ( s ) => s . name === secret . name ) ,
113
+ } ) ) || [ ]
114
+ ) ;
115
+ }
68
116
}
69
117
70
- export default Secrets ;
118
+ export default Secrets ;
0 commit comments