forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_dotenv.go
56 lines (45 loc) · 1 KB
/
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
54
55
56
package main
import (
"fmt"
"io/ioutil"
"github.com/direnv/go-dotenv"
)
// CmdDotEnv is `direnv 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(cmdDotEnvAction),
}
func cmdDotEnvAction(env Env, args []string) (err error) {
var shell Shell
var newenv Env
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
}
newenv, err = dotenv.Parse(string(data))
if err != nil {
return err
}
str := newenv.ToShell(shell)
fmt.Println(str)
return
}