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
212 stars 105 forks source link

feat(cli): Add functionality to operate on Variables #514

Closed anudeeps352 closed 3 weeks ago

anudeeps352 commented 4 weeks ago

User description

Description

Add functionality to operate on Variables

Fixes #301

Developer's checklist

If changes are made in the code:

Documentation Update


PR Type

Enhancement


Description


Changes walkthrough πŸ“

Relevant files
Enhancement
variable.command.ts
Add `VariableCommand` class for CLI variable operations   

apps/cli/src/commands/variable.command.ts
  • Introduced VariableCommand class extending BaseCommand.
  • Added subcommands for variable management.
  • Provides descriptions and names for the command.
  • +28/-0   
    create.variable.ts
    Implement `CreateVariable` command for CLI                             

    apps/cli/src/commands/variable/create.variable.ts
  • Implemented CreateVariable class for creating variables.
  • Defined command arguments and options.
  • Added action method to handle variable creation.
  • +119/-0 
    delete.variable.ts
    Implement `DeleteVariable` command for CLI                             

    apps/cli/src/commands/variable/delete.variable.ts
  • Implemented DeleteVariable class for deleting variables.
  • Defined command arguments.
  • Added action method to handle variable deletion.
  • +44/-0   
    list.variable.ts
    Implement `ListVariable` command for CLI                                 

    apps/cli/src/commands/variable/list.variable.ts
  • Implemented ListVariable class for listing variables.
  • Defined command arguments.
  • Added action method to fetch and display variables.
  • +50/-0   
    revisions.variable.ts
    Implement `FetchVariableRevisions` command for CLI             

    apps/cli/src/commands/variable/revisions.variable.ts
  • Implemented FetchVariableRevisions class for fetching variable
    revisions.
  • Defined command arguments.
  • Added action method to retrieve and display revisions.
  • +63/-0   
    rollback.variable.ts
    Implement `RollbackVariable` command for CLI                         

    apps/cli/src/commands/variable/rollback.variable.ts
  • Implemented RollbackVariable class for rolling back variables.
  • Defined command arguments and options.
  • Added action method to perform rollback operation.
  • +78/-0   
    update.variable.ts
    Implement `UpdateVariable` command for CLI                             

    apps/cli/src/commands/variable/update.variable.ts
  • Implemented UpdateVariable class for updating variables.
  • Defined command arguments and options.
  • Added action method to handle variable updates.
  • +69/-0   
    index.ts
    Integrate `VariableCommand` into CLI application                 

    apps/cli/src/index.ts
  • Imported VariableCommand into the CLI application.
  • Added VariableCommand to the list of available commands.
  • +3/-1     

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

    codiumai-pr-agent-free[bot] commented 4 weeks ago

    PR Reviewer Guide πŸ”

    Here are some key observations to aid the review process:

    **🎫 Ticket compliance analysis βœ…** **[301](https://github.com/keyshade-xyz/keyshade/issues/301) - Fully compliant** Fully compliant requirements: - List all variables under a project - Fetch all revisions of a variable - Create a variable - Update a variable - Rollback a variable - Delete a variable - Create an implementation of `command.interface.ts` and name it `VariableCommand` - Stash all the functions in `src/commands/variable` - Use the `variableController` from `ControllerInstance` to make the API calls
    ⏱️ Estimated effort to review: 4 πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺ
    πŸ§ͺ No relevant tests
    πŸ”’ No security concerns identified
    ⚑ Recommended focus areas for review

    Error Handling
    The error handling for the 'entries' option could be improved. Currently, it throws an error if entries are not provided, which might not be the best user experience. Type Safety
    The use of 'any' type when iterating over variables could lead to potential type-related issues. Consider using a more specific type. Code Duplication
    The logging of revision details is verbose and could be refactored into a separate method to improve readability and maintainability.
    codiumai-pr-agent-free[bot] commented 4 weeks ago

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Implement input validation and type checking for the update options to enhance data integrity and prevent runtime errors ___ **The options object is passed directly to the updateVariable method without any
    validation or type checking. Consider adding input validation and type checking
    before passing the options to ensure data integrity and prevent potential runtime
    errors.** [apps/cli/src/commands/variable/update.variable.ts [51-58]](https://github.com/keyshade-xyz/keyshade/pull/514/files#diff-e081dceac29d37193bb60782e1cbfffc930128f4f0d752af0946baac3256e776R51-R58) ```diff +interface UpdateVariableOptions { + name?: string; + note?: string; + entries?: string[]; +} + +const validatedOptions: UpdateVariableOptions = {}; +if (typeof options.name === 'string') validatedOptions.name = options.name; +if (typeof options.note === 'string') validatedOptions.note = options.note; +if (Array.isArray(options.entries)) validatedOptions.entries = options.entries; + const { data, error, success } = await ControllerInstance.getInstance().variableController.updateVariable( { variableSlug, - ...options + ...validatedOptions }, this.headers ) ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 9 Why: Adding input validation and type checking for the update options is crucial for ensuring data integrity and preventing runtime errors, making this a high-impact improvement.
    9
    Enhancement
    βœ… Improve the robustness and error handling of the entries parsing logic ___ **Consider using a more robust method for parsing the entries input. The current
    implementation assumes a specific format and may fail for more complex inputs.
    Consider using a library like 'yargs' for parsing command-line arguments or
    implement a more flexible parsing logic.** [apps/cli/src/commands/variable/create.variable.ts [101-111]](https://github.com/keyshade-xyz/keyshade/pull/514/files#diff-bc3afb04230c3c629f17dbd1fc5f734e65134bf61ad18ea26d65998b948b9ff7R101-R111) ```diff const parsedEntries = entries.map((entry) => { - const entryObj: { value: string; environmentSlug: string } = { - value: '', - environmentSlug: '' + const [environmentSlug, value] = entry.split('=').map(s => s.trim()) + if (!environmentSlug || !value) { + throw new Error(`Invalid entry format: ${entry}. Expected format: "environmentSlug=value"`) } - entry.split(' ').forEach((pair) => { - const [key, value] = pair.split('=') - entryObj[key] = value - }) - return entryObj + return { environmentSlug, value } }) ``` `[Suggestion has been applied]`
    Suggestion importance[1-10]: 8 Why: The suggestion enhances the robustness of the entries parsing logic by introducing error handling for invalid formats, which can prevent potential runtime errors and improve the user experience.
    8
    Enhance type safety and readability by defining a Revision interface and using template literals ___ **Similar to the previous suggestion, consider defining an interface for the revision
    structure to improve type safety and code readability. Also, use template literals
    for better string formatting.** [apps/cli/src/commands/variable/revisions.variable.ts [47-55]](https://github.com/keyshade-xyz/keyshade/pull/514/files#diff-0418e51a07f5eb276c22c750a6ff91907cc2578e7225282cc8d7df32b8a64a18R47-R55) ```diff -data.items.forEach((revision: any) => { - Logger.info(`Id ${revision.id}`) - Logger.info(`value ${revision.value}`) - Logger.info(`version ${revision.version}`) - Logger.info(`variableID ${revision.variableId}`) - Logger.info(`Created On ${revision.createdOn}`) - Logger.info(`Created By Id ${revision.createdById}`) - Logger.info(`environmentId ${revision.environmentId}`) +interface Revision { + id: string; + value: string; + version: string; + variableId: string; + createdOn: string; + createdById: string; + environmentId: string; +} + +data.items.forEach((revision: Revision) => { + Logger.info(`Id: ${revision.id}`) + Logger.info(`Value: ${revision.value}`) + Logger.info(`Version: ${revision.version}`) + Logger.info(`Variable ID: ${revision.variableId}`) + Logger.info(`Created On: ${revision.createdOn}`) + Logger.info(`Created By ID: ${revision.createdById}`) + Logger.info(`Environment ID: ${revision.environmentId}`) }) ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 7 Why: The suggestion improves type safety and readability by defining a Revision interface and using template literals, which can make the code more maintainable and less error-prone.
    7
    Best practice
    Improve type safety by explicitly defining the structure of the variable object ___ **The variable type in the forEach loop is implicitly any. Consider defining an
    interface for the variable structure and use it to type the variable parameter,
    improving type safety and code readability.** [apps/cli/src/commands/variable/list.variable.ts [40-42]](https://github.com/keyshade-xyz/keyshade/pull/514/files#diff-10b95c97a99af739c47041522cb44cdc5bb784e26faa42731ea78b27e894c9c8R40-R42) ```diff -data.forEach((variable: any) => { +interface Variable { + name: string; + value: string; +} + +data.forEach((variable: Variable) => { Logger.info(`- ${variable.name} (${variable.value})`) }) ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 7 Why: Defining an interface for the variable structure enhances type safety and code readability, which is beneficial for maintaining and understanding the code.
    7

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

    rajdip-b commented 2 weeks ago

    :tada: This PR is included in version 2.7.0 :tada:

    The release is available on GitHub release

    Your semantic-release bot :package::rocket: