-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
58 lines (50 loc) · 974 Bytes
/
main.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
57
58
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime/debug"
"github.com/joho/godotenv"
)
var (
showVersion = flag.Bool("version", false, "pring progream version")
)
// first load env from ~/.env
// second load env from .env
func init() {
flag.Parse()
if *showVersion {
PrintVersion()
os.Exit(0)
}
if len(os.Args) < 2 {
fmt.Printf("Usage: with-env {command}\n")
os.Exit(1)
}
home, _ := os.UserHomeDir()
loadEnv(filepath.Join(home, ".env"), ".env")
}
func loadEnv(files ...string) {
for _, envfile := range files {
if _, err := os.Stat(envfile); !os.IsNotExist(err) {
_ = godotenv.Overload(envfile)
}
}
}
func main() {
c := exec.Command(os.Args[1], os.Args[2:]...)
c.Stderr = os.Stderr
c.Stdout = os.Stdout
if err := c.Run(); err != nil {
fmt.Fprintf(os.Stderr, "%q\n", err)
os.Exit(1)
}
}
func PrintVersion() {
info, ok := debug.ReadBuildInfo()
if ok {
println(info.Main.Version, info.Main.Sum)
}
}