Update to use new logger
This commit is contained in:
67
main.go
67
main.go
@@ -4,31 +4,16 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
logger "git.site.quack-lab.dev/dave/cylogger"
|
||||
|
||||
"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 (
|
||||
steamcmdEnvKey = "STEAMCMD"
|
||||
steamcmdScriptPathEnvKey = "STEAMCMD_SCRIPT"
|
||||
@@ -38,6 +23,8 @@ const (
|
||||
)
|
||||
|
||||
func main() {
|
||||
logger.Init(logger.LevelInfo)
|
||||
|
||||
envfile := flag.String("envfile", ".env", "")
|
||||
inputfile := flag.String("if", "", "")
|
||||
flag.Parse()
|
||||
@@ -46,40 +33,40 @@ func main() {
|
||||
if *inputfile != "" {
|
||||
filehandle, err := os.Open(*inputfile)
|
||||
if err != nil {
|
||||
Error.Printf("Error opening input file: %v", err)
|
||||
logger.Error("Error opening input file: %v", err)
|
||||
return
|
||||
}
|
||||
defer filehandle.Close()
|
||||
fargs, err := io.ReadAll(filehandle)
|
||||
if err != nil {
|
||||
Error.Printf("Error reading input file: %v", err)
|
||||
logger.Error("Error reading input file: %v", err)
|
||||
return
|
||||
}
|
||||
sargs := strings.Split(string(fargs), "\r\n")
|
||||
args = append(args, sargs...)
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
env, err := loadEnv(*envfile)
|
||||
if err != nil {
|
||||
Error.Printf("Error leading env file: %v", err)
|
||||
logger.Error("Error leading env file: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
steamcmdPath, ok := env[steamcmdEnvKey]
|
||||
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
|
||||
}
|
||||
steamcmdPath = filepath.Clean(steamcmdPath)
|
||||
steamcmdExe := filepath.Join(steamcmdPath, "steamcmd.exe")
|
||||
tempfile, err := os.Open(steamcmdExe)
|
||||
if err != nil {
|
||||
Error.Printf("Error opening SteamCMD, does it exist?: %v", err)
|
||||
logger.Error("Error opening SteamCMD, does it exist?: %v", err)
|
||||
return
|
||||
}
|
||||
tempfile.Close()
|
||||
@@ -87,31 +74,31 @@ func main() {
|
||||
steamcmdScriptPath, ok := env[steamcmdScriptPathEnvKey]
|
||||
if !ok {
|
||||
steamcmdScriptPath = filepath.Join(steamcmdPath, "script")
|
||||
log.Printf("SteamCMD script not found in env, using '%s'", steamcmdScriptPath)
|
||||
log.Printf("Specify script path with '%s'", steamcmdScriptPathEnvKey)
|
||||
logger.Info("SteamCMD script not found in env, using '%s'", steamcmdScriptPath)
|
||||
logger.Info("Specify script path with '%s'", steamcmdScriptPathEnvKey)
|
||||
}
|
||||
|
||||
app, ok := env[appEnvKey]
|
||||
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
|
||||
}
|
||||
|
||||
username, ok := env[usernameEnvKey]
|
||||
if !ok {
|
||||
username = "anonymous"
|
||||
log.Printf("Username not found in env, using '%s'", username)
|
||||
log.Printf("If you want to log in specify '%s' and '%s'", usernameEnvKey, passwordEnvKey)
|
||||
logger.Info("Username not found in env, using '%s'", username)
|
||||
logger.Info("If you want to log in specify '%s' and '%s'", usernameEnvKey, passwordEnvKey)
|
||||
}
|
||||
password, ok := env[passwordEnvKey]
|
||||
if !ok {
|
||||
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)
|
||||
log.Printf("As user '%s'", username)
|
||||
log.Printf("Downloading %d items for '%s'", len(args), app)
|
||||
logger.Info("Using steamcmd at '%s'", steamcmdPath)
|
||||
logger.Info("As user '%s'", username)
|
||||
logger.Info("Downloading %d items for '%s'", len(args), app)
|
||||
|
||||
scriptContents := make([]string, 0, len(args)+4)
|
||||
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)
|
||||
if err != nil {
|
||||
Error.Println("Could not open steamcmd script file")
|
||||
logger.Error("Could not open steamcmd script file")
|
||||
return
|
||||
}
|
||||
defer scriptFileHandle.Close()
|
||||
_, err = scriptFileHandle.Write([]byte(strings.Join(scriptContents, "\n")))
|
||||
if err != nil {
|
||||
Error.Println("Could not write to steamcmd script file")
|
||||
logger.Error("Could not write to steamcmd script file")
|
||||
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)
|
||||
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.Dir = steamcmdPath
|
||||
cmd.Stdout = os.Stdout
|
||||
@@ -145,17 +132,17 @@ func main() {
|
||||
cmd.Stdin = os.Stdin
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
Error.Printf("SteamCMD failed with code %v", err)
|
||||
logger.Error("SteamCMD failed with code %v", err)
|
||||
return
|
||||
}
|
||||
log.Printf("All done")
|
||||
logger.Info("All done")
|
||||
}
|
||||
|
||||
func loadEnv(fpath string) (map[string]string, error) {
|
||||
res := make(map[string]string)
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user