2023-12-29 20:32:03 +00:00
|
|
|
package taskfile
|
2021-06-05 15:54:10 -03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
|
|
|
|
"github.com/go-task/task/v3/internal/compiler"
|
2022-08-06 18:19:07 -03:00
|
|
|
"github.com/go-task/task/v3/internal/filepathext"
|
2021-06-05 15:54:10 -03:00
|
|
|
"github.com/go-task/task/v3/internal/templater"
|
2023-12-29 20:32:03 +00:00
|
|
|
"github.com/go-task/task/v3/taskfile/ast"
|
2021-06-05 15:54:10 -03:00
|
|
|
)
|
|
|
|
|
2023-12-29 20:32:03 +00:00
|
|
|
func Dotenv(c *compiler.Compiler, tf *ast.Taskfile, dir string) (*ast.Vars, error) {
|
2021-07-10 19:46:53 -03:00
|
|
|
if len(tf.Dotenv) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-06-05 15:54:10 -03:00
|
|
|
vars, err := c.GetTaskfileVariables()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-29 20:32:03 +00:00
|
|
|
env := &ast.Vars{}
|
2024-03-10 17:11:07 +00:00
|
|
|
cache := &templater.Cache{Vars: vars}
|
2021-06-05 15:54:10 -03:00
|
|
|
|
|
|
|
for _, dotEnvPath := range tf.Dotenv {
|
2024-03-10 17:11:07 +00:00
|
|
|
dotEnvPath = templater.Replace(dotEnvPath, cache)
|
2022-09-08 18:51:39 +00:00
|
|
|
if dotEnvPath == "" {
|
|
|
|
continue
|
|
|
|
}
|
2022-08-06 18:19:07 -03:00
|
|
|
dotEnvPath = filepathext.SmartJoin(dir, dotEnvPath)
|
2021-06-05 15:54:10 -03: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 12:07:57 +01:00
|
|
|
if ok := env.Exists(key); !ok {
|
2023-12-29 20:32:03 +00:00
|
|
|
env.Set(key, ast.Var{Value: value})
|
2021-06-05 15:54:10 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return env, nil
|
|
|
|
}
|