Skip to content
This repository has been archived by the owner on Jun 15, 2020. It is now read-only.

Commit

Permalink
Updated to v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
svigelj committed Feb 12, 2019
1 parent 1c3a2c4 commit 242a139
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 30 deletions.
2 changes: 1 addition & 1 deletion assets/RELEASE.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"tag":"v1.0.0","lastChecked":"2018-07-27T12:05:57+02:00"}
{"tag":"v1.0.1","lastChecked":"2018-07-27T12:05:57+02:00"}
2 changes: 1 addition & 1 deletion assets/cli-config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.0.0",
"version": "1.0.1",
"gamePort": 8887,
"languages": [
{
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"runtime"
)

const VERSION = "1.0.0"
const VERSION = "1.0.1"
const LiaHomePage = "https://liagame.com"

const SettingsFile = ".lia"
Expand Down
4 changes: 3 additions & 1 deletion internal/liaconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ type LiaConfig struct {
Language string `json:"language"`
}

func getConfig(path string) (*LiaConfig, error) {
func getConfig(botDir, path string) (*LiaConfig, error) {
configFile, err := ioutil.ReadFile(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Bot that you have provided does not seem like an actual bot. "+
"Please check if you have misspelled it or if it is missing the lia.json file. (bot dir = %s)\n", botDir)
fmt.Fprintf(os.Stderr, "Couldn't open file. Location: %s.", path)
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"github.com/liagame/lia-SDK/internal/config"
"os"
"os/exec"
"path/filepath"
)

func ShowReplayViewer(replayFile string, replayViewerWidth string) {
var args []string
if config.OperatingSystem == "darwin" {
args = append(args, "-XstartOnFirstThread", "-Dorg.lwjgl.system.allocator=system")
}
args = append(args, "-jar", config.PathToData+"/replay-viewer.jar")
args = append(args, "-jar", filepath.Join(config.PathToData, "replay-viewer.jar"))
if replayFile != "" {
args = append(args, replayFile)
}
Expand Down
49 changes: 29 additions & 20 deletions internal/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (
"github.com/liagame/lia-SDK/internal/config"
"github.com/liagame/lia-SDK/pkg/advancedcopy"
"github.com/mholt/archiver"
"github.com/pkg/errors"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"runtime"
"time"
"strings"
"strconv"
"strings"
"time"
)

const ReleaseRequestFailed = "failed"
Expand Down Expand Up @@ -112,23 +113,24 @@ func Update() {
}

fmt.Println("Replacing old data/ directory with a new one.")
pathToNewDataDir := filepath.Join(releaseDirPath, "/data")
pathToNewDataDir := filepath.Join(releaseDirPath, "data")

if err := advancedcopy.Dir(pathToNewDataDir, config.PathToData); err != nil {
fmt.Fprintf(os.Stderr, "Failed move new data dir from %s to %s. %s\n",
pathToNewDataDir, config.PathToData, err)
osExitStatus = lia_SDK.OsCallFailed
return
}

// Remove tmp directory
if err := os.RemoveAll(tmpReleaseParentDir); err != nil {
fmt.Fprintf(os.Stderr, "failed to remove tmp dir %s, error: %s\n", tmpReleaseParentDir, err)
}
defer func() {
if err := os.RemoveAll(tmpReleaseParentDir); err != nil {
fmt.Fprintf(os.Stderr, "failed to remove tmp dir %s, error: %s\n", tmpReleaseParentDir, err)
}
}()

fmt.Println("Replacing lia executable.")

pathToNewLiaExecutable := releaseDirPath + "/lia"
pathToNewLiaExecutable := filepath.Join(releaseDirPath, "lia")
if runtime.GOOS == "windows" {
pathToNewLiaExecutable += ".exe"
}
Expand All @@ -148,6 +150,12 @@ func Update() {
}

fmt.Println("Lia was updated sucessfully!")

if runtime.GOOS == "windows" {
fmt.Println()
fmt.Println("NOTE: You can safely delete lia.exe.old file.")
fmt.Println()
}
}

func getReleaseZipUrl(latestTag string) string {
Expand Down Expand Up @@ -207,8 +215,9 @@ func timeToCheckForUpdate() (bool, error) {
}

func isUpdateAvailable() (string, bool) {
latestTag := getLatestReleaseTag()
if latestTag == ReleaseRequestFailed {
latestTag, err := getLatestReleaseTag()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get latest release. Error: %v\n", err)
return "", false
}

Expand All @@ -217,12 +226,12 @@ func isUpdateAvailable() (string, bool) {
return "", false
}

larger := isNewUpdateLargerTanCurrent(latestTag, localRelease.Tag)
larger := isNewUpdateLargerThanCurrent(latestTag, localRelease.Tag)

return latestTag, larger
}

func isNewUpdateLargerTanCurrent(new, current string) bool {
func isNewUpdateLargerThanCurrent(new, current string) bool {
new = strings.TrimPrefix(new, "v")
current = strings.TrimPrefix(current, "v")

Expand Down Expand Up @@ -306,45 +315,45 @@ func getLocalReleaseConfig() (*ReleaseConfig, error) {
return cfg, nil
}

func getLatestReleaseTag() string {
func getLatestReleaseTag() (string, error) {
var client = &http.Client{
Timeout: time.Second * 30,
}

url := config.ReleasesUrl
req, err := http.NewRequest("GET", url, nil)
if err != nil {
os.Exit(lia_SDK.Generic)
return "", err
}
req.Header.Add("Accept", "application/json")

res, err := client.Do(req)
if err != nil {
return ReleaseRequestFailed
return "", err
}
defer res.Body.Close()

if res.StatusCode != 200 {
os.Exit(lia_SDK.FailedToGetLatestRelease)
return "", errors.New("Response status " + res.Status)
}

// Convert body to bytes
body, err := ioutil.ReadAll(res.Body)
if err != nil {
os.Exit(lia_SDK.FailedToGetLatestRelease)
return "", err
}

// Get tag
var objmap map[string]*json.RawMessage
if err := json.Unmarshal(body, &objmap); err != nil {
os.Exit(lia_SDK.FailedToGetLatestRelease)
return "", err
}
var tag string
if err := json.Unmarshal(*objmap["tag_name"], &tag); err != nil {
os.Exit(lia_SDK.FailedToGetLatestRelease)
return "", err
}

return tag
return tag, nil
}

func printNewUpdateAvailableNotification(latestTag string) {
Expand Down
8 changes: 4 additions & 4 deletions internal/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

func TestCompareVersions(t *testing.T) {
cases := []struct {
tag1 string
tag2 string
tag1 string
tag2 string
isTag1Newer bool
}{
{"v1.0.0", "v1.0.0", false},
Expand All @@ -24,10 +24,10 @@ func TestCompareVersions(t *testing.T) {

// Run actual tests
for i, c := range cases {
r := isNewUpdateLargerTanCurrent(c.tag1, c.tag2)
r := isNewUpdateLargerThanCurrent(c.tag1, c.tag2)
if r != c.isTag1Newer {
t.Fatalf("%d: Wrong result for tag1=%s, tag2=%s, should be %v but is %v\n",
i, c.tag1, c.tag2, c.isTag1Newer, r)
}
}
}
}
2 changes: 1 addition & 1 deletion internal/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func GetBotLanguage(botDir string) (*config.Language, error) {
botConfigPath := filepath.Join(botDir, "lia.json")

liaConfig, err := getConfig(botConfigPath)
liaConfig, err := getConfig(botDir, botConfigPath)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to read %s\n", botConfigPath)
return nil, err
Expand Down

0 comments on commit 242a139

Please sign in to comment.