forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_dotenv.go
53 lines (43 loc) · 988 Bytes
/
cmd_dotenv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"io/ioutil"
"github.com/direnv/go-dotenv"
)
// `direnv private dotenv [SHELL [PATH_TO_DOTENV]]`
// Transforms a .env file to evaluatable `export KEY=PAIR` statements.
//
// See: https://github.com/bkeepers/dotenv and
// https://github.com/ddollar/foreman
var CmdDotEnv = &Cmd{
Name: "dotenv",
Desc: "Transforms a .env file to evaluatable `export KEY=PAIR` statements",
Args: []string{"[SHELL]", "[PATH_TO_DOTENV]"},
Private: true,
Action: actionSimple(func(env Env, args []string) (err error) {
var shell Shell
var target string
if len(args) > 1 {
shell = DetectShell(args[1])
} else {
shell = BASH
}
if len(args) > 2 {
target = args[2]
}
if target == "" {
target = ".env"
}
var data []byte
if data, err = ioutil.ReadFile(target); err != nil {
return
}
env, err = dotenv.Parse(string(data))
if err != nil {
return err
}
str := env.ToShell(shell)
fmt.Println(str)
return
}),
}