Get the minimum viable of the command working.

Stripped debug code out
Tidied up some flag handling
Get it working with and without the -f flag.
This commit is contained in:
John Barton (joho) 2014-09-21 10:42:05 +10:00
parent 28b52615cf
commit 8c38c298ed

View File

@ -11,8 +11,10 @@ import (
) )
func main() { func main() {
showHelp := *flag.Bool("h", false, "show help") var showHelp bool
rawEnvFilenames := *flag.String("f", "", "comma separated paths to .env files") flag.BoolVar(&showHelp, "h", false, "show help")
var rawEnvFilenames string
flag.StringVar(&rawEnvFilenames, "f", "", "comma separated paths to .env files")
flag.Parse() flag.Parse()
@ -29,26 +31,28 @@ example
` `
// if no args or -h flag // if no args or -h flag
// print usage and return // print usage and return
if showHelp || len(os.Args) < 2 { args := flag.Args()
if showHelp || len(args) == 0 {
fmt.Println(usage) fmt.Println(usage)
return return
} }
// load env // load env
// TODO something in flag passing or whatever isn't quite right // TODO would be nicer for an empty rawEnvFilenames to give us an empty map
// and then only call Load() once
// other TODO error handling on Load()
if rawEnvFilenames == "" {
godotenv.Load()
} else {
envFilenames := strings.Split(rawEnvFilenames, ",") envFilenames := strings.Split(rawEnvFilenames, ",")
fmt.Printf("env filenames %v\n", envFilenames)
godotenv.Load(envFilenames...) godotenv.Load(envFilenames...)
fmt.Printf("FOO=%v\n", os.Getenv("FOO")) }
// take rest of args and "exec" them // take rest of args and "exec" them
args := flag.Args()
cmd := args[0] cmd := args[0]
cmdArgs := args[1:len(args)] cmdArgs := args[1:len(args)]
fmt.Printf("cmd %v with args: %v\n", cmd, cmdArgs)
command := exec.Command(cmd, cmdArgs...) command := exec.Command(cmd, cmdArgs...)
// command.Env = os.Environ()
command.Stdin = os.Stdin command.Stdin = os.Stdin
command.Stdout = os.Stdout command.Stdout = os.Stdout
command.Stderr = os.Stderr command.Stderr = os.Stderr