Skip to content

Conversation

@nxtcoder17
Copy link
Owner

@nxtcoder17 nxtcoder17 commented Oct 20, 2024

Closes #17

Summary by Sourcery

Add support for global dotenv files in task parsing to enable setting environment variables globally from specified dotenv files. Update tests to cover new functionality, ensuring correct behavior when dotenv files are present or missing.

New Features:

  • Add support for parsing global dotenv files in task parsing, allowing environment variables to be set globally from specified dotenv files.

Tests:

  • Introduce tests to verify the correct parsing of global dotenv files, including scenarios where the dotenv file is found and when it is missing.

@nxtcoder17 nxtcoder17 self-assigned this Oct 20, 2024
@sourcery-ai sourcery-ai bot changed the title @sourcery-ai Add support for global dotenv files in task parsing Oct 20, 2024
@sourcery-ai
Copy link

sourcery-ai bot commented Oct 20, 2024

Reviewer's Guide by Sourcery

This pull request adds support for global dotenv files in task parsing. It introduces the ability to load environment variables from specified .env files and applies them globally to all tasks in the Runfile.

Sequence diagram for parsing tasks with global dotenv support

sequenceDiagram
    participant User
    participant TaskParser
    participant DotEnvParser
    participant Runfile

    User->>TaskParser: ParseTask(ctx, rf, task)
    TaskParser->>Runfile: Check DotEnv
    alt DotEnv is not nil
        TaskParser->>DotEnvParser: parseDotEnvFiles(rf.DotEnv...)
        alt DotEnv file found
            DotEnvParser-->>TaskParser: Return env variables
            TaskParser->>TaskParser: Add env variables to globalEnv
        else DotEnv file not found
            DotEnvParser-->>TaskParser: Return error
            TaskParser-->>User: Return error
        end
    end
    TaskParser->>User: Return ParsedTask
Loading

Updated class diagram for Runfile and Task parsing

classDiagram
    class Runfile {
        +[]string DotEnv
        +map[string]Task Tasks
    }
    class Task {
        +bool ignoreSystemEnv
        +[]any Commands
    }
    class ParsedTask {
        +[]string Shell
        +string WorkingDir
        +map<string, string> Env
        +[]CommandJson Commands
    }
    class CommandJson {
        +string Command
    }
    class TaskParser {
        +ParseTask(Context ctx, Runfile rf, Task task) ParsedTask
    }
    class DotEnvParser {
        +parseDotEnvFiles(string... files) map<string, string>
    }
    Runfile --> Task
    TaskParser --> Runfile
    TaskParser --> DotEnvParser
    TaskParser --> ParsedTask
    ParsedTask --> CommandJson
Loading

File-Level Changes

Change Details Files
Implement support for global dotenv files in task parsing
  • Add logic to parse and load environment variables from dotenv files
  • Integrate parsed dotenv variables into the global environment
  • Add error handling for cases where specified dotenv files are not found
pkg/runfile/task-parser.go
pkg/runfile/task-parser_test.go
Add test cases for global dotenv functionality
  • Create test case for successful loading of environment variables from a dotenv file
  • Add test case to verify error handling when a specified dotenv file is not found
pkg/runfile/task-parser_test.go

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@nxtcoder17 nxtcoder17 changed the title Add support for global dotenv files in task parsing Add support for global (top-level) dotenv files Oct 20, 2024
@nxtcoder17 nxtcoder17 merged commit 8835533 into master Oct 20, 2024
@nxtcoder17 nxtcoder17 deleted the feat/global-dotenv branch October 20, 2024 04:32
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @nxtcoder17 - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.


if rf.DotEnv != nil {
m, err := parseDotEnvFiles(rf.DotEnv...)
if err != nil {
Copy link

Choose a reason for hiding this comment

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

suggestion: Add context to the returned error for better debugging.

When returning the error from parseDotEnvFiles, consider wrapping it with additional context using fmt.Errorf or a custom error type. This will make it easier to trace the source of the error during debugging.

Suggested change
if err != nil {
if err != nil {
return nil, fmt.Errorf("failed to parse .env files: %w", err)

}
}

if rf.DotEnv != nil {
Copy link

Choose a reason for hiding this comment

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

suggestion (performance): Add a check for empty rf.DotEnv slice to avoid unnecessary function calls.

Consider adding a check for len(rf.DotEnv) > 0 before calling parseDotEnvFiles to avoid unnecessary function calls when the slice is empty.

Suggested change
if rf.DotEnv != nil {
if rf.DotEnv != nil && len(rf.DotEnv) > 0 {

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.

[✨ FEAT] global dotenv files support

2 participants