diff --git a/run.go b/run.go index e639246..609f84d 100644 --- a/run.go +++ b/run.go @@ -8,6 +8,8 @@ import ( "os/exec" "strings" + "github.com/joho/godotenv" + "github.com/UpCloudLtd/upcloud-go-api/upcloud" "github.com/UpCloudLtd/upcloud-go-api/upcloud/client" @@ -36,8 +38,15 @@ func run() int { if err != nil { log.Fatal(err) } - username := "" - password := "" + username := os.Getenv("UPCLOUD_USERNAME") + password := os.Getenv("UPCLOUD_PASSWORD") + k := os.Getenv("KUPCLAPI") + if len(username) == 0 && len(k) == 0 { + err = godotenv.Load("/etc/upcloud/.env") + if err != nil { + err = godotenv.Load("/usr/local/etc/upcloud/.env") + } + } if runFlags.encdr == TECODER.cmd || runFlags.encdr == TECODER.abbrv { k := os.Getenv("KUPCLAPI") if len(k) > 0 { @@ -59,12 +68,18 @@ func run() int { username = os.Getenv("UPCLOUD_USERNAME") password = os.Getenv("UPCLOUD_PASSWORD") } - + if len(username) == 0 { + var upcloud_env map[string]string + upcloud_env, err = godotenv.Read(os.Getenv("HOME")+ "/.config/upctl.yaml") + if len(upcloud_env["username"]) != 0 { + username = upcloud_env["username"] + password = upcloud_env["password"] + } + } if len(username) == 0 { fmt.Fprintln(os.Stderr, "Auth info: Username must be specified") return 1 } - if len(password) == 0 { fmt.Fprintln(os.Stderr, "Auth info: Password must be specified") return 2 @@ -272,7 +287,7 @@ func run() int { func onServers(s *service.Service, tsksrvc string, runFlags RunFlags, datacfg *DataConfig) error { uuid := runFlags.id - runComand := runFlags.runCmd + runCommand := runFlags.runCmd servers, err := s.GetServers() if err != nil || len(servers.Servers) == 0 { fmt.Fprintf(os.Stderr, "Unable to get servers: %#v\n", err) @@ -417,7 +432,7 @@ func onServers(s *service.Service, tsksrvc string, runFlags RunFlags, datacfg *D } case "runssh": if sshAccess.Host != "" { - output, err := runSSH(sshAccess, runComand) + output, err := runSSH(sshAccess, runCommand) if err != nil { log.Fatal(err) } @@ -426,7 +441,7 @@ func onServers(s *service.Service, tsksrvc string, runFlags RunFlags, datacfg *D fmt.Fprintf(os.Stderr, "No Host to run ssh on %s \n", server.UUID) } case "runcmd": - args := strings.Split(runComand, " ") + args := strings.Split(runCommand, " ") output, err := cmdexec(args[0],args[1:]) if err != nil { log.Fatal(err)