-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from joho/add_bin_command
Add a bin command
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
|
||
"strings" | ||
|
||
"github.com/joho/godotenv" | ||
) | ||
|
||
func main() { | ||
var showHelp bool | ||
flag.BoolVar(&showHelp, "h", false, "show help") | ||
var rawEnvFilenames string | ||
flag.StringVar(&rawEnvFilenames, "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 | ||
args := flag.Args() | ||
if showHelp || len(args) == 0 { | ||
fmt.Println(usage) | ||
return | ||
} | ||
|
||
// load env | ||
var envFilenames []string | ||
if rawEnvFilenames != "" { | ||
envFilenames = strings.Split(rawEnvFilenames, ",") | ||
} | ||
|
||
// take rest of args and "exec" them | ||
cmd := args[0] | ||
cmdArgs := args[1:len(args)] | ||
|
||
err := godotenv.Exec(envFilenames, cmd, cmdArgs) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters