1
0
mirror of https://github.com/go-task/task.git synced 2025-01-06 03:53:54 +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
- 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)).

View File

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

View File

@ -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 {