Skip to content

Commit 8af04fa

Browse files
committed
Add 'info' command
1 parent 7ee6983 commit 8af04fa

File tree

4 files changed

+128
-16
lines changed

4 files changed

+128
-16
lines changed

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ Create a new project:
1616
codegame new
1717
```
1818

19+
Get information about a game server:
20+
```sh
21+
codegame info <url>
22+
```
23+
1924
Help:
2025
```sh
2126
codegame --help
@@ -24,13 +29,15 @@ codegame --help
2429
## Features
2530

2631
- View the CodeGame documentation
27-
- Automatic version detection and management
28-
- Create a new client
29-
- Go
30-
- Create a new game server
31-
- Go
32-
- Initialize Git
33-
- Create README and LICENSE files
32+
- View information about a game server
33+
- Automatic project setup
34+
- CodeGame version detection and management
35+
- Create a new client
36+
- Go
37+
- Create a new game server
38+
- Go
39+
- Initialize Git
40+
- Create README and LICENSE files
3441

3542
## Installation
3643

commands/info.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package commands
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io"
7+
"net/http"
8+
"strings"
9+
"unicode/utf8"
10+
11+
"github.com/code-game-project/codegame-cli/cli"
12+
"github.com/code-game-project/codegame-cli/external"
13+
"github.com/mattn/go-colorable"
14+
"github.com/ogier/pflag"
15+
)
16+
17+
type gameInfo struct {
18+
Name string `json:"name"`
19+
CGVersion string `json:"cg_version"`
20+
DisplayName string `json:"display_name"`
21+
Description string `json:"description"`
22+
Version string `json:"version"`
23+
RepositoryURL string `json:"repository_url"`
24+
}
25+
26+
func Info() error {
27+
var url string
28+
if pflag.NArg() >= 2 {
29+
url = strings.ToLower(pflag.Arg(1))
30+
} else {
31+
var err error
32+
url, err = cli.Input("Game server URL:")
33+
if err != nil {
34+
return err
35+
}
36+
}
37+
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, "/")
48+
49+
info, err := fetchInfo(url)
50+
if err != nil {
51+
return err
52+
}
53+
54+
printInfo(info)
55+
return nil
56+
}
57+
58+
func printInfo(info gameInfo) {
59+
out := colorable.NewColorableStdout()
60+
printInfoProperty(out, "Display Name", info.DisplayName, 17)
61+
printInfoProperty(out, "Name", info.Name, 17)
62+
printInfoProperty(out, "Description", info.Description, 17)
63+
printInfoProperty(out, "Version", info.Version, 17)
64+
printInfoProperty(out, "CodeGame Version", info.CGVersion, 17)
65+
printInfoProperty(out, "Repository", info.RepositoryURL, 17)
66+
}
67+
68+
func printInfoProperty(out io.Writer, name, value string, labelWidth int) {
69+
if value == "" {
70+
return
71+
}
72+
73+
label := name + ":"
74+
if labelWidth-utf8.RuneCountInString(label) > 0 {
75+
label += strings.Repeat(" ", labelWidth-utf8.RuneCountInString(label))
76+
}
77+
78+
fmt.Fprintf(out, "\x1b[36m%s\x1b[0m %s\n", label, value)
79+
}
80+
81+
func fetchInfo(url string) (gameInfo, error) {
82+
url = baseURL(url, isSSL(url)) + "/info"
83+
res, err := http.Get(url)
84+
if err != nil || res.StatusCode != http.StatusOK {
85+
return gameInfo{}, cli.Error("Couldn't access %s.", url)
86+
}
87+
if !external.HasContentType(res.Header, "application/json") {
88+
return gameInfo{}, cli.Error("%s doesn't return JSON.", url)
89+
}
90+
defer res.Body.Close()
91+
92+
var data gameInfo
93+
err = json.NewDecoder(res.Body).Decode(&data)
94+
if err != nil {
95+
return gameInfo{}, cli.Error("Couldn't decode /info data.")
96+
}
97+
98+
return data, nil
99+
}

commands/new.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func newServer(projectName string) error {
8181
language = strings.ToLower(pflag.Arg(2))
8282
} else {
8383
var err error
84-
language, err = cli.Select("In which language do you want to write your project?", []string{"Go"}, []string{"go"})
84+
language, err = cli.Select("Language:", []string{"Go"}, []string{"go"})
8585
if err != nil {
8686
return err
8787
}
@@ -98,7 +98,7 @@ func newServer(projectName string) error {
9898
}
9999

100100
func newClient(projectName string) error {
101-
url, err := cli.Input("Enter the URL of the game server:")
101+
url, err := cli.Input("Game server URL:")
102102
if err != nil {
103103
return err
104104
}
@@ -127,7 +127,7 @@ func newClient(projectName string) error {
127127
language = strings.ToLower(pflag.Arg(2))
128128
} else {
129129
var err error
130-
language, err = cli.Select("In which language do you want to write your project?", []string{"Go"}, []string{"go"})
130+
language, err = cli.Select("Language", []string{"Go"}, []string{"go"})
131131
if err != nil {
132132
return err
133133
}
@@ -157,7 +157,7 @@ func git(projectName string) error {
157157
return nil
158158
}
159159

160-
yes, err := cli.YesNo("Do you want to initialize git?", true)
160+
yes, err := cli.YesNo("Initialize git?", true)
161161
if err != nil {
162162
os.RemoveAll(projectName)
163163
return err
@@ -177,7 +177,7 @@ func git(projectName string) error {
177177
}
178178

179179
func readme(projectName string) error {
180-
yes, err := cli.YesNo("Create a README file?", true)
180+
yes, err := cli.YesNo("Create README?", true)
181181
if err != nil {
182182
os.RemoveAll(projectName)
183183
return err
@@ -220,7 +220,7 @@ var licenseApache string
220220
var licenseReadmeApache string
221221

222222
func license(projectName string) error {
223-
license, err := cli.Select("Select a license", []string{"None", "MIT", "GPLv3", "AGPL", "Apache 2.0"}, []string{"none", "MIT", "GPL", "AGPL", "Apache"})
223+
license, err := cli.Select("License", []string{"None", "MIT", "GPLv3", "AGPL", "Apache 2.0"}, []string{"none", "MIT", "GPL", "AGPL", "Apache"})
224224
if err != nil {
225225
os.RemoveAll(projectName)
226226
return err
@@ -315,9 +315,13 @@ func getCodeGameInfo(baseURL string) (string, string, error) {
315315
Name string `json:"name"`
316316
CGVersion string `json:"cg_version"`
317317
}
318-
res, err := http.Get(baseURL + "/info")
319-
if err != nil || res.StatusCode != http.StatusOK || !external.HasContentType(res.Header, "application/json") {
320-
return "", "", cli.Error("Couldn't access /info endpoint.")
318+
url := baseURL + "/info"
319+
res, err := http.Get(url)
320+
if err != nil || res.StatusCode != http.StatusOK {
321+
return "", "", cli.Error("Couldn't access %s.", url)
322+
}
323+
if !external.HasContentType(res.Header, "application/json") {
324+
return "", "", cli.Error("%s doesn't return JSON.", url)
321325
}
322326
defer res.Body.Close()
323327

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ func main() {
3636
switch command {
3737
case "new":
3838
err = commands.New()
39+
case "info":
40+
err = commands.Info()
3941
case "docs":
4042
err = external.OpenBrowser("https://docs.code-game.org")
4143
if err != nil {

0 commit comments

Comments
 (0)