Skip to content

Commit c689f19

Browse files
committed
Implement debug command
1 parent 9c97a8e commit c689f19

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

cmd/debug.go

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

0 commit comments

Comments
 (0)