Skip to content

Commit

Permalink
added everything to make it have basic functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
JensvandeWiel committed Aug 3, 2023
1 parent 38e0cad commit a619265
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 11 deletions.
41 changes: 41 additions & 0 deletions console/console_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//go:build windows

package console

import (
"context"
"github.com/UserExistsError/conpty"
"io"
)

type Console struct {
stdout io.Writer
commandLine string
conPTY *conpty.ConPty
ExitCode uint32
}

func New(exePath string, stdout io.Writer) *Console {
return &Console{
commandLine: exePath,
stdout: stdout,
}
}

func (c *Console) Run() error {
var err error
c.conPTY, err = conpty.Start(c.commandLine)
if err != nil {
return err
}
defer c.conPTY.Close()

go io.Copy(c.stdout, c.conPTY)

c.ExitCode, err = c.conPTY.Wait(context.Background())
if err != nil {
return err
}

return nil
}
5 changes: 0 additions & 5 deletions examples/1/main.go

This file was deleted.

17 changes: 17 additions & 0 deletions examples/install-rustserver/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import "github.com/jensvandewiel/gosteamcmd"

func main() {
//this code follows the steps of: https://www.rustafied.com/how-to-host-your-own-rust-server
cmd := gosteamcmd.New()
cmd.Prompts = append(cmd.Prompts, gosteamcmd.ForceInstallDir("c:\\rustserver\\"))
cmd.Prompts = append(cmd.Prompts, gosteamcmd.Login("", ""))
cmd.Prompts = append(cmd.Prompts, gosteamcmd.AppUpdate(258550, "", false))
//running it headless means it will not output anything
err := cmd.RunHeadless()

if err != nil {
panic(err)
}
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module github.com/jensvandewiel/gosteamcmd

go 1.20

require (
github.com/UserExistsError/conpty v0.1.1 // indirect
golang.org/x/sys v0.8.0 // indirect
)
16 changes: 12 additions & 4 deletions propmt.go → prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type PromptType int
const (
AppUpdatePrompt PromptType = iota
LoginPrompt
ForceInstallDirPrompt
)

type Prompt struct {
Expand All @@ -15,7 +16,7 @@ type Prompt struct {
}

// AppUpdate updates the given appID. If beta is not empty, it will update to the given beta. If validate is true, it will validate the files.
func AppUpdate(appID int, beta string, validate bool) Prompt {
func AppUpdate(appID int, beta string, validate bool) *Prompt {
cmd := "app_update "
cmd += strconv.Itoa(appID) + " "
if beta != "" {
Expand All @@ -24,16 +25,23 @@ func AppUpdate(appID int, beta string, validate bool) Prompt {
if validate {
cmd += "validate"
}
return Prompt{cmd, AppUpdatePrompt}
return &Prompt{cmd, AppUpdatePrompt}
}

// Login logs into SteamCMD with the given username and password. If the arguments are empty, it will login as anonymous.
func Login(username string, password string) Prompt {
func Login(username string, password string) *Prompt {
cmd := "login "
if username != "" {
cmd += username + " " + password
} else {
cmd += "anonymous"
}
return Prompt{cmd, LoginPrompt}
return &Prompt{cmd, LoginPrompt}
}

func ForceInstallDir(directory string) *Prompt {
cmd := "force_install_dir "
cmd += directory

return &Prompt{cmd, ForceInstallDirPrompt}
}
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This is a W.I.P. project, so it's not finished yet.

To use you must have steamcmd instaled and added it to your path, for now you can only use the library from windows

## Contribute
I would love to see people contributing to this project, so if you want to contribute just fork the project and make a pull request.
30 changes: 28 additions & 2 deletions steamcmd.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
package gosteamcmd

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

type SteamCMD struct {
prompts *[]Prompt
// Prompts contains all the commands that will be executed.
Prompts []*Prompt
console *console.Console
}

func New() *SteamCMD {
return &SteamCMD{}
return &SteamCMD{
Prompts: make([]*Prompt, 0),
}
}

func (s *SteamCMD) RunHeadless() error {
cmd := "steamcmd"

for _, prompt := range s.Prompts {
cmd += " +" + prompt.FullPrompt
}

cmd += " +quit"

s.console = console.New(cmd, os.Stdout)
err := s.console.Run()
if err != nil {
return err
}
return nil
}

0 comments on commit a619265

Please sign in to comment.