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

Ft/cli #3

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open

Ft/cli #3

wants to merge 10 commits into from

Conversation

hernaval
Copy link
Owner

@hernaval hernaval commented May 28, 2024

Create basic commandline (CLI) for managing password.

  • init
  • add
  • get
  • list

Cobra is the library used to setup CLI application.

Summary by CodeRabbit

  • New Features

    • Introduced add command to securely store passwords.
    • Added get command to retrieve and optionally copy passwords.
    • Introduced init command to set up password storage.
    • Added list command to display stored passwords.
  • Bug Fixes

    • Improved error handling for password retrieval and storage.
  • Documentation

    • Updated README with new features and future plans.
  • Chores

    • Updated .gitignore to exclude additional file types.

Copy link

coderabbitai bot commented May 28, 2024

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The updates introduce several new commands and functionalities to the password manager application, including adding, retrieving, listing, and initializing password storage. Enhancements were made to error handling, documentation, and file management. The .gitignore was also updated to exclude specific file types, and the project structure was refactored for better organization and future feature support.

Changes

File(s) Change Summary
.gitignore Excluded files with extensions .psm, .exe, and -manager.yaml.
README.md Updated to-do list, project structure, and documentation with future plans for multi-config support and authentication features.
cmd/add.go Introduced functionality to add a new password securely.
cmd/get.go Added get command to retrieve and optionally copy a stored password.
cmd/helper.go Added functions for initializing a password manager and checking initialization status.
cmd/init.go Introduced functionality to initialize password storage and create configuration files.
cmd/list.go Added list command to display all stored passwords with an optional --show flag.
cmd/root.go Defined the CLI application structure using Cobra.
encrypt/aes-256.go Introduced ErrAuthentication and updated Decrypt function for improved error handling.
feature/manage_password.go Added functions for managing passwords, including adding, retrieving, and listing passwords.
main.go Restructured import statements and main function to execute the CLI application.
storage/file_storage.go Updated error messages, file names for storage, and enhanced error handling for password search.
storage/storage_test.go Enhanced error handling in tests for retrieving passwords by name.
utils/file-utils.go Added utility functions for file operations, including finding files, creating files, and getting directory paths and separators.

Sequence Diagram(s) (Beta)

sequenceDiagram
    actor User
    participant CLI as Command-Line Interface
    participant InitCmd as Init Command
    participant AddCmd as Add Command
    participant GetCmd as Get Command
    participant ListCmd as List Command
    participant Storage as File Storage

    User ->> CLI: Execute Init Command
    CLI ->> InitCmd: Initialize Storage
    InitCmd ->> Storage: Create Config and Database Files
    Storage -->> InitCmd: Confirmation

    User ->> CLI: Execute Add Command
    CLI ->> AddCmd: Add New Password
    AddCmd ->> Storage: Store Encrypted Password
    Storage -->> AddCmd: Confirmation

    User ->> CLI: Execute Get Command
    CLI ->> GetCmd: Retrieve Password by Name
    GetCmd ->> Storage: Load Encrypted Password
    Storage -->> GetCmd: Encrypted Password
    GetCmd ->> GetCmd: Decrypt Password
    GetCmd -->> User: Display/Copy Password

    User ->> CLI: Execute List Command
    CLI ->> ListCmd: List All Stored Passwords
    ListCmd ->> Storage: Load All Passwords
    Storage -->> ListCmd: All Passwords
    ListCmd -->> User: Display Passwords
Loading

In a burrow deep and snug,
Where code and passwords quietly hug,
The rabbit typed with nimble paws,
Encrypting secrets, without a pause.
Commands to add, list, and get,
With safety nets and no regret.
In this warren of secure delight,
Your data's safe, day and night.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@hernaval
Copy link
Owner Author

@coderabbitai review

Copy link

coderabbitai bot commented May 28, 2024

Actions Performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

coderabbitai bot commented May 28, 2024

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

Commits Files that changed from the base of the PR and between 50ce71b and 2d5cde3.
Files selected for processing (15)
  • .gitignore (1 hunks)
  • README.md (1 hunks)
  • cmd/add.go (1 hunks)
  • cmd/get.go (1 hunks)
  • cmd/helper.go (1 hunks)
  • cmd/init.go (1 hunks)
  • cmd/list.go (1 hunks)
  • cmd/root.go (1 hunks)
  • encrypt/aes-256.go (2 hunks)
  • feature/manage_password.go (1 hunks)
  • main.go (1 hunks)
  • storage/file_storage.go (3 hunks)
  • storage/storage_test.go (1 hunks)
  • utils/file-utils.go (2 hunks)
  • utils/file_utils_test.go (1 hunks)
 _____________________________________________________
< Making Elsa proud by letting it go...to production. >
 -----------------------------------------------------
  \
   \   (\__/)
       (•ㅅ•)
       /   づ

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to full the review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 13

Outside diff range and nitpick comments (3)
README.md (2)

5-5: Consider changing "Todo" to "To-do" for grammatical correctness.


Line range hint 1-21: Add blank lines around headings for better readability and to adhere to Markdown best practices.

+ #
# pass-manager
This is a sample go app for password manager (encryption/decryption)

+ ##
## Todo
[ ] Beautify CLI and user input
[ ] Add more metadata for password data
[ ] Figlet 

+ ###
### ALPHA RELEASE
+ ###
### Refactoring 
[ ] Better error handling: user error not technical error

+ ###
### Documentation
[ ] Readme
[ ] How it works (with command explanation)
[ ] Encryption mechanism

+ ###
## Future
[ ] Multi config
[ ] Authentication and Session for Master password
[ ] Scan password (expiration, strength)
storage/file_storage.go (1)

Line range hint 19-64: Consider enhancing the error messages to provide more context about the errors, especially in the FindByName function.

- fmt.Println("storage error ", err)
+ fmt.Println("Storage error: ", err)
- return structs.PasswordData{}, fmt.Errorf("no password found with name %s", name)
+ return structs.PasswordData{}, fmt.Errorf("No password found with the specified name: %s", name)
Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 50ce71b and 2d5cde3.
Files selected for processing (15)
  • .gitignore (1 hunks)
  • README.md (1 hunks)
  • cmd/add.go (1 hunks)
  • cmd/get.go (1 hunks)
  • cmd/helper.go (1 hunks)
  • cmd/init.go (1 hunks)
  • cmd/list.go (1 hunks)
  • cmd/root.go (1 hunks)
  • encrypt/aes-256.go (2 hunks)
  • feature/manage_password.go (1 hunks)
  • main.go (1 hunks)
  • storage/file_storage.go (3 hunks)
  • storage/storage_test.go (1 hunks)
  • utils/file-utils.go (2 hunks)
  • utils/file_utils_test.go (1 hunks)
Files skipped from review due to trivial changes (1)
  • .gitignore
Additional Context Used
LanguageTool (1)
README.md (1)

Near line 4: It appears that a hyphen is missing in the noun “To-do” (= task) or did you mean the verb “to do”?
Context: ...ord manager (encryption/decryption) ## Todo [ ] Beautify CLI and user input [ ] Add...
Rule ID: TO_DO_HYPHEN

Markdownlint (9)
README.md (9)

7: Expected: 0 or 2; Actual: 1
Trailing spaces


10: Expected: 0 or 2; Actual: 1
Trailing spaces


1: Expected: 1; Actual: 0; Below
Headings should be surrounded by blank lines


4: Expected: 1; Actual: 0; Below
Headings should be surrounded by blank lines


9: Expected: 1; Actual: 0; Below
Headings should be surrounded by blank lines


10: Expected: 1; Actual: 0; Above
Headings should be surrounded by blank lines


10: Expected: 1; Actual: 0; Below
Headings should be surrounded by blank lines


13: Expected: 1; Actual: 0; Below
Headings should be surrounded by blank lines


18: Expected: 1; Actual: 0; Below
Headings should be surrounded by blank lines

Additional comments not posted (14)
main.go (1)

10-10: The cmd.Execute() function call is correctly placed in the main function to start the CLI application.

feature/manage_password.go (1)

10-10: Ensure error handling is consistent and informative across all functions.

Also applies to: 20-20, 35-35

cmd/add.go (1)

16-37: The addCmd command setup looks comprehensive and well-structured. Ensure that CheckInitialized and encrypt.EncKey are implemented correctly and handle all edge cases.

Verification successful

The CheckInitialized function in cmd/helper.go ensures that the password manager is initialized by checking a condition and exiting with an error message if the condition is not met. This implementation appears to handle the edge case where the password manager is not initialized.

The EncKey function in encrypt/aes-256.go generates a SHA-256 hash of the provided key, which is a standard and secure way to derive a key for encryption purposes.

Both functions seem to handle their respective tasks appropriately and include necessary error handling.

  • CheckInitialized ensures the password manager is initialized and exits with an error message if it is not.
  • EncKey securely generates a key using SHA-256.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the implementation of CheckInitialized and encrypt.EncKey.

# Test: Search for the function implementations in the repository. Expect: Proper error handling and edge case management.
ast-grep --lang go --pattern $'func CheckInitialized() { $$$ }'
ast-grep --lang go --pattern $'func EncKey($_) $_ { $$$ }'

Length of output: 581

cmd/get.go (2)

15-15: The declaration of the copy boolean variable is appropriate for managing the clipboard copy feature.


48-52: The initialization setup for getCmd is correctly implemented, including the addition of the command to the root and the setup of the copy flag.

cmd/list.go (2)

17-17: The declaration of the show boolean variable is appropriate for managing the visibility of passwords.


57-62: The initialization setup for listCmd is correctly implemented, including the addition of the command to the root and the setup of the show flag.

cmd/root.go (2)

14-16: The declarations of cfgFile and masterPassword are appropriate for managing configuration files and encryption mechanisms.


35-63: The initialization setup for reading configuration files and handling environment variables is correctly implemented.

encrypt/aes-256.go (2)

16-16: The introduction of ErrAuthentication is well-implemented and enhances error specificity.


87-87: Proper use of ErrAuthentication in the Decrypt function enhances clarity in error handling.

cmd/init.go (3)

22-22: The declaration of filename is appropriate for storing the configuration file name.


88-94: The implementation of createNewConfigFile is robust with appropriate error handling.


107-112: Proper setup of the initCmd with appropriate flags and registration.

case DoConfigFile:
createNewConfigFile()

case DoDatabasee:
Copy link

Choose a reason for hiding this comment

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

Typo in the case label DoDatabasee; should be DoDatabase.

- case DoDatabasee:
+ case DoDatabase:

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
case DoDatabasee:
case DoDatabase:

Comment on lines +96 to +105
func createEmptyDatabse() {
passData := structs.PasswordData{}
key := encrypt.EncKey([]byte(masterPassword))
contents, _ := utils.ToJson(passData)
err := utils.EncryptWrite(storageName, contents, key)
if err != nil {
fmt.Printf("Error init databse %s", err)
os.Exit(1)
}
}
Copy link

Choose a reason for hiding this comment

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

Typo in the function name createEmptyDatabse; should be createEmptyDatabase.

- func createEmptyDatabse() {
+ func createEmptyDatabase() {

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
func createEmptyDatabse() {
passData := structs.PasswordData{}
key := encrypt.EncKey([]byte(masterPassword))
contents, _ := utils.ToJson(passData)
err := utils.EncryptWrite(storageName, contents, key)
if err != nil {
fmt.Printf("Error init databse %s", err)
os.Exit(1)
}
}
func createEmptyDatabase() {
passData := structs.PasswordData{}
key := encrypt.EncKey([]byte(masterPassword))
contents, _ := utils.ToJson(passData)
err := utils.EncryptWrite(storageName, contents, key)
if err != nil {
fmt.Printf("Error init databse %s", err)
os.Exit(1)
}
}

import (
"fmt"
)
import "pass-manager/pass-manager/cmd"
Copy link

Choose a reason for hiding this comment

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

Tip

Codebase Verification

The cmd directory is located at the root of the repository, not under pass-manager/pass-manager/cmd. Therefore, the import path in main.go should be corrected to reflect this.

  • Update the import path in main.go to import "pass-manager/cmd".
Analysis chain

Ensure the import path is correct and follows Go conventions.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the import path for the cmd package.

# Test: Search for the package definition in the repository. Expect: Only one occurrence of the package definition.
rg --type go --files-with-matches $'package cmd'

Length of output: 121



Script:

#!/bin/bash
# Description: Verify the directory structure to ensure the import path is correct.

# List the directory structure to check if the path `pass-manager/pass-manager/cmd` exists.
find . -type d -path './pass-manager/pass-manager/cmd'

Length of output: 54



Script:

#!/bin/bash
# Description: Search for the `cmd` directory in the repository to identify its correct location.

# List all directories named `cmd` in the repository.
find . -type d -name 'cmd'

Length of output: 32


## Future
[ ]Multi config
Copy link

Choose a reason for hiding this comment

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

Ensure there is a space after the bullet point for consistency.

- [ ]Multi config
+ [ ] Multi config

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
[ ]Multi config
[ ] Multi config

func TestFindFilePattern(t *testing.T) {
pattern := "*.go"
dir := ".\\"
files, err := FindFilesMatch(pattern, dir)
Copy link

Choose a reason for hiding this comment

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

Ensure the directory path is platform-independent by using path/filepath instead of hardcoding Windows-style paths.

- dir := ".\\"
+ dir := filepath.Join(".", "")

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
files, err := FindFilesMatch(pattern, dir)
files, err := FindFilesMatch(pattern, filepath.Join(".", ""))

Comment on lines +19 to +55
// listCmd represents the list command
// TODO flag for showing specified field
var listCmd = &cobra.Command{
Use: "list",
Short: "List all password",
Long: `List all password and display all info about it, by default password text is hidden.
To show password use --show -s flag.
Example list --show
`,
PreRun: func(cmd *cobra.Command, args []string) {
CheckInitialized()
},
Run: func(cmd *cobra.Command, args []string) {
key := encrypt.EncKey([]byte(masterPassword))
data, err := feature.List(key)
if err != nil {
switch err {
case encrypt.ErrAuthentication:
fmt.Println("Unauthorized: missing or wrong password provided.")

default:
fmt.Printf("error loading password %s", err)
}
os.Exit(1)
}

fmt.Println("ID NAME PASSWORD")
for _, password := range data.Data {
textPas := password.Ciphertext
if !show {
textPas = strings.Repeat("*", 7)
}
fmt.Printf("%d ----------> %s -----------> %s", password.Id, password.Name, textPas)
fmt.Println()
}
},
}
Copy link

Choose a reason for hiding this comment

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

Consider enhancing the error message in the default case to provide more context about the error.

- fmt.Printf("error loading password %s", err)
+ fmt.Printf("Error loading password: %s", err)

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
// listCmd represents the list command
// TODO flag for showing specified field
var listCmd = &cobra.Command{
Use: "list",
Short: "List all password",
Long: `List all password and display all info about it, by default password text is hidden.
To show password use --show -s flag.
Example list --show
`,
PreRun: func(cmd *cobra.Command, args []string) {
CheckInitialized()
},
Run: func(cmd *cobra.Command, args []string) {
key := encrypt.EncKey([]byte(masterPassword))
data, err := feature.List(key)
if err != nil {
switch err {
case encrypt.ErrAuthentication:
fmt.Println("Unauthorized: missing or wrong password provided.")
default:
fmt.Printf("error loading password %s", err)
}
os.Exit(1)
}
fmt.Println("ID NAME PASSWORD")
for _, password := range data.Data {
textPas := password.Ciphertext
if !show {
textPas = strings.Repeat("*", 7)
}
fmt.Printf("%d ----------> %s -----------> %s", password.Id, password.Name, textPas)
fmt.Println()
}
},
}
// listCmd represents the list command
// TODO flag for showing specified field
var listCmd = &cobra.Command{
Use: "list",
Short: "List all password",
Long: `List all password and display all info about it, by default password text is hidden.
To show password use --show -s flag.
Example list --show
`,
PreRun: func(cmd *cobra.Command, args []string) {
CheckInitialized()
},
Run: func(cmd *cobra.Command, args []string) {
key := encrypt.EncKey([]byte(masterPassword))
data, err := feature.List(key)
if err != nil {
switch err {
case encrypt.ErrAuthentication:
fmt.Println("Unauthorized: missing or wrong password provided.")
default:
fmt.Printf("Error loading password: %s", err)
}
os.Exit(1)
}
fmt.Println("ID NAME PASSWORD")
for _, password := range data.Data {
textPas := password.Ciphertext
if !show {
textPas = strings.Repeat("*", 7)
}
fmt.Printf("%d ----------> %s -----------> %s", password.Id, password.Name, textPas)
fmt.Println()
}
},
}

// Initialization steps
const (
DoConfigFile Step = iota
DoDatabasee
Copy link

Choose a reason for hiding this comment

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

Correct the typo in the constant name DoDatabasee to DoDatabase.

- DoDatabasee
+ DoDatabase

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
DoDatabasee
DoDatabase

Comment on lines +40 to +76
func GetInitSteps() ([]Step, error) {
rootDir := utils.CurrentDir()
var results []Step
fileConfigs, err := utils.FindFilesMatch(fileConfigExt, rootDir)
if err != nil {
return nil, ErrLoadingFile
}
if len(fileConfigs) == 0 {
results = append(results, DoConfigFile)
}
// check file is well structured

// check if database is present
databaseFiles, err := utils.FindFilesMatch(databaseFilegExt, rootDir)
if err != nil {
return nil, ErrLoadingFile
}
if len(databaseFiles) == 0 {
results = append(results, DoDatabasee)
}
// load or create a config file with minimum template
return results, nil
}

func IsInitialized() bool {
steps, err := GetInitSteps()

return len(steps) == 0 && err == nil
}

func CheckInitialized() {
if !IsInitialized() {
fmt.Println("Password manager not initialized. Do it first!!!")
initCmd.Usage()
os.Exit(1)
}
}
Copy link

Choose a reason for hiding this comment

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

Enhance the error handling in GetInitSteps to provide more specific error messages based on the context of the failure.

- return nil, ErrLoadingFile
+ return nil, fmt.Errorf("failed to find configuration files: %w", err)

Committable suggestion was skipped due low confidence.

Comment on lines +17 to +26
var rootCmd = &cobra.Command{
Use: "pass-manager",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
}
Copy link

Choose a reason for hiding this comment

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

Consider revising the descriptions in rootCmd to more accurately reflect the specific functionalities of the password manager rather than generic Cobra application information.

- Short: "A brief description of your application"
- Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`
+ Short: "Manage your passwords securely"
+ Long: `Pass-manager is a command-line tool that helps you securely store, retrieve, and manage your passwords. It uses robust encryption to ensure your data is safe.`

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
var rootCmd = &cobra.Command{
Use: "pass-manager",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
}
var rootCmd = &cobra.Command{
Use: "pass-manager",
Short: "Manage your passwords securely",
Long: `Pass-manager is a command-line tool that helps you securely store, retrieve, and manage your passwords. It uses robust encryption to ensure your data is safe.`,
}

Comment on lines +56 to +80

// Get a root path as input
// Loop the children of the root
// Find all file matching the pattern (use shell based file pattern)
func FindFilesMatch(pattern string, root string) ([]string, error) {
var files []string
err := filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if matched, err := filepath.Match(pattern, filepath.Base(path)); err != nil {
return err
} else if matched {
files = append(files, path)
}
return nil
})
if err != nil {
return nil, err
}
return files, nil
}
Copy link

Choose a reason for hiding this comment

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

Enhance the error handling in FindFilesMatch to provide more specific error messages based on the context of the failure.

- return nil, err
+ return nil, fmt.Errorf("Failed to walk the directory: %w", err)

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
// Get a root path as input
// Loop the children of the root
// Find all file matching the pattern (use shell based file pattern)
func FindFilesMatch(pattern string, root string) ([]string, error) {
var files []string
err := filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if matched, err := filepath.Match(pattern, filepath.Base(path)); err != nil {
return err
} else if matched {
files = append(files, path)
}
return nil
})
if err != nil {
return nil, err
}
return files, nil
}
// Get a root path as input
// Loop the children of the root
// Find all file matching the pattern (use shell based file pattern)
func FindFilesMatch(pattern string, root string) ([]string, error) {
var files []string
err := filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if matched, err := filepath.Match(pattern, filepath.Base(path)); err != nil {
return err
} else if matched {
files = append(files, path)
}
return nil
})
if err != nil {
return nil, fmt.Errorf("Failed to walk the directory: %w", err)
}
return files, nil
}

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

Successfully merging this pull request may close these issues.

1 participant