Skip to content

Commit

Permalink
added light linux support
Browse files Browse the repository at this point in the history
  • Loading branch information
JensvandeWiel committed Oct 18, 2023
1 parent 956bc59 commit 0c0e7a2
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 19 deletions.
14 changes: 14 additions & 0 deletions console/console.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package console

import "io"

func New(exePath string, args []string, stdout io.Writer) *Console {
p := NewParser()

return &Console{
commandLine: exePath,
stdout: stdout,
args: args,
Parser: p,
}
}
59 changes: 59 additions & 0 deletions console/console_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//go:build unix

package console

import (
"fmt"
"io"
"os/exec"
)

type Console struct {
stdout io.Writer // stdout is the writer where the output will be written to.
commandLine string // commandLine is the command that will be executed.
proc *exec.Cmd
exitCode uint32
args []string
Parser *Parser
}

// Run executes the command in steamcmd and returns the exit code. Exit code does not need to be 0 to return no errors (error is for executing the pseudoconsole)
func (c *Console) Run() (uint32, error) {
var err error

if c.commandLine == "" {
_, err := exec.LookPath(c.commandLine)
if err != nil {
return 1, fmt.Errorf("Steamcmd not found: %v\n", err)
}
}

var a []string
a = append(a, c.commandLine)
a = append(a, c.args...)

c.proc = exec.Command("sh", a...)
if err != nil {
return 1, err
}

d := &duplicateWriter{
writer1: c.Parser,
writer2: c.stdout,
}

c.proc.Stdout = d
c.proc.Stderr = d

err = c.proc.Run()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
c.exitCode = uint32(exitErr.ExitCode())
} else {
return 1, err
}
}
c.exitCode = uint32(c.proc.ProcessState.ExitCode())

return c.exitCode, nil
}
28 changes: 17 additions & 11 deletions console/console_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,38 @@ package console

import (
"context"
"fmt"
"github.com/UserExistsError/conpty"
"io"
"os/exec"
"strings"
)

type Console struct {
stdout io.Writer // stdout is the writer where the output will be written to.
commandLine string // commandLine is the command that will be executed.
conPTY *conpty.ConPty
args []string
exitCode uint32
Parser *Parser
}

func New(exePath string, stdout io.Writer) *Console {
p := NewParser()

return &Console{
commandLine: exePath,
stdout: stdout,
Parser: p,
}
}

// Run executes the command in steamcmd and returns the exit code. Exit code does not need to be 0 to return no errors (error is for executing the pseudoconsole)
func (c *Console) Run() (uint32, error) {
var err error
c.conPTY, err = conpty.Start(c.commandLine)

if c.commandLine == "" {
_, err := exec.LookPath(c.commandLine)
if err != nil {
return 1, fmt.Errorf("Steamcmd not found: %v\n", err)
}
}

var a []string
a = append(a, c.commandLine)
a = append(a, c.args...)

c.conPTY, err = conpty.Start(c.commandLine + " " + strings.Join(a, " "))
if err != nil {
return 1, err
}
Expand Down
32 changes: 32 additions & 0 deletions examples/install-rustserver-linux/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"github.com/jensvandewiel/gosteamcmd"
"github.com/jensvandewiel/gosteamcmd/console"
"os"
)

func main() {
//this code follows the steps of: https://www.rustafied.com/how-to-host-your-own-rust-server

prompts := []*gosteamcmd.Prompt{
gosteamcmd.ForceInstallDir("/home/jens/rustserver"),
gosteamcmd.Login("", ""),
gosteamcmd.AppUpdate(258550, "", true),
}

cmd := gosteamcmd.New(os.Stdout, prompts, "/mnt/MyProjects/gosteamcmd/steamcmd.sh")

cmd.Console.Parser.OnInformationReceived = func(action console.Action, progress float64, currentWritten, total uint64) {
println("")
}
cmd.Console.Parser.OnAppInstalled = func(app uint32) {
println("App installed: ", app, " Yay!")
}

_, err := cmd.Run()

if err != nil {
panic(err)
}
}
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module github.com/jensvandewiel/gosteamcmd

go 1.20

require github.com/UserExistsError/conpty v0.1.1
require (
github.com/UserExistsError/conpty v0.1.1
github.com/creack/pty v1.1.18
)

require golang.org/x/sys v0.11.0 // indirect
require golang.org/x/sys v0.13.0 // indirect
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
github.com/UserExistsError/conpty v0.1.1 h1:cHDsU/XeoeDAQmVvCTV53SrXLG39YJ4++Pp3iAi1gXE=
github.com/UserExistsError/conpty v0.1.1/go.mod h1:PDglKIkX3O/2xVk0MV9a6bCWxRmPVfxqZoTG/5sSd9I=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7 changes: 4 additions & 3 deletions steamcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ func New(stdout io.Writer, prompts []*Prompt, steamPath string) *SteamCMD {

//prepare command
cmd := path(steamPath)
var args []string
for _, prompt := range s.prompts {
cmd += " +" + prompt.FullPrompt
args = append(args, "+"+prompt.FullPrompt)
}
cmd += " +quit"
s.Console = console.New(cmd, s.Stdout)
args = append(args, "+quit")
s.Console = console.New(cmd, args, s.Stdout)

return s
}
Expand Down

0 comments on commit 0c0e7a2

Please sign in to comment.