#324 implement dotenv

This commit is contained in:
Chris Garrett
2020-08-03 16:18:38 -06:00
parent e5a3c861cb
commit 8b962fb8e8
13 changed files with 144 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ package read
import (
"errors"
"fmt"
"github.com/joho/godotenv"
"os"
"path/filepath"
"runtime"
@@ -16,6 +17,7 @@ import (
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 = errors.New("task: Included Taskfiles can't have dotenv declarations. Please, move the dotenv declaration to the main Taskfile")
)
// Taskfile reads a Taskfile for a given directory
@@ -34,6 +36,22 @@ 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
}
}
}
}
for namespace, includedTask := range t.Includes {
if v >= 3.0 {
tr := templater.Templater{Vars: &taskfile.Vars{}, RemoveNoValue: true}
@@ -68,6 +86,12 @@ 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 includedTask.AdvancedImport {
for _, task := range includedTaskfile.Tasks {
if !filepath.IsAbs(task.Dir) {

View File

@@ -16,6 +16,7 @@ type Taskfile struct {
Env *Vars
Tasks Tasks
Silent bool
Dotenv []string
}
// UnmarshalYAML implements yaml.Unmarshaler interface
@@ -30,6 +31,7 @@ func (tf *Taskfile) UnmarshalYAML(unmarshal func(interface{}) error) error {
Env *Vars
Tasks Tasks
Silent bool
Dotenv []string
}
if err := unmarshal(&taskfile); err != nil {
return err
@@ -43,6 +45,7 @@ func (tf *Taskfile) UnmarshalYAML(unmarshal func(interface{}) error) error {
tf.Env = taskfile.Env
tf.Tasks = taskfile.Tasks
tf.Silent = taskfile.Silent
tf.Dotenv = taskfile.Dotenv
if tf.Expansions <= 0 {
tf.Expansions = 2
}