Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
daveads committed Jan 5, 2025
1 parent 39d159b commit b2a591c
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 113 deletions.
142 changes: 72 additions & 70 deletions source/commands/env/export/generators/ResourceGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ActionData>;
attributes?: Record<string, AttributeData>;
key: string;
name: string;
description?: string;
urn?: string;
actions: Record<string, ActionData>;
attributes?: Record<string, AttributeData>;
}

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<string> {
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<string> {
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<string, ActionBlockRead>): Record<string, ActionData> {
const transformedActions: Record<string, ActionData> = {};
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<string, ActionBlockRead>,
): Record<string, ActionData> {
const transformedActions: Record<string, ActionData> = {};
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<string, AttributeBlockRead> | undefined,
): Record<string, AttributeData> | undefined {
if (!attributes) return undefined;
// Helper function to transform attributes
private transformAttributes(
attributes: Record<string, AttributeBlockRead> | undefined,
): Record<string, AttributeData> | undefined {
if (!attributes) return undefined;

const transformedAttributes: Record<string, AttributeData> = {};
for (const [key, attribute] of Object.entries(attributes)) {
transformedAttributes[key] = {
type: attribute.type || 'string',
required: attribute.required || false,
};
}
return transformedAttributes;
}
}
const transformedAttributes: Record<string, AttributeData> = {};
for (const [key, attribute] of Object.entries(attributes)) {
transformedAttributes[key] = {
type: attribute.type || 'string',
required: attribute.required || false,
};
}
return transformedAttributes;
}
}
85 changes: 42 additions & 43 deletions source/commands/env/export/generators/UserSetGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
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<string> {
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 '';
}
}
}

0 comments on commit b2a591c

Please sign in to comment.