Skip to content

Commit

Permalink
fix: lint issues (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlucktay committed Nov 18, 2023
1 parent aac6194 commit 8675154
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
6 changes: 3 additions & 3 deletions cmd/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ func monitor(logger *logging.Logger, cfg config) {
for {
time.Sleep(cfg.Monitor.MonitorInterval)

r := dialAndAuth(logger, cfg.RCON)
rcon := dialAndAuth(logger, cfg.RCON)

players, errCP := r.CmdPlayers()
players, errCP := rcon.CmdPlayers()
if errCP != nil {
logger.Log(logging.Entry{
Payload: fmt.Errorf("error fetching player count: %w", errCP),
Expand All @@ -27,7 +27,7 @@ func monitor(logger *logging.Logger, cfg config) {
continue
}

if errClose := r.Close(); errClose != nil {
if errClose := rcon.Close(); errClose != nil {
logger.Log(logging.Entry{
Payload: fmt.Errorf("error closing RCON: %w", errClose),
Severity: logging.Error,
Expand Down
6 changes: 3 additions & 3 deletions cmd/password.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
)

// mustGetPassword fetches the RCON password.
func mustGetPassword(l *logging.Logger) string {
func mustGetPassword(logger *logging.Logger) string {
bPassword, errRF := os.ReadFile("/opt/factorio/config/rconpw")
if errRF != nil {
l.Log(logging.Entry{
logger.Log(logging.Entry{
Payload: fmt.Errorf("error reading password file: %w", errRF),
Severity: logging.Critical,
})

if err := l.Flush(); err != nil {
if err := logger.Flush(); err != nil {
fmt.Fprintf(os.Stderr, "could not flush logger: %v", err)
}

Expand Down
20 changes: 10 additions & 10 deletions cmd/rcon.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,60 @@ import (
"time"

"cloud.google.com/go/logging"
rcon "github.com/gtaylor/factorio-rcon"
factorioRCON "github.com/gtaylor/factorio-rcon"
"github.com/jpillora/backoff"
)

var errPlaceholder = errors.New("placeholder")

// dialAndAuth creates the RCON client and authenticates with the server.
func dialAndAuth(logger *logging.Logger, cfg configRcon) *rcon.RCON {
func dialAndAuth(logger *logging.Logger, cfg configRcon) *factorioRCON.RCON {
// Set up exponential backoff
b := &backoff.Backoff{
backoff := &backoff.Backoff{
Min: cfg.BackoffMin,
Max: cfg.BackoffMax,
Factor: cfg.BackoffFactor,
Jitter: true,
}

var r *rcon.RCON
var rcon *factorioRCON.RCON

logger.Log(logging.Entry{Payload: fmt.Sprintf("Dialling '%s' and authing...", rconAddress)})

// Set placeholder errors before going into loop
errDial, errAuth := errPlaceholder, errPlaceholder

for errDial != nil || errAuth != nil {
r, errDial = rcon.Dial(rconAddress)
rcon, errDial = factorioRCON.Dial(rconAddress)
if errDial != nil {
logger.Log(logging.Entry{
Payload: fmt.Errorf("error dialling address '%s': %w", rconAddress, errDial),
Severity: logging.Error,
})
time.Sleep(b.Duration())
time.Sleep(backoff.Duration())

continue
}

errAuth = r.Authenticate(cfg.Password)
errAuth = rcon.Authenticate(cfg.Password)
if errAuth != nil {
logger.Log(logging.Entry{
Payload: fmt.Errorf("error authenticating to address '%s': %w", rconAddress, errAuth),
Severity: logging.Error,
})

if errClose := r.Close(); errClose != nil {
if errClose := rcon.Close(); errClose != nil {
logger.Log(logging.Entry{
Payload: fmt.Errorf("error closing RCON: %w", errClose),
Severity: logging.Error,
})
}

time.Sleep(b.Duration())
time.Sleep(backoff.Duration())
}
}

logger.Log(logging.Entry{Payload: "Online!"})

return r
return rcon
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"cloud.google.com/go/logging"
"github.com/davecgh/go-spew/spew"
"github.com/ilyakaznacheev/cleanenv"

"go.jlucktay.dev/version"
)

Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package main is the entrypoint for this tool.
package main

import (
Expand Down

0 comments on commit 8675154

Please sign in to comment.