Skip to content

Commit

Permalink
Update binary path
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmacdonald committed Dec 6, 2023
1 parent 2d0a63a commit 57e48a9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* text eol=lf
/frontend/.yarn/releases/** binary
/frontend/.yarn/plugins/** binary
*.dem binary
25 changes: 19 additions & 6 deletions pkg/demoparser/demo_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"encoding/json"
"io"
"io/fs"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -93,7 +94,7 @@ func Parse(ctx context.Context, demoPath string, info *DemoInfo) error {
return nil
}

func exists(path string) bool {
func Exists(path string) bool {
_, err := os.Stat(path)

return err == nil
Expand All @@ -102,7 +103,7 @@ func exists(path string) bool {
func ensureBinary(ctx context.Context) error {
fullPath := fullBinPath()

if exists(fullPath) {
if Exists(fullPath) {
return nil
}

Expand All @@ -124,7 +125,7 @@ func ensureBinary(ctx context.Context) error {

_ = resp.Body.Close()

openFile, err := os.OpenFile(fullPath, os.O_CREATE|os.O_RDWR|os.O_EXCL, 0x775)
openFile, err := os.OpenFile(fullPath, os.O_CREATE|os.O_RDWR|os.O_EXCL, 0x777)
if err != nil {
return errors.Wrap(err, "Failed to create new fd")
}
Expand All @@ -140,10 +141,22 @@ func ensureBinary(ctx context.Context) error {
return nil
}

func fullBinPath() string {
dir, _ := os.Getwd()
func appDir() string {
dir, err := os.UserHomeDir()
if err != nil {
panic(err)
}

fullDir := filepath.Join(dir, ".config", "parse_demo")
if errMkdir := os.MkdirAll(fullDir, fs.ModePerm); errMkdir != nil {
panic(errMkdir)
}

return fullDir
}

return filepath.Join(dir, binPath)
func fullBinPath() string {
return filepath.Join(appDir(), binPath)
}

func callBin(arg string) ([]byte, error) {
Expand Down
18 changes: 12 additions & 6 deletions pkg/demoparser/demo_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ import (
)

func TestParse(t *testing.T) {
path, _ := filepath.Abs("../../testdata/test.dem")
path, _ := filepath.Abs("testdata/test.dem")
if !demoparser.Exists(path) {
path, _ = filepath.Abs("../../testdata/test.dem")
if !demoparser.Exists(path) {
return
}
}

var info demoparser.DemoInfo

require.NoError(t, demoparser.Parse(context.Background(), path, &info))
require.Equal(t, 0, len(info.Chat))
require.Equal(t, 0, len(info.Deaths))
require.Equal(t, 0, len(info.Rounds))
require.Equal(t, 4, len(info.Users))
require.Equal(t, 25827, info.StartTick)
require.Equal(t, 20, len(info.Chat))
require.Equal(t, 243, len(info.Deaths))
require.Equal(t, 2, len(info.Rounds))
require.Equal(t, 45, len(info.Users))
require.Equal(t, 509, info.StartTick)
require.Equal(t, 0.015, info.IntervalPerTick)
}
Binary file modified testdata/test.dem
Binary file not shown.

0 comments on commit 57e48a9

Please sign in to comment.