diff --git a/CHANGELOG.md b/CHANGELOG.md index c93da12b..8648a432 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ # v3.0.0 - Unreleased +- Add support to `.env` like files + ([#324](https://github.com/go-task/task/issues/324), [#356](https://github.com/go-task/task/pull/356)). - Add `label:` to task so you can override the task name in the logs ([#321](https://github.com/go-task/task/issues/321]), [#337](https://github.com/go-task/task/pull/337)). diff --git a/docs/usage.md b/docs/usage.md index 609f9c71..4561fcf0 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -35,8 +35,8 @@ If you omit a task name, "default" will be assumed. ## Environment variables - ### Task + You can use `env` to set custom environment variables for a specific task: ```yaml @@ -68,29 +68,20 @@ tasks: > NOTE: `env` supports expansion and retrieving output from a shell command > just like variables, as you can see on the [Variables](#variables) section. +### .env files -### Operating System -Environment variables from the OS are accessible using `$VARNAME`: +You can also ask Task to include `.env` like files by using the `dotenv:` +setting: -```yaml -version: '2' - -tasks: - greet: - cmds: - - echo "Hello $USER" -``` - -### .env - -*.env* files are supported in v3 using the `dotenv` declaration: - -.env ``` +# .env KEYNAME=VALUE ``` -Taskfile.yml + + ```yaml +# Taskfile.yml + version: '3' dotenv: ['.env'] diff --git a/internal/taskfile/read/taskfile.go b/internal/taskfile/read/taskfile.go index 70bee710..edde4bcc 100644 --- a/internal/taskfile/read/taskfile.go +++ b/internal/taskfile/read/taskfile.go @@ -3,7 +3,6 @@ package read import ( "errors" "fmt" - "github.com/joho/godotenv" "os" "path/filepath" "runtime" @@ -11,12 +10,14 @@ import ( "github.com/go-task/task/v2/internal/taskfile" "github.com/go-task/task/v2/internal/templater" + "github.com/joho/godotenv" "gopkg.in/yaml.v3" ) var ( // ErrIncludedTaskfilesCantHaveIncludes is returned when a included Taskfile contains includes ErrIncludedTaskfilesCantHaveIncludes = errors.New("task: Included Taskfiles can't have includes. Please, move the include to the main Taskfile") + // ErrIncludedTaskfilesCantHaveDotenvs is returned when a included Taskfile contains dotenvs ErrIncludedTaskfilesCantHaveDotenvs = errors.New("task: Included Taskfiles can't have dotenv declarations. Please, move the dotenv declaration to the main Taskfile") ) @@ -36,17 +37,18 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) { return nil, err } - if v >= 3.0 { - if len(t.Dotenv) > 0 { - for _, envFile := range t.Dotenv { - var envFilePath string - if filepath.IsAbs(envFile) { - envFilePath = envFile - } else { - envFilePath = filepath.Join(dir, envFile) - } - if err = godotenv.Load(envFilePath); err != nil { - return nil, err + if v >= 3.0 && len(t.Dotenv) > 0 { + for _, dotEnvPath := range t.Dotenv { + if !filepath.IsAbs(dotEnvPath) { + dotEnvPath = filepath.Join(dir, dotEnvPath) + } + envs, err := godotenv.Read(dotEnvPath) + if err != nil { + return nil, err + } + for key, value := range envs { + if _, ok := t.Env.Mapping[key]; !ok { + t.Env.Set(key, taskfile.Var{Static: value}) } } } @@ -86,10 +88,8 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) { return nil, ErrIncludedTaskfilesCantHaveIncludes } - if v >= 3.0 { - if len(includedTaskfile.Dotenv) > 0 { - return nil, ErrIncludedTaskfilesCantHaveDotenvs - } + if v >= 3.0 && len(includedTaskfile.Dotenv) > 0 { + return nil, ErrIncludedTaskfilesCantHaveDotenvs } if includedTask.AdvancedImport {