4
4
*/
5
5
6
6
import * as vscode from 'vscode'
7
- import { join } from 'path'
8
- import { EnvironmentVariables } from '../shared/environmentVariables'
9
7
import { SystemUtilities } from '../shared/systemUtilities'
10
8
import { ToolkitError } from '../shared/errors'
11
9
import { assertHasProps } from '../shared/utilities/tsUtils'
12
-
13
- export function getCredentialsFilename ( ) : string {
14
- const env = process . env as EnvironmentVariables
15
-
16
- return env . AWS_SHARED_CREDENTIALS_FILE || join ( SystemUtilities . getHomeDirectory ( ) , '.aws' , 'credentials' )
17
- }
18
-
19
- export function getConfigFilename ( ) : string {
20
- const env = process . env as EnvironmentVariables
21
-
22
- return env . AWS_CONFIG_FILE || join ( SystemUtilities . getHomeDirectory ( ) , '.aws' , 'config' )
23
- }
10
+ import { getConfigFilename , getCredentialsFilename } from './sharedCredentialsFile'
24
11
25
12
export async function updateAwsSdkLoadConfigEnvVar ( ) : Promise < void > {
26
13
const configFileExists = await SystemUtilities . fileExists ( getConfigFilename ( ) )
@@ -35,7 +22,7 @@ interface AssignmentNode {
35
22
36
23
export interface BaseSection {
37
24
readonly type : string
38
- readonly name : string
25
+ readonly name : SectionName
39
26
readonly source : vscode . Uri
40
27
readonly startLines : number [ ]
41
28
readonly assignments : AssignmentNode [ ]
@@ -75,7 +62,7 @@ export class ParseError extends Error {
75
62
export const isProfileSection = ( section : Section ) : section is ProfileSection => section . type === 'profile'
76
63
export const isSsoSessionSection = ( section : Section ) : section is SsoSessionSection => section . type === 'sso-session'
77
64
78
- export function extractData ( section : BaseSection ) : Record < string , string > {
65
+ export function extractDataFromSection ( section : BaseSection ) : Record < string , string > {
79
66
const data : Record < string , string > = { }
80
67
for ( const assignment of section . assignments ) {
81
68
data [ assignment . key ] = assignment . value
@@ -86,7 +73,7 @@ export function extractData(section: BaseSection): Record<string, string> {
86
73
87
74
export function getRequiredFields < T extends string > ( section : Section , ...keys : T [ ] ) : { [ P in T ] : string } {
88
75
try {
89
- const data = extractData ( section )
76
+ const data = extractDataFromSection ( section )
90
77
assertHasProps ( data , ...keys )
91
78
92
79
return data
@@ -99,7 +86,7 @@ export function getRequiredFields<T extends string>(section: Section, ...keys: T
99
86
100
87
export function getSectionOrThrow < T extends Section [ 'type' ] = Section [ 'type' ] > (
101
88
sections : Section [ ] ,
102
- name : string ,
89
+ name : SectionName ,
103
90
type : T
104
91
) : Section & { type : T } {
105
92
const section = sections . find ( s => s . name === name && s . type === type ) as ( Section & { type : T } ) | undefined
@@ -111,13 +98,13 @@ export function getSectionOrThrow<T extends Section['type'] = Section['type']>(
111
98
return section
112
99
}
113
100
114
- export function getSectionDataOrThrow ( sections : Section [ ] , name : string , type : Section [ 'type' ] ) {
101
+ export function getSectionDataOrThrow ( sections : Section [ ] , name : SectionName , type : Section [ 'type' ] ) {
115
102
const section = getSectionOrThrow ( sections , name , type )
116
103
if ( section . type !== type ) {
117
104
throw ParseError . fromSection ( section , `Expected section to be type "${ type } ", got: ${ section . type } ` )
118
105
}
119
106
120
- return extractData ( section )
107
+ return extractDataFromSection ( section )
121
108
}
122
109
123
110
const sectionTypes = [ 'profile' , 'sso-session' ] as const
@@ -130,6 +117,19 @@ function validateSection(section: BaseSection): asserts section is Section {
130
117
}
131
118
}
132
119
120
+ /**
121
+ * Loads existing merged (credentials + config) profiles from the filesystem
122
+ */
123
+ export async function loadSharedCredentialsProfiles ( ) : Promise < Record < SectionName , Profile > > {
124
+ const profiles = { } as Record < SectionName , Profile >
125
+ for ( const [ k , v ] of ( await loadSharedCredentialsSections ( ) ) . sections . entries ( ) ) {
126
+ if ( v . type === 'profile' ) {
127
+ profiles [ k ] = extractDataFromSection ( v )
128
+ }
129
+ }
130
+ return profiles
131
+ }
132
+
133
133
export async function loadSharedCredentialsSections ( ) : Promise < ParseResult > {
134
134
// These should eventually be changed to use `parse` to allow for credentials from other file systems
135
135
const data = await loadSharedConfigFiles ( {
@@ -234,6 +234,13 @@ async function loadCredentialsFile(credentialsUri?: vscode.Uri): Promise<ReturnT
234
234
return parseIni ( await SystemUtilities . readFile ( credentialsUri ) , credentialsUri )
235
235
}
236
236
237
+ /**
238
+ * The name of a section in a credentials/config file
239
+ *
240
+ * The is the value of `{A}` in `[ {A} ]` or `[ {B} {A} ]`.
241
+ */
242
+ export type SectionName = string
243
+
237
244
export interface Profile {
238
245
[ key : string ] : string | undefined
239
246
}
0 commit comments