forked from Southclaws/ScavengeSurvive
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved all the code into runner package and added an ensure function t…
…hat will populate an empty directory with the scavenge and survive code and compile it
- Loading branch information
1 parent
30289e4
commit 38700c5
Showing
6 changed files
with
127 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters