Update to use new logger

This commit is contained in:
2025-07-17 17:18:33 +02:00
parent f1c7825d95
commit 45eae63b12
3 changed files with 72 additions and 42 deletions

17
go.mod
View File

@@ -1,5 +1,18 @@
module steamcmd-api module steamcmd-api
go 1.23.0 go 1.23.6
require github.com/joho/godotenv v1.5.1 require (
git.site.quack-lab.dev/dave/cylogger v1.3.0
git.site.quack-lab.dev/dave/cyutils v1.0.0
github.com/joho/godotenv v1.5.1
)
require (
github.com/google/go-cmp v0.5.9 // indirect
github.com/hexops/valast v1.5.0 // indirect
golang.org/x/mod v0.7.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/tools v0.4.0 // indirect
mvdan.cc/gofumpt v0.4.0 // indirect
)

30
go.sum
View File

@@ -1,2 +1,32 @@
git.site.quack-lab.dev/dave/cylogger v1.3.0 h1:eTWPUD+ThVi8kGIsRcE0XDeoH3yFb5miFEODyKUdWJw=
git.site.quack-lab.dev/dave/cylogger v1.3.0/go.mod h1:wctgZplMvroA4X6p8f4B/LaCKtiBcT1Pp+L14kcS8jk=
git.site.quack-lab.dev/dave/cyutils v1.0.0 h1:yp/jkM2M7UZ+UIQuy+vPI7yDvTUdpbEdFL8h0lzUTvA=
git.site.quack-lab.dev/dave/cyutils v1.0.0/go.mod h1:luGNFimplFhkpRLebhkVTNjG2wYfPAs+pu+UIMhBYbE=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hexops/autogold v0.8.1 h1:wvyd/bAJ+Dy+DcE09BoLk6r4Fa5R5W+O+GUzmR985WM=
github.com/hexops/autogold v0.8.1/go.mod h1:97HLDXyG23akzAoRYJh/2OBs3kd80eHyKPvZw0S5ZBY=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/hexops/valast v1.5.0 h1:FBTuvVi0wjTngtXJRZXMbkN/Dn6DgsUsBwch2DUJU8Y=
github.com/hexops/valast v1.5.0/go.mod h1:Jcy1pNH7LNraVaAZDLyv21hHg2WBv9Nf9FL6fGxU7o4=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA=
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.4.0 h1:7mTAgkunk3fr4GAloyyCasadO6h9zSsQZbwvcaIciV4=
golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ=
mvdan.cc/gofumpt v0.4.0 h1:JVf4NN1mIpHogBj7ABpgOyZc65/UUOkKQFkoURsz4MM=
mvdan.cc/gofumpt v0.4.0/go.mod h1:PljLOHDeZqgS8opHRKLzp2It2VBuSdteAgqUfzMTxlQ=

67
main.go
View File

@@ -4,31 +4,16 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"log"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
logger "git.site.quack-lab.dev/dave/cylogger"
"github.com/joho/godotenv" "github.com/joho/godotenv"
) )
var Error *log.Logger
var Warning *log.Logger
func init() {
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
logger := io.MultiWriter(os.Stdout)
log.SetOutput(logger)
Error = log.New(io.MultiWriter(os.Stderr, os.Stdout),
fmt.Sprintf("%sERROR:%s ", "\033[0;101m", "\033[0m"),
log.Lmicroseconds|log.Lshortfile)
Warning = log.New(io.MultiWriter(os.Stdout),
fmt.Sprintf("%sWarning:%s ", "\033[0;93m", "\033[0m"),
log.Lmicroseconds|log.Lshortfile)
}
const ( const (
steamcmdEnvKey = "STEAMCMD" steamcmdEnvKey = "STEAMCMD"
steamcmdScriptPathEnvKey = "STEAMCMD_SCRIPT" steamcmdScriptPathEnvKey = "STEAMCMD_SCRIPT"
@@ -38,6 +23,8 @@ const (
) )
func main() { func main() {
logger.Init(logger.LevelInfo)
envfile := flag.String("envfile", ".env", "") envfile := flag.String("envfile", ".env", "")
inputfile := flag.String("if", "", "") inputfile := flag.String("if", "", "")
flag.Parse() flag.Parse()
@@ -46,40 +33,40 @@ func main() {
if *inputfile != "" { if *inputfile != "" {
filehandle, err := os.Open(*inputfile) filehandle, err := os.Open(*inputfile)
if err != nil { if err != nil {
Error.Printf("Error opening input file: %v", err) logger.Error("Error opening input file: %v", err)
return return
} }
defer filehandle.Close() defer filehandle.Close()
fargs, err := io.ReadAll(filehandle) fargs, err := io.ReadAll(filehandle)
if err != nil { if err != nil {
Error.Printf("Error reading input file: %v", err) logger.Error("Error reading input file: %v", err)
return return
} }
sargs := strings.Split(string(fargs), "\r\n") sargs := strings.Split(string(fargs), "\r\n")
args = append(args, sargs...) args = append(args, sargs...)
} }
if len(args) == 0 { if len(args) == 0 {
Error.Println("No args specified, please pass space delimited list of workshop ids for downloading") logger.Error("No args specified, please pass space delimited list of workshop ids for downloading")
return return
} }
env, err := loadEnv(*envfile) env, err := loadEnv(*envfile)
if err != nil { if err != nil {
Error.Printf("Error leading env file: %v", err) logger.Error("Error leading env file: %v", err)
return return
} }
steamcmdPath, ok := env[steamcmdEnvKey] steamcmdPath, ok := env[steamcmdEnvKey]
if !ok { if !ok {
Error.Printf("SteamCMD not found in env, please specify '%s'", steamcmdEnvKey) logger.Error("SteamCMD not found in env, please specify '%s'", steamcmdEnvKey)
return return
} }
steamcmdPath = filepath.Clean(steamcmdPath) steamcmdPath = filepath.Clean(steamcmdPath)
steamcmdExe := filepath.Join(steamcmdPath, "steamcmd.exe") steamcmdExe := filepath.Join(steamcmdPath, "steamcmd.exe")
tempfile, err := os.Open(steamcmdExe) tempfile, err := os.Open(steamcmdExe)
if err != nil { if err != nil {
Error.Printf("Error opening SteamCMD, does it exist?: %v", err) logger.Error("Error opening SteamCMD, does it exist?: %v", err)
return return
} }
tempfile.Close() tempfile.Close()
@@ -87,31 +74,31 @@ func main() {
steamcmdScriptPath, ok := env[steamcmdScriptPathEnvKey] steamcmdScriptPath, ok := env[steamcmdScriptPathEnvKey]
if !ok { if !ok {
steamcmdScriptPath = filepath.Join(steamcmdPath, "script") steamcmdScriptPath = filepath.Join(steamcmdPath, "script")
log.Printf("SteamCMD script not found in env, using '%s'", steamcmdScriptPath) logger.Info("SteamCMD script not found in env, using '%s'", steamcmdScriptPath)
log.Printf("Specify script path with '%s'", steamcmdScriptPathEnvKey) logger.Info("Specify script path with '%s'", steamcmdScriptPathEnvKey)
} }
app, ok := env[appEnvKey] app, ok := env[appEnvKey]
if !ok { if !ok {
Error.Printf("App not found in env, please specify '%s'", appEnvKey) logger.Error("App not found in env, please specify '%s'", appEnvKey)
return return
} }
username, ok := env[usernameEnvKey] username, ok := env[usernameEnvKey]
if !ok { if !ok {
username = "anonymous" username = "anonymous"
log.Printf("Username not found in env, using '%s'", username) logger.Info("Username not found in env, using '%s'", username)
log.Printf("If you want to log in specify '%s' and '%s'", usernameEnvKey, passwordEnvKey) logger.Info("If you want to log in specify '%s' and '%s'", usernameEnvKey, passwordEnvKey)
} }
password, ok := env[passwordEnvKey] password, ok := env[passwordEnvKey]
if !ok { if !ok {
password = "" password = ""
log.Printf("Password not found in env, using empty") logger.Info("Password not found in env, using empty")
} }
log.Printf("Using steamcmd at '%s'", steamcmdPath) logger.Info("Using steamcmd at '%s'", steamcmdPath)
log.Printf("As user '%s'", username) logger.Info("As user '%s'", username)
log.Printf("Downloading %d items for '%s'", len(args), app) logger.Info("Downloading %d items for '%s'", len(args), app)
scriptContents := make([]string, 0, len(args)+4) scriptContents := make([]string, 0, len(args)+4)
scriptContents = append(scriptContents, "@ShutdownOnFailedCommand 0") scriptContents = append(scriptContents, "@ShutdownOnFailedCommand 0")
@@ -124,20 +111,20 @@ func main() {
scriptFileHandle, err := os.OpenFile(steamcmdScriptPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0755) scriptFileHandle, err := os.OpenFile(steamcmdScriptPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0755)
if err != nil { if err != nil {
Error.Println("Could not open steamcmd script file") logger.Error("Could not open steamcmd script file")
return return
} }
defer scriptFileHandle.Close() defer scriptFileHandle.Close()
_, err = scriptFileHandle.Write([]byte(strings.Join(scriptContents, "\n"))) _, err = scriptFileHandle.Write([]byte(strings.Join(scriptContents, "\n")))
if err != nil { if err != nil {
Error.Println("Could not write to steamcmd script file") logger.Error("Could not write to steamcmd script file")
return return
} }
log.Printf("Wrote %d lines to script file", len(scriptContents)) logger.Info("Wrote %d lines to script file", len(scriptContents))
steamcmdExe, _ = filepath.Abs(steamcmdExe) steamcmdExe, _ = filepath.Abs(steamcmdExe)
steamcmdScriptPath, _ = filepath.Abs(steamcmdScriptPath) steamcmdScriptPath, _ = filepath.Abs(steamcmdScriptPath)
log.Printf("Running steamcmd at %s", steamcmdExe) logger.Info("Running steamcmd at %s", steamcmdExe)
cmd := exec.Command(steamcmdExe, "+runscript", steamcmdScriptPath) cmd := exec.Command(steamcmdExe, "+runscript", steamcmdScriptPath)
cmd.Dir = steamcmdPath cmd.Dir = steamcmdPath
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
@@ -145,17 +132,17 @@ func main() {
cmd.Stdin = os.Stdin cmd.Stdin = os.Stdin
err = cmd.Run() err = cmd.Run()
if err != nil { if err != nil {
Error.Printf("SteamCMD failed with code %v", err) logger.Error("SteamCMD failed with code %v", err)
return return
} }
log.Printf("All done") logger.Info("All done")
} }
func loadEnv(fpath string) (map[string]string, error) { func loadEnv(fpath string) (map[string]string, error) {
res := make(map[string]string) res := make(map[string]string)
fpath = filepath.Clean(fpath) fpath = filepath.Clean(fpath)
log.Printf("Trying to open env file at '%s'", fpath) logger.Info("Trying to open env file at '%s'", fpath)
file, err := os.Open(fpath) file, err := os.Open(fpath)
if err != nil { if err != nil {