Skip to content

Commit 910e63a

Browse files
committed
Add 'docs <game_url>' command
1 parent 61df503 commit 910e63a

File tree

9 files changed

+112
-25
lines changed

9 files changed

+112
-25
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ Get information about a game server:
2121
codegame info <url>
2222
```
2323

24+
View the documentation of a game:
25+
```sh
26+
codegame docs <url>
27+
```
28+
2429
Help:
2530
```sh
2631
codegame --help

commands/docs.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package commands
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
7+
"github.com/code-game-project/codegame-cli/cli"
8+
"github.com/code-game-project/codegame-cli/external"
9+
"github.com/gomarkdown/markdown"
10+
"github.com/gomarkdown/markdown/html"
11+
"github.com/gomarkdown/markdown/parser"
12+
"github.com/ogier/pflag"
13+
14+
_ "embed"
15+
)
16+
17+
//go:embed templates/css/docs.css
18+
var docsStyle string
19+
20+
func Docs() error {
21+
if pflag.NArg() == 1 {
22+
cli.Begin("Opening documentation...")
23+
err := external.OpenBrowser("https://docs.code-game.org")
24+
if err != nil {
25+
cli.Error(err.Error())
26+
}
27+
cli.Finish()
28+
return err
29+
}
30+
31+
cli.Begin("Generating markdown documentation...")
32+
33+
url := trimURL(pflag.Arg(1))
34+
url = baseURL(url, isSSL(url))
35+
36+
cgeVersion, err := external.GetCGEVersion(url)
37+
if err != nil {
38+
return err
39+
}
40+
41+
err = external.CGGenEvents(os.TempDir(), url, cgeVersion, "markdown")
42+
if err != nil {
43+
return err
44+
}
45+
46+
cli.Finish()
47+
48+
cli.Begin("Converting documentation to HTML...")
49+
50+
md, err := os.ReadFile(filepath.Join(os.TempDir(), "event_docs.md"))
51+
if err != nil {
52+
return cli.Error(err.Error())
53+
}
54+
55+
md = markdown.NormalizeNewlines(md)
56+
text := markdown.ToHTML(md, parser.NewWithExtensions(parser.CommonExtensions|parser.AutoHeadingIDs), html.NewRenderer(html.RendererOptions{
57+
CSS: filepath.Join(os.TempDir(), "event_docs.css"),
58+
Flags: html.CommonFlags | html.CompletePage,
59+
}))
60+
61+
err = os.WriteFile(filepath.Join(os.TempDir(), "event_docs.html"), text, 0644)
62+
if err != nil {
63+
return cli.Error(err.Error())
64+
}
65+
66+
err = os.WriteFile(filepath.Join(os.TempDir(), "event_docs.css"), []byte(docsStyle), 0644)
67+
if err != nil {
68+
return cli.Error(err.Error())
69+
}
70+
71+
cli.Finish()
72+
73+
cli.Begin("Opening documentation...")
74+
75+
err = external.OpenBrowser(filepath.Join(os.TempDir(), "event_docs.html"))
76+
if err != nil {
77+
cli.Error(err.Error())
78+
}
79+
80+
cli.Finish()
81+
82+
return nil
83+
}

commands/info.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"net/http"
8+
"os"
89
"strings"
910
"unicode/utf8"
1011

@@ -35,23 +36,16 @@ func Info() error {
3536
}
3637
}
3738

38-
if strings.HasPrefix(url, "http://") {
39-
url = strings.TrimPrefix(url, "http://")
40-
} else if strings.HasPrefix(url, "https://") {
41-
url = strings.TrimPrefix(url, "https://")
42-
} else if strings.HasPrefix(url, "ws://") {
43-
url = strings.TrimPrefix(url, "ws://")
44-
} else if strings.HasPrefix(url, "wss://") {
45-
url = strings.TrimPrefix(url, "wss://")
46-
}
47-
url = strings.TrimSuffix(url, "/")
39+
url = trimURL(url)
4840

4941
info, err := fetchInfo(url)
5042
if err != nil {
5143
return err
5244
}
5345

5446
printInfo(info)
47+
48+
cli.Info("\nTo view the documentation of this game run:\n%s docs %s", os.Args[0], url)
5549
return nil
5650
}
5751

commands/new.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,7 @@ func newClient(projectName string) error {
101101
if err != nil {
102102
return err
103103
}
104-
if strings.HasPrefix(url, "http://") {
105-
url = strings.TrimPrefix(url, "http://")
106-
} else if strings.HasPrefix(url, "https://") {
107-
url = strings.TrimPrefix(url, "https://")
108-
} else if strings.HasPrefix(url, "ws://") {
109-
url = strings.TrimPrefix(url, "ws://")
110-
} else if strings.HasPrefix(url, "wss://") {
111-
url = strings.TrimPrefix(url, "wss://")
112-
}
113-
url = strings.TrimSuffix(url, "/")
104+
url = trimURL(url)
114105
ssl := isSSL(url)
115106
name, cgVersion, err := getCodeGameInfo(baseURL(url, ssl))
116107
if err != nil {
@@ -363,6 +354,19 @@ func execTemplate(templateText, path string, data any) error {
363354
return tmpl.Execute(file, data)
364355
}
365356

357+
func trimURL(url string) string {
358+
if strings.HasPrefix(url, "http://") {
359+
url = strings.TrimPrefix(url, "http://")
360+
} else if strings.HasPrefix(url, "https://") {
361+
url = strings.TrimPrefix(url, "https://")
362+
} else if strings.HasPrefix(url, "ws://") {
363+
url = strings.TrimPrefix(url, "ws://")
364+
} else if strings.HasPrefix(url, "wss://") {
365+
url = strings.TrimPrefix(url, "wss://")
366+
}
367+
return strings.TrimSuffix(url, "/")
368+
}
369+
366370
func baseURL(domain string, ssl bool) string {
367371
if ssl {
368372
return "https://" + domain

commands/templates/css/docs.css

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

external/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/code-game-project/codegame-cli/cli"
1313
)
1414

15-
const currentVersion = "0.4.1"
15+
const currentVersion = "0.5.0"
1616

1717
func CheckVersion() {
1818
latest, err := getLatestVersion()

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.18
55
require (
66
github.com/AlecAivazis/survey/v2 v2.3.4
77
github.com/adrg/xdg v0.4.0
8+
github.com/gomarkdown/markdown v0.0.0-20220603122033-8f3b341fef32
89
github.com/mattn/go-colorable v0.1.12
910
github.com/ogier/pflag v0.0.1
1011
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr
99
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1010
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1111
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12+
github.com/gomarkdown/markdown v0.0.0-20220603122033-8f3b341fef32 h1:QxcGJpbMJw6tHRtrHKJiL11LdX1SXDfV1f4t4mJl3QI=
13+
github.com/gomarkdown/markdown v0.0.0-20220603122033-8f3b341fef32/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
1214
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog=
1315
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68=
1416
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=

main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ func main() {
4141
case "info":
4242
err = commands.Info()
4343
case "docs":
44-
err = external.OpenBrowser("https://docs.code-game.org")
45-
if err != nil {
46-
cli.Error(err.Error())
47-
}
44+
err = commands.Docs()
4845
default:
4946
cli.Error("Unknown command: %s", strings.ToLower(pflag.Arg(0)))
5047
pflag.Usage()

0 commit comments

Comments
 (0)