From b2a591c740ec251737594c27645da36300282931 Mon Sep 17 00:00:00 2001 From: daveads Date: Sun, 5 Jan 2025 10:42:37 +0100 Subject: [PATCH] format --- .../export/generators/ResourceGenerator.ts | 142 +++++++++--------- .../env/export/generators/UserSetGenerator.ts | 85 ++++++----- 2 files changed, 114 insertions(+), 113 deletions(-) diff --git a/source/commands/env/export/generators/ResourceGenerator.ts b/source/commands/env/export/generators/ResourceGenerator.ts index 3d5372c..2c4bf54 100644 --- a/source/commands/env/export/generators/ResourceGenerator.ts +++ b/source/commands/env/export/generators/ResourceGenerator.ts @@ -10,96 +10,98 @@ const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); interface ResourceData { - key: string; - name: string; - description?: string; - urn?: string; - actions: Record; - attributes?: Record; + key: string; + name: string; + description?: string; + urn?: string; + actions: Record; + attributes?: Record; } interface ActionData { - name: string; - description?: string; + name: string; + description?: string; } // Define a type for attributes interface AttributeData { - type: string; - required?: boolean; + type: string; + required?: boolean; } interface ActionBlockRead { - name?: string; - description?: string; + name?: string; + description?: string; } interface AttributeBlockRead { - type?: string; - required?: boolean; + type?: string; + required?: boolean; } export class ResourceGenerator implements HCLGenerator { - name = 'resources'; - private template: TemplateDelegate<{ resources: ResourceData[] }>; + name = 'resources'; + private template: TemplateDelegate<{ resources: ResourceData[] }>; - constructor( - private permit: Permit, - private warningCollector: WarningCollector, - ) { - this.template = Handlebars.compile( - readFileSync(join(__dirname, '../templates/resource.hcl'), 'utf-8'), - ); - } + constructor( + private permit: Permit, + private warningCollector: WarningCollector, + ) { + this.template = Handlebars.compile( + readFileSync(join(__dirname, '../templates/resource.hcl'), 'utf-8'), + ); + } - async generateHCL(): Promise { - try { - const resources = await this.permit.api.resources.list(); - const validResources = resources - .filter((resource) => resource.key !== '__user') - .map((resource) => ({ - key: createSafeId(resource.key), - name: resource.name, - description: resource.description, - urn: resource.urn, - actions: this.transformActions(resource.actions || {}), - attributes: this.transformAttributes(resource.attributes), - })); + async generateHCL(): Promise { + try { + const resources = await this.permit.api.resources.list(); + const validResources = resources + .filter(resource => resource.key !== '__user') + .map(resource => ({ + key: createSafeId(resource.key), + name: resource.name, + description: resource.description, + urn: resource.urn, + actions: this.transformActions(resource.actions || {}), + attributes: this.transformAttributes(resource.attributes), + })); - if (validResources.length === 0) return ''; + if (validResources.length === 0) return ''; - return '\n# Resources\n' + this.template({ resources: validResources }); - } catch (error) { - this.warningCollector.addWarning(`Failed to export resources: ${error}`); - return ''; - } - } + return '\n# Resources\n' + this.template({ resources: validResources }); + } catch (error) { + this.warningCollector.addWarning(`Failed to export resources: ${error}`); + return ''; + } + } - // Helper function to transform actions - private transformActions(actions: Record): Record { - const transformedActions: Record = {}; - for (const [key, action] of Object.entries(actions)) { - transformedActions[key] = { - name: action.name || key, - description: action.description, - }; - } - return transformedActions; - } + // Helper function to transform actions + private transformActions( + actions: Record, + ): Record { + const transformedActions: Record = {}; + for (const [key, action] of Object.entries(actions)) { + transformedActions[key] = { + name: action.name || key, + description: action.description, + }; + } + return transformedActions; + } - // Helper function to transform attributes - private transformAttributes( - attributes: Record | undefined, - ): Record | undefined { - if (!attributes) return undefined; + // Helper function to transform attributes + private transformAttributes( + attributes: Record | undefined, + ): Record | undefined { + if (!attributes) return undefined; - const transformedAttributes: Record = {}; - for (const [key, attribute] of Object.entries(attributes)) { - transformedAttributes[key] = { - type: attribute.type || 'string', - required: attribute.required || false, - }; - } - return transformedAttributes; - } -} \ No newline at end of file + const transformedAttributes: Record = {}; + for (const [key, attribute] of Object.entries(attributes)) { + transformedAttributes[key] = { + type: attribute.type || 'string', + required: attribute.required || false, + }; + } + return transformedAttributes; + } +} diff --git a/source/commands/env/export/generators/UserSetGenerator.ts b/source/commands/env/export/generators/UserSetGenerator.ts index 0721388..2ccbd82 100644 --- a/source/commands/env/export/generators/UserSetGenerator.ts +++ b/source/commands/env/export/generators/UserSetGenerator.ts @@ -9,51 +9,50 @@ import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); - interface UserSetData { - key: string; - name: string; - description?: string; - conditions: string; - resource: string; + key: string; + name: string; + description?: string; + conditions: string; + resource: string; } export class UserSetGenerator implements HCLGenerator { - name = 'user set'; - private template: TemplateDelegate<{ sets: UserSetData[] }>; - - constructor( - private permit: Permit, - private warningCollector: WarningCollector, - ) { - this.template = Handlebars.compile( - readFileSync(join(__dirname, '../templates/user-set.hcl'), 'utf-8'), - ); - } - - async generateHCL(): Promise { - try { - const conditionSets = await this.permit.api.conditionSets.list({}); - - const validSets = conditionSets - .filter((set) => set.type === 'userset') - .map((set) => ({ - key: createSafeId(set.key), - name: set.name, - description: set.description, - conditions: - typeof set.conditions === 'string' - ? set.conditions - : JSON.stringify(set.conditions), - resource: set.resource_id?.toString() || '', - })); - - if (validSets.length === 0) return ''; - - return '\n# User Sets\n' + this.template({ sets: validSets }); - } catch (error) { - this.warningCollector.addWarning(`Failed to export user sets: ${error}`); - return ''; - } - } + name = 'user set'; + private template: TemplateDelegate<{ sets: UserSetData[] }>; + + constructor( + private permit: Permit, + private warningCollector: WarningCollector, + ) { + this.template = Handlebars.compile( + readFileSync(join(__dirname, '../templates/user-set.hcl'), 'utf-8'), + ); + } + + async generateHCL(): Promise { + try { + const conditionSets = await this.permit.api.conditionSets.list({}); + + const validSets = conditionSets + .filter(set => set.type === 'userset') + .map(set => ({ + key: createSafeId(set.key), + name: set.name, + description: set.description, + conditions: + typeof set.conditions === 'string' + ? set.conditions + : JSON.stringify(set.conditions), + resource: set.resource_id?.toString() || '', + })); + + if (validSets.length === 0) return ''; + + return '\n# User Sets\n' + this.template({ sets: validSets }); + } catch (error) { + this.warningCollector.addWarning(`Failed to export user sets: ${error}`); + return ''; + } + } }