-
Notifications
You must be signed in to change notification settings - Fork 463
/
viewPolicyVersion.ts
47 lines (39 loc) · 1.75 KB
/
viewPolicyVersion.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*!
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import * as vscode from 'vscode'
import { localize } from '../../shared/utilities/vsCodeUtils'
import { getLogger } from '../../shared/logger'
import { getTabSizeSetting } from '../../shared/utilities/editorUtilities'
import { IotPolicyVersionNode } from '../explorer/iotPolicyVersionNode'
import { showViewLogsMessage } from '../../shared/utilities/messages'
export async function viewPolicyVersionCommand(node: IotPolicyVersionNode) {
getLogger().debug('viewPolicyVersion called for %O', node)
const policyVersionId = node.version.versionId!
const policyName = node.policy.name
try {
const policy = await node.iot.getPolicyVersion({ policyName, policyVersionId })
const document = policy.policyDocument!
await showPolicyContent(document)
} catch (e) {
getLogger().error('Failed to retrieve policy document')
showViewLogsMessage(localize('AWS.iot.viewPolicyVersion.error', 'Failed to retrieve policy document'))
return undefined
}
}
export function policyFormatter(rawPolicyContent: string, tabSize: number = getTabSizeSetting()): string {
const prettyPolicyContent = JSON.stringify(JSON.parse(rawPolicyContent), undefined, tabSize)
return prettyPolicyContent
}
export async function showPolicyContent(
rawPolicyContent: string,
tabSize: number = getTabSizeSetting()
): Promise<void> {
const prettyPolicyContent = policyFormatter(rawPolicyContent, tabSize)
const newDoc = await vscode.workspace.openTextDocument({
language: 'json',
content: prettyPolicyContent,
})
await vscode.window.showTextDocument(newDoc, vscode.ViewColumn.One, false)
}