-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.go
More file actions
96 lines (79 loc) · 2.59 KB
/
Copy pathdebug.go
File metadata and controls
96 lines (79 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package cmd
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/Bananenpro/cli"
"github.com/adrg/xdg"
"github.com/code-game-project/go-utils/exec"
"github.com/code-game-project/go-utils/external"
"github.com/code-game-project/go-utils/semver"
"github.com/code-game-project/go-utils/server"
"github.com/spf13/cobra"
)
var cgDebugPath = filepath.Join(xdg.DataHome, "codegame", "bin", "cg-debug")
// debugCmd represents the debug command
var debugCmd = &cobra.Command{
Use: "debug",
Short: "View debug logs of a game server.",
Args: cobra.RangeArgs(0, 1),
Run: func(cmd *cobra.Command, args []string) {
var url string
var err error
if len(args) == 0 {
url, err = cli.Input("Game server URL:")
if err != nil {
return
}
} else {
url = args[0]
}
api, err := server.NewAPI(url)
if err != nil {
abort(fmt.Errorf("%s is not a CodeGame game server.", external.TrimURL(url)))
}
info, err := api.FetchGameInfo()
abortf("Failed to fetch game info: %s", err)
version, err := findDebugVersion(info.CGVersion)
abortf("Failed to determine the correct cg-debug version to use: %s", err)
exeName, err := installDebug(version)
_, err = exec.Execute(false, filepath.Join(cgDebugPath, exeName), url)
if err != nil {
os.Exit(1)
}
},
}
func findDebugVersion(cgVersion string) (string, error) {
if cgVersion == "latest" {
version, err := external.LatestGithubTag("code-game-project", "cg-debug")
return strings.TrimPrefix(version, "v"), err
}
res, err := external.LoadVersionsJSON("code-game-project", "cg-debug")
if err != nil {
cli.Warn("Couldn't fetch versions.json. Using latest cg-debug version.")
version, err := external.LatestGithubTag("code-game-project", "cg-debug")
return strings.TrimPrefix(version, "v"), err
}
var versions map[string]string
err = json.Unmarshal(res, &versions)
if err != nil {
cli.Warn("Invalid versions.json. Using latest cg-debug version.")
version, err := external.LatestGithubTag("code-game-project", "cg-debug")
return strings.TrimPrefix(version, "v"), err
}
v := semver.CompatibleVersion(versions, cgVersion)
if v == "latest" {
version, err := external.LatestGithubTag("code-game-project", "cg-debug")
return strings.TrimPrefix(version, "v"), err
}
v, err = external.GithubTagFromVersion("code-game-project", "cg-debug", v)
return strings.TrimPrefix(v, "v"), err
}
func installDebug(version string) (string, error) {
return external.InstallProgram("cg-debug", "cg-debug", "https://github.com/code-game-project/cg-debug", version, cgDebugPath)
}
func init() {
rootCmd.AddCommand(debugCmd)
}