Add env loading
This commit is contained in:
35
main.go
35
main.go
@@ -7,6 +7,8 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
var Error *log.Logger
|
||||
@@ -25,6 +27,11 @@ func init() {
|
||||
log.Lmicroseconds|log.Lshortfile)
|
||||
}
|
||||
|
||||
const (
|
||||
steamcmdEnvKey = "STEAMCMD"
|
||||
appEnvKey = "APP"
|
||||
)
|
||||
|
||||
func main() {
|
||||
envfile := flag.String("envfile", ".env", "")
|
||||
flag.Parse()
|
||||
@@ -34,7 +41,22 @@ func main() {
|
||||
Error.Printf("Error leading env file: %v", err)
|
||||
return
|
||||
}
|
||||
log.Printf("%#v", env)
|
||||
|
||||
steamcmdexe, ok := env[steamcmdEnvKey]
|
||||
if !ok {
|
||||
Error.Printf("SteamCMD not found in env, please specify %s", steamcmdEnvKey)
|
||||
return
|
||||
}
|
||||
|
||||
app, ok := env[appEnvKey]
|
||||
if !ok {
|
||||
Error.Printf("App not found in env, please specify %s", appEnvKey)
|
||||
return
|
||||
}
|
||||
|
||||
args := flag.Args()
|
||||
log.Printf("Using steamcmd at %s", steamcmdexe)
|
||||
log.Printf("Downloading %d items for %s", len(args), app)
|
||||
}
|
||||
|
||||
func loadEnv(fpath string) (map[string]string, error) {
|
||||
@@ -43,5 +65,16 @@ func loadEnv(fpath string) (map[string]string, error) {
|
||||
fpath = filepath.Clean(fpath)
|
||||
log.Printf("Trying to open env file at %s", fpath)
|
||||
|
||||
file, err := os.Open(fpath)
|
||||
if err != nil {
|
||||
return res, fmt.Errorf("error opening env file: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
res, err = godotenv.Parse(file)
|
||||
if err != nil {
|
||||
return res, fmt.Errorf("error parsing env file: %w", err)
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user