Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Oct 29, 2024

Conversation

anudeeps352
Copy link
Contributor

@anudeeps352 anudeeps352 commented Oct 28, 2024

User description

Description

Add functionality to operate on Variables

Fixes #301

Developer's checklist

  • My PR follows the style guidelines of this project
  • I have performed a self-check on my work

If changes are made in the code:

  • I have followed the coding guidelines
  • My changes in code generate no new warnings
  • My changes are breaking another fix/feature of the project
  • I have added test cases to show that my feature works
  • I have added relevant screenshots in my PR
  • There are no UI/UX issues

Documentation Update

  • This PR requires an update to the documentation at docs.keyshade.xyz
  • I have made the necessary updates to the documentation, or no documentation changes are required.

PR Type

Enhancement


Description

  • Added a new VariableCommand class to manage variables through the CLI, including subcommands for creating, deleting, listing, fetching revisions, rolling back, and updating variables.
  • Implemented individual command classes for each variable operation, defining necessary arguments and options.
  • Integrated the VariableCommand into the CLI application, making it available for use.

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

    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    🎫 Ticket compliance analysis ✅

    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.

    @anudeeps352 anudeeps352 changed the title fix(cli): Add functionality to operate on Variables feat(cli): Add functionality to operate on Variables Oct 28, 2024
    Copy link
    Contributor

    codiumai-pr-agent-free bot commented Oct 28, 2024

    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]

    +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]

     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]

    -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]

    -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

    Refactor code to improve error handling
    
    Co-authored-by: codiumai-pr-agent-free[bot] <138128286+codiumai-pr-agent-free[bot]@users.noreply.github.com>
    @rajdip-b rajdip-b merged commit 32d93e6 into keyshade-xyz:develop Oct 29, 2024
    4 checks passed
    rajdip-b pushed a commit that referenced this pull request Nov 5, 2024
    ## [2.7.0](v2.6.0...v2.7.0) (2024-11-05)
    
    ### 🚀 Features
    
    * **cli:** Add functionality to operate on Variables ([#514](#514)) ([32d93e6](32d93e6))
    * **platform:** Create ui link for resend otp ([#489](#489)) ([46eb5c5](46eb5c5))
    
    ### 🐛 Bug Fixes
    
    * **api,api-client:** Add environmentSlug in multiple places across the [secure] module ([#509](#509)) ([ee58f07](ee58f07))
    * **cli:** Removed unnecessary console log in [secure]s ([#515](#515)) ([9403cc4](9403cc4))
    
    ### 🔧 Miscellaneous Chores
    
    * Fixed lint issues ([835397a](835397a))
    * Minor housekeeping ([922bf31](922bf31))
    * Update eslint ([c583718](c583718))
    * Update eslint ([7c0c596](7c0c596))
    * Update pnpx commands to pnpm dlx ([#511](#511)) ([534a231](534a231))
    @rajdip-b
    Copy link
    Member

    rajdip-b commented Nov 5, 2024

    🎉 This PR is included in version 2.7.0 🎉

    The release is available on GitHub release

    Your semantic-release bot 📦🚀

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    CLI: Add functionality to operate on Variables
    2 participants