Add env loading
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
steamcmd
|
steamcmd
|
||||||
|
.env
|
||||||
|
2
go.mod
2
go.mod
@@ -1,3 +1,5 @@
|
|||||||
module steamcmd-api
|
module steamcmd-api
|
||||||
|
|
||||||
go 1.23.0
|
go 1.23.0
|
||||||
|
|
||||||
|
require github.com/joho/godotenv v1.5.1
|
||||||
|
2
go.sum
Normal file
2
go.sum
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||||
|
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
35
main.go
35
main.go
@@ -7,6 +7,8 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/joho/godotenv"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Error *log.Logger
|
var Error *log.Logger
|
||||||
@@ -25,6 +27,11 @@ func init() {
|
|||||||
log.Lmicroseconds|log.Lshortfile)
|
log.Lmicroseconds|log.Lshortfile)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
steamcmdEnvKey = "STEAMCMD"
|
||||||
|
appEnvKey = "APP"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
envfile := flag.String("envfile", ".env", "")
|
envfile := flag.String("envfile", ".env", "")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
@@ -34,7 +41,22 @@ func main() {
|
|||||||
Error.Printf("Error leading env file: %v", err)
|
Error.Printf("Error leading env file: %v", err)
|
||||||
return
|
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) {
|
func loadEnv(fpath string) (map[string]string, error) {
|
||||||
@@ -43,5 +65,16 @@ func loadEnv(fpath string) (map[string]string, error) {
|
|||||||
fpath = filepath.Clean(fpath)
|
fpath = filepath.Clean(fpath)
|
||||||
log.Printf("Trying to open env file at %s", 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
|
return res, nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user