Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions source/commands/env/export/generators/RoleGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class RoleGenerator implements HCLGenerator {
constructor(
private permit: Permit,
private warningCollector: WarningCollector,
private includeDefaultRoles: boolean = false,
) {
// Register Handlebars helpers
this.registerHandlebarsHelpers();
Expand Down Expand Up @@ -300,8 +301,11 @@ export class RoleGenerator implements HCLGenerator {
const validRoles: RoleData[] = [];

for (const role of roles) {
// Skip default global roles that already exist in the system
if (DEFAULT_GLOBAL_ROLES.includes(role.key)) {
// Skip default global roles that already exist in the system unless includeDefaultRoles is true
if (
DEFAULT_GLOBAL_ROLES.includes(role.key) &&
!this.includeDefaultRoles
) {
// Still add to the ID map for role derivations
this.roleIdMap.set(role.key, role.key);
continue;
Expand Down
11 changes: 11 additions & 0 deletions source/commands/env/export/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ export const options = zod.object({
alias: 'f',
}),
),
includeDefaultRoles: zod
.boolean()
.optional()
.default(false)
.describe(
option({
description:
'Include default roles (admin, editor, viewer) in the export',
alias: 'i',
}),
),
});

type Props = {
Expand Down
1 change: 1 addition & 0 deletions source/commands/env/export/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface ExportState {
export interface ExportOptions {
key?: string;
file?: string;
includeDefaultRoles?: boolean;
}

export interface HCLGenerator {
Expand Down
7 changes: 5 additions & 2 deletions source/components/export/ExportContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import fs from 'node:fs/promises';
import { Text } from 'ink';

export const ExportContent: FC<{ options: ExportOptions }> = ({
options: { key: apiKey, file },
options: { key: apiKey, file, includeDefaultRoles },
}) => {
const { validateApiKeyScope } = useApiKeyApi();
const { authToken } = useAuth();
const key = apiKey || authToken;
const { state, setState, exportConfig } = useExport(key);
const { state, setState, exportConfig } = useExport({
apiKey: key,
includeDefaultRoles,
});
const [hclOutput, setHclOutput] = useState<string | null>(null);
const hasRunRef = useRef(false);

Expand Down
22 changes: 19 additions & 3 deletions source/hooks/export/useExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,30 @@ type ExtendedRelationGenerator = Omit<RelationGenerator, 'getRelationIdMap'> & {
getRelationIdMap?: () => Map<string, string>;
};

export const useExport = (apiKey: string) => {
interface UseExportProps {
apiKey?: string;
includeDefaultRoles?: boolean;
}

export const useExport = ({
apiKey,
includeDefaultRoles,
}: UseExportProps = {}) => {
const [state, setState] = useState<ExportState>({
status: '',
isComplete: false,
error: null,
warnings: [],
});

const permit = usePermitSDK(apiKey);
const permit = apiKey ? usePermitSDK(apiKey) : null;

const exportConfig = async (scope: ExportScope) => {
try {
if (!permit) {
throw new Error('No API key provided. Please provide a key or login first.');
}
Comment on lines +74 to +76
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ocap-kirk - this logic is provided by the AuthProvider, and not needed to address here


const warningCollector = createWarningCollector();

let hcl = `# Generated by Permit CLI
Expand All @@ -73,7 +85,11 @@ ${generateProviderBlock()}`;

// Create all generators first
const resourceGenerator = new ResourceGenerator(permit, warningCollector);
const roleGenerator = new RoleGenerator(permit, warningCollector);
const roleGenerator = new RoleGenerator(
permit,
warningCollector,
includeDefaultRoles,
);
const userAttributesGenerator = new UserAttributesGenerator(
permit,
warningCollector,
Expand Down
Loading