keyshade-xyz / keyshade

Realtime secret and configuration management tool, with the best in class security and seamless integration support
https://keyshade.xyz
Mozilla Public License 2.0
196 stars 96 forks source link

feat(cli): implement commands to get, list, update, and delete, workspace roles #469

Closed Meeran-Tofiq closed 5 hours ago

Meeran-Tofiq commented 2 days ago

User description

Description

Give a summary of the change that you have made
I have made 4 commands that are sub-commands of role. the commands are get, list, update, and delete. Update has the ability to change name, description, color code, authorities, and projects.

Fixes #447

Dependencies

Mention any dependencies/packages used N/A

Future Improvements

Mention any improvements to be done in future related to any file/feature The authorities and projects are updated poorly. If a role has a few authorities already, and you need to add a new one, you have to give the command the entire list of all authorities you already have + the authorities you need. Same for projects. This is tedious, arduous, and annoying. If possible and applicable, in the future there should be another sub-command for project and for authorities.

Mentions

Mention and tag the people

Screenshots of relevant screens

Add screenshots of relevant screens

Screenshot from 2024-09-28 15-34-13 Screenshot from 2024-09-28 15-34-22 Screenshot from 2024-09-28 15-34-55 Screenshot from 2024-09-29 14-48-34 Screenshot from 2024-09-28 15-35-56 Screenshot from 2024-09-29 14-40-20

Developer's checklist

If changes are made in the code:

Documentation Update


PR Type

enhancement, documentation


Description


Changes walkthrough πŸ“

Relevant files
Enhancement
workspace.command.ts
Integrate Workspace Role Command into Workspace Commands 

apps/cli/src/commands/workspace.command.ts
  • Imported WorkspaceRoleCommand into workspace sub-commands.
  • Added WorkspaceRoleCommand to the list of sub-commands.
  • +3/-1     
    role.workspace.ts
    Implement Workspace Role Command with Sub-commands             

    apps/cli/src/commands/workspace/role.workspace.ts
  • Created WorkspaceRoleCommand class.
  • Defined sub-commands for managing workspace roles.
  • +24/-0   
    delete.role.ts
    Add Delete Role Command for Workspace Roles                           

    apps/cli/src/commands/workspace/role/delete.role.ts
  • Implemented DeleteRoleCommand class.
  • Added functionality to delete a workspace role.
  • +44/-0   
    get.role.ts
    Add Get Role Command for Workspace Roles                                 

    apps/cli/src/commands/workspace/role/get.role.ts
  • Implemented GetRoleCommand class.
  • Added functionality to fetch a workspace role.
  • +56/-0   
    list.role.ts
    Add List Role Command for Workspace Roles                               

    apps/cli/src/commands/workspace/role/list.role.ts
  • Implemented ListRoleCommand class.
  • Added functionality to list all roles of a workspace.
  • +54/-0   
    update.role.ts
    Add Update Role Command for Workspace Roles                           

    apps/cli/src/commands/workspace/role/update.role.ts
  • Implemented UpdateRoleCommand class.
  • Added functionality to update a workspace role.
  • +97/-0   

    πŸ’‘ PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    codiumai-pr-agent-free[bot] commented 2 days ago

    PR Reviewer Guide πŸ”

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 πŸ”΅πŸ”΅πŸ”΅βšͺβšͺ
    πŸ§ͺ No relevant tests
    πŸ”’ No security concerns identified
    ⚑ Key issues to review

    Input Validation
    The `update.role.ts` file lacks input validation for the `colorCode` option. It should be validated to ensure it's a valid hex color code. Error Handling
    The `update.role.ts` file doesn't handle the case where `authorities` or `projectSlugs` options are provided but empty. This could lead to unexpected behavior.
    codiumai-pr-agent-free[bot] commented 2 days ago

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Input validation
    Add input validation for the color code option to ensure it's a valid hexadecimal color code ___ **Consider adding input validation for the color code option. Ensure that the provided
    color code is a valid hexadecimal color code before sending the request to the
    server.** [apps/cli/src/commands/workspace/role/update.role.ts [58-76]](https://github.com/keyshade-xyz/keyshade/pull/469/files#diff-400a288034de523bf4d15fe2dacde50dd425e9cec7e53f64730f095a98f81cf4R58-R76) ```diff async action({ args, options }: CommandActionData): Promise { const [workspaceRoleSlug] = args const { name, description, colorCode, authorities, projectSlugs } = options const authoritiesArray = authorities?.split(',') const projectSlugsArray = projectSlugs?.split(',') + + if (colorCode && !/^#[0-9A-Fa-f]{6}$/.test(colorCode)) { + Logger.error('Invalid color code. Please provide a valid hexadecimal color code (e.g., #FF0000).') + return + } const { data, error, success } = await ControllerInstance.getInstance().workspaceRoleController.updateWorkspaceRole( { workspaceRoleSlug, name, description, colorCode, authorities: authoritiesArray, projectSlugs: projectSlugsArray }, this.headers ) ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 9 Why: Input validation is crucial for preventing errors and ensuring data integrity, making this a high-impact improvement.
    9
    Enhancement
    Add a confirmation prompt before deleting a workspace role to prevent accidental deletions ___ **Consider adding a confirmation prompt before deleting the workspace role. This can
    prevent accidental deletions and improve the user experience. You can use a library
    like inquirer to implement this.** [apps/cli/src/commands/workspace/role/delete.role.ts [27-43]](https://github.com/keyshade-xyz/keyshade/pull/469/files#diff-afb508962ea8e6f708531815448ef09c512e680b5f02cec5ebdcbb2e391c0793R27-R43) ```diff async action({ args }: CommandActionData): Promise { const [workspaceRoleSlug] = args + + const confirm = await inquirer.prompt([ + { + type: 'confirm', + name: 'proceed', + message: `Are you sure you want to delete the workspace role "${workspaceRoleSlug}"?`, + default: false + } + ]) + + if (!confirm.proceed) { + Logger.info('Operation cancelled.') + return + } const { error, success } = await ControllerInstance.getInstance().workspaceRoleController.deleteWorkspaceRole( { workspaceRoleSlug }, this.headers ) if (success) { Logger.info(`Workspace role ${workspaceRoleSlug} deleted successfully!`) } else { Logger.error(`Failed deleting workspace role: ${error.message}`) } } ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 8 Why: Adding a confirmation prompt before deletion is a significant improvement for preventing accidental data loss, enhancing user experience and safety.
    8
    Add an option to output the role information in JSON format for easier integration with other tools ___ **Consider adding an option to output the role information in a machine-readable
    format like JSON. This would make it easier for users to integrate the command
    output with other tools or scripts.** [apps/cli/src/commands/workspace/role/get.role.ts [27-55]](https://github.com/keyshade-xyz/keyshade/pull/469/files#diff-2ab8e0f820820b1d3971936ef96de35ba6f64c396176fb6bc80b134d38209adeR27-R55) ```diff -async action({ args }: CommandActionData): Promise { +async action({ args, options }: CommandActionData): Promise { const [workspaceRoleSlug] = args + const { json } = options const { data, error, success } = await ControllerInstance.getInstance().workspaceRoleController.getWorkspaceRole( { workspaceRoleSlug }, this.headers ) if (success) { - Logger.info(`Workspace role fetched successfully!`) - Logger.info(`Workspace role: ${data.name} (${data.slug})`) - Logger.info(`Description: ${data.description || 'N/A'}`) - Logger.info(`Created at ${data.createdAt}`) - Logger.info(`Updated at ${data.updatedAt}`) - Logger.info(`Authorities:`) - for (const authority of data.authorities) { - Logger.info(`- ${authority}`) - } - Logger.info(`Projects:`) - for (const project of data.projects) { - Logger.info(`- ${project.project.name} (${project.project.slug})`) + if (json) { + console.log(JSON.stringify(data, null, 2)) + } else { + Logger.info(`Workspace role fetched successfully!`) + Logger.info(`Workspace role: ${data.name} (${data.slug})`) + Logger.info(`Description: ${data.description || 'N/A'}`) + Logger.info(`Created at ${data.createdAt}`) + Logger.info(`Updated at ${data.updatedAt}`) + Logger.info(`Authorities:`) + for (const authority of data.authorities) { + Logger.info(`- ${authority}`) + } + Logger.info(`Projects:`) + for (const project of data.projects) { + Logger.info(`- ${project.project.name} (${project.project.slug})`) + } } } else { Logger.error(`Failed fetching workspace role: ${error.message}`) } } ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 6 Why: Providing output in JSON format enhances the command's versatility and integration capabilities, though it is not critical for functionality.
    6
    Performance
    βœ… Implement pagination for the list command to handle large numbers of roles efficiently ___
    Suggestion Impact:The commit added pagination support by introducing a pagination option to the command, which aligns with the suggestion to handle large numbers of roles efficiently. code diff: ```diff + getOptions(): CommandOption[] { + return PAGINATION_OPTION + } + + async action({ args, options }: CommandActionData): Promise { Logger.info("Fetching workspace's roles...") const [workspaceSlug] = args @@ -32,7 +38,8 @@ const { data, error, success } = await ControllerInstance.getInstance().workspaceRoleController.getWorkspaceRolesOfWorkspace( { - workspaceSlug + workspaceSlug, + ...options ```
    ___ **Consider adding pagination support for the list command. This will improve
    performance and user experience when dealing with a large number of roles.** [apps/cli/src/commands/workspace/role/list.role.ts [27-53]](https://github.com/keyshade-xyz/keyshade/pull/469/files#diff-67a18ea572fcd5b275ddf309bf9f401a81a8c2133c17a80483ce617cf336c552R27-R53) ```diff async action({ args }: CommandActionData): Promise { Logger.info("Fetching workspace's roles...") const [workspaceSlug] = args + let page = 1 + const pageSize = 10 - const { data, error, success } = - await ControllerInstance.getInstance().workspaceRoleController.getWorkspaceRolesOfWorkspace( - { - workspaceSlug - }, - this.headers - ) + while (true) { + const { data, error, success } = + await ControllerInstance.getInstance().workspaceRoleController.getWorkspaceRolesOfWorkspace( + { + workspaceSlug, + page, + pageSize + }, + this.headers + ) - if (success) { - Logger.info('Workspace Roles fetched successfully:') - const roles = data.items - if (roles.length > 0) { - roles.forEach((role: any) => { - Logger.info(`- ${role.name} (${role.slug})`) - }) + if (success) { + const roles = data.items + if (roles.length > 0) { + Logger.info(`Page ${page}:`) + roles.forEach((role: any) => { + Logger.info(`- ${role.name} (${role.slug})`) + }) + if (roles.length < pageSize) { + break + } + page++ + } else { + if (page === 1) { + Logger.info('No roles found') + } + break + } } else { - Logger.info('No roles found') + Logger.error(`Failed fetching roles: ${error.message}`) + break } - } else { - Logger.error(`Failed fetching roles: ${error.message}`) } } ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 7 Why: Pagination is a useful feature for handling large datasets, improving performance and user experience by preventing overwhelming output.
    7

    πŸ’‘ Need additional feedback ? start a PR chat