2021-06-05 20:54:10 +02:00
|
|
|
package read
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
|
|
|
|
"github.com/go-task/task/v3/internal/compiler"
|
2022-08-06 23:19:07 +02:00
|
|
|
"github.com/go-task/task/v3/internal/filepathext"
|
2021-06-05 20:54:10 +02:00
|
|
|
"github.com/go-task/task/v3/internal/templater"
|
|
|
|
"github.com/go-task/task/v3/taskfile"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Dotenv(c compiler.Compiler, tf *taskfile.Taskfile, dir string) (*taskfile.Vars, error) {
|
2021-07-11 00:46:53 +02:00
|
|
|
if len(tf.Dotenv) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-06-05 20:54:10 +02:00
|
|
|
vars, err := c.GetTaskfileVariables()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
env := &taskfile.Vars{}
|
|
|
|
|
|
|
|
tr := templater.Templater{Vars: vars, RemoveNoValue: true}
|
|
|
|
|
|
|
|
for _, dotEnvPath := range tf.Dotenv {
|
|
|
|
dotEnvPath = tr.Replace(dotEnvPath)
|
2022-09-08 20:51:39 +02:00
|
|
|
if dotEnvPath == "" {
|
|
|
|
continue
|
|
|
|
}
|
2022-08-06 23:19:07 +02:00
|
|
|
dotEnvPath = filepathext.SmartJoin(dir, dotEnvPath)
|
2021-06-05 20:54:10 +02:00
|
|
|
|
|
|
|
if _, err := os.Stat(dotEnvPath); os.IsNotExist(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
envs, err := godotenv.Read(dotEnvPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for key, value := range envs {
|
2023-04-06 13:07:57 +02:00
|
|
|
if ok := env.Exists(key); !ok {
|
2021-06-05 20:54:10 +02:00
|
|
|
env.Set(key, taskfile.Var{Static: value})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return env, nil
|
|
|
|
}
|