From 28b52615cf7a190dcf98f5618320cfd091835a7c Mon Sep 17 00:00:00 2001 From: "John Barton (joho)" Date: Sat, 20 Sep 2014 21:38:51 +1000 Subject: [PATCH] WIP on adding a bin command --- cmd/cmd.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 cmd/cmd.go diff --git a/cmd/cmd.go b/cmd/cmd.go new file mode 100644 index 0000000..cb16b5a --- /dev/null +++ b/cmd/cmd.go @@ -0,0 +1,56 @@ +package main + +import ( + "flag" + "fmt" + "os" + "os/exec" + "strings" + + "github.com/joho/godotenv" +) + +func main() { + showHelp := *flag.Bool("h", false, "show help") + rawEnvFilenames := *flag.String("f", "", "comma separated paths to .env files") + + flag.Parse() + + usage := ` +Run a process with a env setup from a .env file + +godotenv [-f ENV_FILE_PATHS] COMMAND_ARGS + +ENV_FILE_PATHS: comma separated paths to .env files +COMMAND_ARGS: command and args you want to run + +example + godotenv -f /path/to/something/.env,/another/path/.env fortune +` + // if no args or -h flag + // print usage and return + if showHelp || len(os.Args) < 2 { + fmt.Println(usage) + return + } + + // load env + // TODO something in flag passing or whatever isn't quite right + envFilenames := strings.Split(rawEnvFilenames, ",") + fmt.Printf("env filenames %v\n", envFilenames) + godotenv.Load(envFilenames...) + fmt.Printf("FOO=%v\n", os.Getenv("FOO")) + + // take rest of args and "exec" them + args := flag.Args() + cmd := args[0] + cmdArgs := args[1:len(args)] + fmt.Printf("cmd %v with args: %v\n", cmd, cmdArgs) + + command := exec.Command(cmd, cmdArgs...) + // command.Env = os.Environ() + command.Stdin = os.Stdin + command.Stdout = os.Stdout + command.Stderr = os.Stderr + command.Start() +}