1
0
mirror of https://github.com/go-task/task.git synced 2025-04-25 12:25:07 +02:00

Some improvements to #356

This commit is contained in:
Andrey Nering 2020-08-15 19:12:39 -03:00
parent 8b962fb8e8
commit eab14b6c49
3 changed files with 27 additions and 34 deletions

View File

@ -2,6 +2,8 @@
# v3.0.0 - Unreleased # 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 - 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)). ([#321](https://github.com/go-task/task/issues/321]), [#337](https://github.com/go-task/task/pull/337)).

View File

@ -35,8 +35,8 @@ If you omit a task name, "default" will be assumed.
## Environment variables ## Environment variables
### Task ### Task
You can use `env` to set custom environment variables for a specific task: You can use `env` to set custom environment variables for a specific task:
```yaml ```yaml
@ -68,29 +68,20 @@ tasks:
> NOTE: `env` supports expansion and retrieving output from a shell command > NOTE: `env` supports expansion and retrieving output from a shell command
> just like variables, as you can see on the [Variables](#variables) section. > just like variables, as you can see on the [Variables](#variables) section.
### .env files
### Operating System You can also ask Task to include `.env` like files by using the `dotenv:`
Environment variables from the OS are accessible using `$VARNAME`: 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 KEYNAME=VALUE
``` ```
Taskfile.yml
```yaml ```yaml
# Taskfile.yml
version: '3' version: '3'
dotenv: ['.env'] dotenv: ['.env']

View File

@ -3,7 +3,6 @@ package read
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/joho/godotenv"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -11,12 +10,14 @@ import (
"github.com/go-task/task/v2/internal/taskfile" "github.com/go-task/task/v2/internal/taskfile"
"github.com/go-task/task/v2/internal/templater" "github.com/go-task/task/v2/internal/templater"
"github.com/joho/godotenv"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
var ( var (
// ErrIncludedTaskfilesCantHaveIncludes is returned when a included Taskfile contains includes // 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") 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") ErrIncludedTaskfilesCantHaveDotenvs = errors.New("task: Included Taskfiles can't have dotenv declarations. Please, move the dotenv declaration to the main Taskfile")
) )
@ -36,18 +37,19 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) {
return nil, err return nil, err
} }
if v >= 3.0 { if v >= 3.0 && len(t.Dotenv) > 0 {
if len(t.Dotenv) > 0 { for _, dotEnvPath := range t.Dotenv {
for _, envFile := range t.Dotenv { if !filepath.IsAbs(dotEnvPath) {
var envFilePath string dotEnvPath = filepath.Join(dir, dotEnvPath)
if filepath.IsAbs(envFile) {
envFilePath = envFile
} else {
envFilePath = filepath.Join(dir, envFile)
} }
if err = godotenv.Load(envFilePath); err != nil { envs, err := godotenv.Read(dotEnvPath)
if err != nil {
return nil, err return nil, err
} }
for key, value := range envs {
if _, ok := t.Env.Mapping[key]; !ok {
t.Env.Set(key, taskfile.Var{Static: value})
}
} }
} }
} }
@ -86,11 +88,9 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) {
return nil, ErrIncludedTaskfilesCantHaveIncludes return nil, ErrIncludedTaskfilesCantHaveIncludes
} }
if v >= 3.0 { if v >= 3.0 && len(includedTaskfile.Dotenv) > 0 {
if len(includedTaskfile.Dotenv) > 0 {
return nil, ErrIncludedTaskfilesCantHaveDotenvs return nil, ErrIncludedTaskfilesCantHaveDotenvs
} }
}
if includedTask.AdvancedImport { if includedTask.AdvancedImport {
for _, task := range includedTaskfile.Tasks { for _, task := range includedTaskfile.Tasks {