Skip to content

Commit

Permalink
moved all the code into runner package and added an ensure function t…
Browse files Browse the repository at this point in the history
…hat will populate an empty directory with the scavenge and survive code and compile it
  • Loading branch information
Southclaws committed Oct 18, 2020
1 parent 30289e4 commit 38700c5
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 76 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ go 1.14

require (
github.com/Southclaws/sampctl v0.0.0-20201015215605-f6e02560637f
github.com/go-errors/errors v1.1.1
github.com/google/go-github v0.0.0-20180819205025-d7732128a00e
github.com/google/go-github/v28 v28.1.1
github.com/joho/godotenv v1.3.0
github.com/kr/pretty v0.2.0
github.com/pkg/errors v0.9.1
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/go-errors/errors v1.1.1 h1:ljK/pL5ltg3qoN+OtN6yCv9HWSfMwxSx90GJCZQxYNg=
github.com/go-errors/errors v1.1.1/go.mod h1:psDX2osz5VnTOnFWbDeWwS7yejl+uV3FEWEp4lssFEs=
github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4=
github.com/go-git/gcfg v1.5.0/go.mod h1:5m20vg6GwYabIxaOonVkTdrILxQMpEShl1xiMF4ua+E=
github.com/go-git/go-billy/v5 v5.0.0 h1:7NQHvd9FVid8VL4qVUMm8XifBK+2xCoZ2lSk0agRrHM=
Expand All @@ -60,6 +62,9 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-github v0.0.0-20180819205025-d7732128a00e h1:9POI/RDmrtowShQBeQQyCSgRHNphR9V5R1wl2b/aXU4=
github.com/google/go-github v0.0.0-20180819205025-d7732128a00e/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
github.com/google/go-github/v28 v28.1.1 h1:kORf5ekX5qwXO2mGzXXOjMe/g6ap8ahVe0sBEulhSxo=
github.com/google/go-github/v28 v28.1.1/go.mod h1:bsqJWQX05omyWVmc00nEUql9mhQyv38lDZ8kPZcQVoM=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
Expand Down Expand Up @@ -156,9 +161,11 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2 h1:eDrdRpKgkcCqKZQwyZRyeFZgfqt37SL7Kv3tok06cKE=
golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand All @@ -181,6 +188,7 @@ golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
Expand Down
78 changes: 3 additions & 75 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,102 +1,30 @@
package main

import (
"context"
"fmt"
"os"
"os/signal"
"runtime"
"strconv"

"github.com/Southclaws/ScavengeSurvive/runner"
"github.com/Southclaws/sampctl/download"
"github.com/Southclaws/sampctl/pkgcontext"
"github.com/google/go-github/github"
"github.com/joho/godotenv"
"github.com/pkg/errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

func main() {

if err := run(); err != nil {
if err := runner.Run(); err != nil {
zap.L().Info("unexpected exit", zap.String("error", err.Error()))
}
zap.L().Info("exited gracefully")
}

func run() error {
zap.L().Info("scavenge and survive runner initialising")

dir, err := os.Getwd()
if err != nil {
return err
}

cacheDir, err := download.GetCacheDir()
if err != nil {
return err
}

gh := github.NewClient(nil)

pcx, err := pkgcontext.NewPackageContext(gh, nil, true, dir, runtime.GOOS, cacheDir, "")
if err != nil {
return errors.Wrap(err, "failed to interpret directory as Pawn package")
}

// pcx.Runtime = runtimeName
pcx.CacheDir = cacheDir
// pcx.BuildName = build
pcx.ForceBuild = false
pcx.ForceEnsure = false
pcx.BuildFile = "BUILD_NUMBER"
pcx.Relative = true

if err := pcx.RunPrepare(context.Background()); err != nil {
return errors.Wrap(err, "failed to prepare runtime")
}

zap.L().Info("prepared runtime environment")

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go runner.Run(ctx, os.Stdin, os.Stdout)

zap.L().Info("awaiting signals, cancellations or fatal errors")

f := func() error {
select {
case s := <-sigs:
return errors.Errorf("signal received: %s", s.String())

case <-ctx.Done():
return context.Canceled

default:
return nil
}
}

for {
if err := f(); err != nil {
return err
}
}
}

func init() {
//nolint:errcheck
godotenv.Load()

prod, err := strconv.ParseBool(os.Getenv("PRODUCTION"))
if err != nil {
fmt.Println(err)
if _, ok := err.(*strconv.NumError); !ok {
fmt.Println("Failed to parse environment variable `PRODUCTION`", os.Getenv("PRODUCTION"), "error:", err)
os.Exit(1)
}

Expand Down
24 changes: 24 additions & 0 deletions runner/ensure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package runner

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

func Ensure() error {
if err := exec.Command("git", "clone", "https://github.com/Southclaws/ScavengeSurvive", ".").Run(); err != nil {
return err
}

return nil
}

func isDirEmpty(dir string) bool {
d, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
}
fmt.Println(d)
return len(d) == 0
}
89 changes: 89 additions & 0 deletions runner/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package runner

import (
"context"
"os"
"os/signal"
"runtime"

"github.com/Southclaws/sampctl/download"
"github.com/Southclaws/sampctl/pkgcontext"
"github.com/google/go-github/github"
"github.com/pkg/errors"
"go.uber.org/zap"
)

func Run() error {
zap.L().Info("scavenge and survive runner initialising")

dir, err := os.Getwd()
if err != nil {
return errors.Wrap(err, "failed to get current working directory")
}

forceBuild := false
forceEnsure := false
if isDirEmpty(dir) {
zap.L().Info("Current directory is empty, cloning new copy of Scavenge and Survive")
if err := Ensure(); err != nil {
return errors.Wrap(err, "failed to ensure")
}
forceBuild = true
forceEnsure = true
}

cacheDir, err := download.GetCacheDir()
if err != nil {
return errors.Wrap(err, "failed to get cache directory")
}

gh := github.NewClient(nil)

pcx, err := pkgcontext.NewPackageContext(gh, nil, true, dir, runtime.GOOS, cacheDir, "")
if err != nil {
return errors.Wrap(err, "failed to interpret directory as Pawn package")
}

// pcx.Runtime = runtimeName
pcx.CacheDir = cacheDir
// pcx.BuildName = build
pcx.ForceBuild = forceBuild
pcx.ForceEnsure = forceEnsure
pcx.BuildFile = "BUILD_NUMBER"
pcx.Relative = true

if err := pcx.RunPrepare(context.Background()); err != nil {
return errors.Wrap(err, "failed to prepare runtime")
}

zap.L().Info("prepared runtime environment")

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go RunServer(ctx, os.Stdin, os.Stdout)

zap.L().Info("awaiting signals, cancellations or fatal errors")

f := func() error {
select {
case s := <-sigs:
return errors.Errorf("signal received: %s", s.String())

case <-ctx.Done():
return context.Canceled

default:
return nil
}
}

for {
if err := f(); err != nil {
return err
}
}
}
2 changes: 1 addition & 1 deletion runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const ExitPattern = `[OnScriptExit] LAST_EXIT`

var PluginPattern = regexp.MustCompile(`Loading plugin:\s(\w+)`)

func Run(ctx context.Context, r io.Reader, w io.Writer) {
func RunServer(ctx context.Context, r io.Reader, w io.Writer) {
zap.L().Info("starting blocking process")

// a signaller that uses logs to understand when the gamemode is restarting
Expand Down

0 comments on commit 38700c5

Please sign in to comment.