chore: add godotenv, scan .env from paths

This commit is contained in:
Jesús Pérez Lorenzo 2021-08-31 09:02:58 +01:00
parent 99b249c5ab
commit b37eba06f1

29
run.go
View File

@ -8,6 +8,8 @@ import (
"os/exec" "os/exec"
"strings" "strings"
"github.com/joho/godotenv"
"github.com/UpCloudLtd/upcloud-go-api/upcloud" "github.com/UpCloudLtd/upcloud-go-api/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/upcloud/client" "github.com/UpCloudLtd/upcloud-go-api/upcloud/client"
@ -36,8 +38,15 @@ func run() int {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
username := "" username := os.Getenv("UPCLOUD_USERNAME")
password := "" 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 { if runFlags.encdr == TECODER.cmd || runFlags.encdr == TECODER.abbrv {
k := os.Getenv("KUPCLAPI") k := os.Getenv("KUPCLAPI")
if len(k) > 0 { if len(k) > 0 {
@ -59,12 +68,18 @@ func run() int {
username = os.Getenv("UPCLOUD_USERNAME") username = os.Getenv("UPCLOUD_USERNAME")
password = os.Getenv("UPCLOUD_PASSWORD") 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 { if len(username) == 0 {
fmt.Fprintln(os.Stderr, "Auth info: Username must be specified") fmt.Fprintln(os.Stderr, "Auth info: Username must be specified")
return 1 return 1
} }
if len(password) == 0 { if len(password) == 0 {
fmt.Fprintln(os.Stderr, "Auth info: Password must be specified") fmt.Fprintln(os.Stderr, "Auth info: Password must be specified")
return 2 return 2
@ -272,7 +287,7 @@ func run() int {
func onServers(s *service.Service, tsksrvc string, runFlags RunFlags, datacfg *DataConfig) error { func onServers(s *service.Service, tsksrvc string, runFlags RunFlags, datacfg *DataConfig) error {
uuid := runFlags.id uuid := runFlags.id
runComand := runFlags.runCmd runCommand := runFlags.runCmd
servers, err := s.GetServers() servers, err := s.GetServers()
if err != nil || len(servers.Servers) == 0 { if err != nil || len(servers.Servers) == 0 {
fmt.Fprintf(os.Stderr, "Unable to get servers: %#v\n", err) 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": case "runssh":
if sshAccess.Host != "" { if sshAccess.Host != "" {
output, err := runSSH(sshAccess, runComand) output, err := runSSH(sshAccess, runCommand)
if err != nil { if err != nil {
log.Fatal(err) 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) fmt.Fprintf(os.Stderr, "No Host to run ssh on %s \n", server.UUID)
} }
case "runcmd": case "runcmd":
args := strings.Split(runComand, " ") args := strings.Split(runCommand, " ")
output, err := cmdexec(args[0],args[1:]) output, err := cmdexec(args[0],args[1:])
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)