Skip to content

Commit 173eff8

Browse files
refactor: update InstallAndRunSteamCMD to return exit status and error; handle response in (http) update handler
1 parent 094e305 commit 173eff8

2 files changed

Lines changed: 38 additions & 27 deletions

File tree

src/setup/steamcmd.go

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package setup
22

33
import (
4+
"fmt"
45
"io"
56
"os"
67
"os/exec"
@@ -26,32 +27,33 @@ const (
2627
)
2728

2829
// InstallAndRunSteamCMD installs and runs SteamCMD based on the platform (Windows/Linux).
29-
// It automatically detects the OS and calls the appropriate installation function.
30-
func InstallAndRunSteamCMD() {
30+
// It returns the exit status of the SteamCMD execution and any error encountered.
31+
func InstallAndRunSteamCMD() (int, error) {
3132
if config.Branch == "indev-no-steamcmd" {
3233
logger.Install.Info("🔍 Detected indev-no-steamcmd branch, skipping SteamCMD installation")
33-
return
34+
return 0, nil
3435
}
3536

3637
if runtime.GOOS == "windows" {
37-
installSteamCMDWindows()
38+
return installSteamCMDWindows()
3839
} else if runtime.GOOS == "linux" {
39-
installSteamCMDLinux()
40+
return installSteamCMDLinux()
4041
} else {
41-
logger.Install.Error("❌ SteamCMD installation is not supported on this OS.\n")
42-
return
42+
err := fmt.Errorf("SteamCMD installation is not supported on this OS")
43+
logger.Install.Error("❌ " + err.Error() + "\n")
44+
return -1, err
4345
}
4446
}
4547

46-
func installSteamCMD(platform string, steamCMDDir string, downloadURL string, extractFunc ExtractorFunc) {
48+
func installSteamCMD(platform string, steamCMDDir string, downloadURL string, extractFunc ExtractorFunc) (int, error) {
4749
// Check if SteamCMD is already installed
4850
if _, err := os.Stat(steamCMDDir); os.IsNotExist(err) {
4951
logger.Install.Warn("⚠️ SteamCMD not found for " + platform + ", downloading...\n")
5052

5153
// Create SteamCMD directory
5254
if err := createSteamCMDDirectory(steamCMDDir); err != nil {
5355
logger.Install.Error("❌ Error creating SteamCMD directory: " + err.Error() + "\n")
54-
return
56+
return -1, err
5557
}
5658

5759
// Ensure cleanup on failure
@@ -66,63 +68,62 @@ func installSteamCMD(platform string, steamCMDDir string, downloadURL string, ex
6668
// Install required libraries
6769
if err := installRequiredLibraries(); err != nil {
6870
logger.Install.Error("❌ Error installing required libraries: " + err.Error() + "\n")
69-
return
71+
return -1, err
7072
}
7173

7274
// Download and extract SteamCMD
7375
if err := downloadAndExtractSteamCMD(downloadURL, steamCMDDir, extractFunc); err != nil {
7476
logger.Install.Error("❌ " + err.Error() + "\n")
75-
return
77+
return -1, err
7678
}
7779

7880
// Set executable permissions for SteamCMD files
7981
if err := setExecutablePermissions(steamCMDDir); err != nil {
8082
logger.Install.Error("❌ Error setting executable permissions: " + err.Error() + "\n")
81-
return
83+
return -1, err
8284
}
8385

8486
// Verify the steamcmd binary
8587
if err := verifySteamCMDBinary(steamCMDDir); err != nil {
8688
logger.Install.Error("❌ " + err.Error() + "\n")
87-
return
89+
return -1, err
8890
}
8991

9092
// Mark installation as successful
9193
success = true
9294
logger.Install.Info("✅ SteamCMD installed successfully.\n")
9395
} else {
94-
9596
logger.Install.Info("✅ SteamCMD is already installed.")
9697
}
9798

98-
// Run SteamCMD
99-
runSteamCMD(steamCMDDir)
99+
// Run SteamCMD and return its exit status and error
100+
return runSteamCMD(steamCMDDir)
100101
}
101102

102103
// installSteamCMDLinux downloads and installs SteamCMD on Linux.
103-
func installSteamCMDLinux() {
104-
installSteamCMD("Linux", SteamCMDLinuxDir, SteamCMDLinuxURL, untarWrapper)
104+
func installSteamCMDLinux() (int, error) {
105+
return installSteamCMD("Linux", SteamCMDLinuxDir, SteamCMDLinuxURL, untarWrapper)
105106
}
106107

107108
// installSteamCMDWindows downloads and installs SteamCMD on Windows.
108-
func installSteamCMDWindows() {
109-
installSteamCMD("Windows", SteamCMDWindowsDir, SteamCMDWindowsURL, unzip)
109+
func installSteamCMDWindows() (int, error) {
110+
return installSteamCMD("Windows", SteamCMDWindowsDir, SteamCMDWindowsURL, unzip)
110111
}
111112

112-
// runSteamCMD runs the SteamCMD command to update the game.
113-
func runSteamCMD(steamCMDDir string) {
113+
// runSteamCMD runs the SteamCMD command to update the game and returns its exit status and any error.
114+
func runSteamCMD(steamCMDDir string) (int, error) {
114115
currentDir, err := os.Getwd()
115116
if err != nil {
116117
logger.Install.Error("❌ Error getting current working directory: " + err.Error() + "\n")
117-
return
118+
return -1, err
118119
}
119120
logger.Install.Debug("✅ Current working directory: " + currentDir + "\n")
120121

121122
// Ensure permissions every time if we run on linux
122123
if runtime.GOOS != "windows" {
123124
if err := setExecutablePermissions(steamCMDDir); err != nil {
124125
logger.Install.Error("❌ Error setting executable permissions, your Steamcmd install might be broken: " + err.Error() + "\n")
125-
return
126+
return -1, err
126127
}
127128
}
128129

@@ -142,10 +143,15 @@ func runSteamCMD(steamCMDDir string) {
142143
}
143144
err = cmd.Run()
144145
if err != nil {
146+
if exitErr, ok := err.(*exec.ExitError); ok {
147+
logger.Install.Error("❌ SteamCMD exited unsuccessfully: " + err.Error() + "\n")
148+
return exitErr.ExitCode(), err
149+
}
145150
logger.Install.Error("❌ Error running SteamCMD: " + err.Error() + "\n")
146-
return
151+
return -1, err
147152
}
148153
logger.Install.Info("✅ SteamCMD executed successfully.\n")
154+
return 0, nil
149155
}
150156

151157
// buildSteamCMDCommand constructs the SteamCMD command based on the OS.

src/web/http.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,18 @@ func HandleRunSteamCMD(w http.ResponseWriter, r *http.Request) {
162162
time.Sleep(10000 * time.Millisecond)
163163
}
164164
logger.Core.Info("Running SteamCMD")
165-
setup.InstallAndRunSteamCMD()
165+
_, err := setup.InstallAndRunSteamCMD()
166166

167167
// Update last execution time
168168
lastSteamCMDExecution = time.Now()
169169

170170
// Success: return 202 Accepted and JSON
171171
w.WriteHeader(http.StatusAccepted)
172172
w.Header().Set("Content-Type", "application/json")
173-
json.NewEncoder(w).Encode(map[string]string{"statuscode": "202", "status": "Accepted", "message": "SteamCMD ran successfully."})
173+
if err == nil {
174+
json.NewEncoder(w).Encode(map[string]string{"statuscode": "202", "status": "Success", "message": "SteamCMD ran successfully."})
175+
return
176+
}
177+
// Failure: return 202 Accepted and JSON with the error message
178+
json.NewEncoder(w).Encode(map[string]string{"statuscode": "202", "status": "Failed", "message": "SteamCMD ran unsuccessfully:" + err.Error()})
174179
}

0 commit comments

Comments
 (0)