2023-12-29 22:32:03 +02:00
|
|
|
package taskfile
|
2021-06-05 20:54:10 +02:00
|
|
|
|
|
|
|
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"
|
2023-12-29 22:32:03 +02:00
|
|
|
"github.com/go-task/task/v3/taskfile/ast"
|
2021-06-05 20:54:10 +02:00
|
|
|
)
|
|
|
|
|
2023-12-29 22:32:03 +02:00
|
|
|
func Dotenv(c *compiler.Compiler, tf *ast.Taskfile, dir string) (*ast.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
|
|
|
|
}
|
|
|
|
|
2023-12-29 22:32:03 +02:00
|
|
|
env := &ast.Vars{}
|
2021-06-05 20:54:10 +02:00
|
|
|
|
2023-12-29 22:26:02 +02:00
|
|
|
tr := templater.Templater{Vars: vars}
|
2021-06-05 20:54:10 +02:00
|
|
|
|
|
|
|
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 {
|
2023-12-29 22:32:03 +02:00
|
|
|
env.Set(key, ast.Var{Value: value})
|
2021-06-05 20:54:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return env, nil
|
|
|
|
}
|