Skip to content

Commit 5080597

Browse files
committed
Implement 'change-url' command
1 parent e95e2de commit 5080597

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ Update event definitions, wrappers and libraries to match the latest game versio
2121
codegame update
2222
```
2323

24+
Permanently switch to a different game URL:
25+
```sh
26+
codegame change-url <new_url>
27+
```
28+
2429
Run a project:
2530
```sh
2631
codegame run

commands/change_url.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package commands
2+
3+
import (
4+
"os"
5+
"strings"
6+
7+
"github.com/Bananenpro/cli"
8+
"github.com/code-game-project/codegame-cli/util/cgfile"
9+
)
10+
11+
func ChangeURL() error {
12+
root, err := cgfile.FindProjectRoot()
13+
if err != nil {
14+
return err
15+
}
16+
17+
var url string
18+
if len(os.Args) >= 3 {
19+
url = strings.ToLower(os.Args[2])
20+
} else {
21+
var err error
22+
url, err = cli.Input("New game URL:")
23+
if err != nil {
24+
return err
25+
}
26+
}
27+
28+
config, err := cgfile.LoadCodeGameFile(root)
29+
if err != nil {
30+
return err
31+
}
32+
33+
if config.Type != "client" {
34+
return cli.Error("Project is not a client.")
35+
}
36+
37+
name, _, err := getCodeGameInfo(baseURL(url))
38+
if err != nil {
39+
return err
40+
}
41+
if name != config.Game {
42+
return cli.Error("The URL points to a different game.")
43+
}
44+
45+
prevURL := config.URL
46+
47+
config.URL = url
48+
err = config.Write(root)
49+
if err != nil {
50+
return err
51+
}
52+
53+
err = Update()
54+
if err != nil {
55+
config.URL = prevURL
56+
config.Write(root)
57+
return err
58+
}
59+
return nil
60+
}

main.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ func main() {
2424
fmt.Fprintln(os.Stderr, "\nDescription:")
2525
fmt.Fprintln(os.Stderr, "The official CodeGame CLI.")
2626
fmt.Fprintln(os.Stderr, "\nCommands:")
27-
fmt.Fprintln(os.Stderr, "\tnew \tCreate a new project.")
28-
fmt.Fprintln(os.Stderr, "\tupdate \tUpdate the current project.")
29-
fmt.Fprintln(os.Stderr, "\trun \tRun the current project.")
30-
fmt.Fprintln(os.Stderr, "\tbuild \tBuild the current project.")
31-
fmt.Fprintln(os.Stderr, "\tinfo \tDisplay some info about a game server.")
32-
fmt.Fprintln(os.Stderr, "\tdocs \tOpen the CodeGame documentation in a web browser.")
27+
fmt.Fprintln(os.Stderr, " new Create a new project.")
28+
fmt.Fprintln(os.Stderr, " update Update the current project.")
29+
fmt.Fprintln(os.Stderr, " change-url Permanently switch to a different game URL.")
30+
fmt.Fprintln(os.Stderr, " run Run the current project.")
31+
fmt.Fprintln(os.Stderr, " build Build the current project.")
32+
fmt.Fprintln(os.Stderr, " info Display some info about a game server.")
33+
fmt.Fprintln(os.Stderr, " docs Open the CodeGame documentation in a web browser.")
3334
fmt.Fprintln(os.Stderr, "\nAbout: https://code-game.org")
3435
fmt.Fprintln(os.Stderr, "Copyright (c) 2022 CodeGame Contributors (https://code-game.org/contributors)")
3536
pflag.PrintDefaults()
@@ -50,6 +51,8 @@ func main() {
5051
err = commands.New()
5152
case "update":
5253
err = commands.Update()
54+
case "change-url":
55+
err = commands.ChangeURL()
5356
case "info":
5457
err = commands.Info()
5558
case "docs":

0 commit comments

Comments
 (0)