Skip to content

Commit 4cc448d

Browse files
committed
Send config to modules as a temporary JSON file
1 parent 6bf74f0 commit 4cc448d

File tree

12 files changed

+310
-109
lines changed

12 files changed

+310
-109
lines changed

README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,93 @@ cd codegame-cli
140140
go build .
141141
```
142142

143+
## Modules
144+
145+
Language specific functionality is provided by modules.
146+
*codegame-cli* automatically downloads the correct module version corresponding to the CodeGame version and
147+
executes the downloaded binary at the root of the project directory with the respective command.
148+
149+
Additionally, the _CONFIG_FILE_ environment variable is set with the path to a temporary file containing command specific configuration in JSON format.
150+
151+
Modules can be implemented in any language. However, it is recommended to write them in Go
152+
using the `github.com/code-game-project/codegame-cli/pkg/*` packages and the `github.com/Bananenpro/cli` package for CLI interaction in order to be consistent with the CLI and other modules.
153+
154+
### new
155+
156+
Creates a new project of the type provided by a second command line argument:
157+
158+
#### client
159+
160+
Creates a new game client.
161+
This includes integration with the client library for the language, a functioning template with a main file and language specific metadata files like `package.json` or similar
162+
and wrappers around the library to make its usage easier including setting the game URL with the CG_GAME_URL environment variable (required).
163+
164+
##### config data
165+
166+
```jsonc
167+
{
168+
"lang": "go", // the chosen programming language (in case one module supports multiple languages)
169+
"name": "my_game", // the name of the game
170+
"url": "my_game.example.com", // the URL of the game server
171+
"library_version": "0.9.2" // the version of the client library to use
172+
}
173+
```
174+
175+
#### server
176+
177+
Creates a new game server.
178+
This includes integration with the server library of the language and a functioning template, which implements common logic like starting the server and providing a game class.
179+
180+
##### config data
181+
182+
```jsonc
183+
{
184+
"lang": "go", // the chosen programming language (in case one module supports multiple languages)
185+
"library_version": "0.9.2" // the version of the server library to use
186+
}
187+
```
188+
189+
### update
190+
191+
Updates the library to the specified version and all other dependencies used by the project to their newest compatible version.
192+
Additionally all wrappers are updated.
193+
194+
##### config data
195+
196+
```jsonc
197+
{
198+
"lang": "go", // the chosen programming language (in case one module supports multiple languages)
199+
"library_version": "0.9.2" // the new version of the library to use
200+
}
201+
202+
```
203+
204+
### run
205+
206+
Runs the project with the specified command line arguments.
207+
208+
##### config data
209+
210+
```jsonc
211+
{
212+
"lang": "go", // the chosen programming language (in case one module supports multiple languages)
213+
"args": ["-f", "my_file.txt"] // the command line arguments for the application
214+
}
215+
```
216+
217+
### build
218+
219+
Builds the projects and injects the URL specified in the `.codegame.json` file, which makes the *CG_GAME_URL* environment variable optional.
220+
221+
##### config data
222+
223+
```jsonc
224+
{
225+
"lang": "go", // the chosen programming language (in case one module supports multiple languages)
226+
"output": "bin/my-game" // The name of the output file
227+
}
228+
```
229+
143230
## License
144231

145232
Copyright (c) 2022 CodeGame Contributors (https://code-game.org/contributors)

cmd/build.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,42 @@
1-
/*
2-
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
3-
4-
*/
51
package cmd
62

73
import (
84
"fmt"
5+
"os"
96

107
"github.com/code-game-project/codegame-cli/pkg/cgfile"
8+
"github.com/code-game-project/codegame-cli/pkg/external"
119
"github.com/code-game-project/codegame-cli/pkg/modules"
1210
"github.com/spf13/cobra"
1311
)
1412

1513
// buildCmd represents the build command
1614
var buildCmd = &cobra.Command{
17-
Use: "build",
18-
Short: "Build the current project.",
19-
DisableFlagParsing: true,
15+
Use: "build",
16+
Short: "Build the current project.",
2017
Run: func(cmd *cobra.Command, args []string) {
21-
rootRelative, err := cgfile.FindProjectRootRelative()
18+
root, err := cgfile.FindProjectRoot()
19+
abort(err)
20+
err = os.Chdir(root)
2221
abort(err)
2322

24-
data, err := cgfile.LoadCodeGameFile(rootRelative)
23+
data, err := cgfile.LoadCodeGameFile("")
2524
abortf("failed to load .codegame.json: %w", err)
25+
data.URL = external.TrimURL(data.URL)
26+
abort(data.Write(""))
2627

2728
cmdArgs := []string{"build"}
2829
cmdArgs = append(cmdArgs, args...)
2930

31+
output, err := cmd.Flags().GetString("output")
32+
abort(err)
33+
buildData := modules.BuildData{
34+
Lang: data.Lang,
35+
Output: output,
36+
}
3037
switch data.Lang {
3138
case "go":
32-
err = modules.Execute("go", "latest", data.Type, cmdArgs...)
39+
err = modules.ExecuteBuild(buildData, data)
3340
abort(err)
3441
default:
3542
abort(fmt.Errorf("'build' is not supported for '%s'", data.Lang))
@@ -39,4 +46,5 @@ var buildCmd = &cobra.Command{
3946

4047
func init() {
4148
rootCmd.AddCommand(buildCmd)
49+
buildCmd.Flags().StringP("output", "o", "", "The name of the output file.")
4250
}

cmd/changeUrl.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
/*
2-
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
3-
4-
*/
51
package cmd
62

73
import (
84
"errors"
9-
"os"
105
"strings"
116

127
"github.com/Bananenpro/cli"
138
"github.com/code-game-project/codegame-cli/pkg/cgfile"
9+
"github.com/code-game-project/codegame-cli/pkg/external"
1410
"github.com/code-game-project/codegame-cli/pkg/server"
1511
"github.com/spf13/cobra"
1612
)
@@ -25,8 +21,8 @@ var changeUrlCmd = &cobra.Command{
2521
abort(err)
2622

2723
var url string
28-
if len(os.Args) >= 3 {
29-
url = strings.ToLower(os.Args[2])
24+
if len(args) > 0 {
25+
url = strings.ToLower(args[0])
3026
} else {
3127
var err error
3228
url, err = cli.Input("New game URL:")
@@ -50,7 +46,7 @@ var changeUrlCmd = &cobra.Command{
5046

5147
prevURL := config.URL
5248

53-
config.URL = url
49+
config.URL = external.TrimURL(url)
5450
err = config.Write(root)
5551
abort(err)
5652

cmd/info.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/*
2-
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
3-
4-
*/
51
package cmd
62

73
import (

cmd/new.go

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/*
2-
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
3-
4-
*/
51
package cmd
62

73
import (
@@ -22,7 +18,6 @@ import (
2218
"github.com/code-game-project/codegame-cli/pkg/exec"
2319
"github.com/code-game-project/codegame-cli/pkg/external"
2420
"github.com/code-game-project/codegame-cli/pkg/modules"
25-
"github.com/code-game-project/codegame-cli/pkg/semver"
2621
"github.com/code-game-project/codegame-cli/pkg/server"
2722
"github.com/spf13/cobra"
2823
)
@@ -89,36 +84,31 @@ func init() {
8984
}
9085

9186
func newServer(projectName string) error {
92-
var language string
93-
if len(os.Args) >= 4 {
94-
language = strings.ToLower(os.Args[3])
95-
} else {
96-
var err error
97-
language, err = cli.SelectString("Language:", []string{"Go", "JavaScript", "TypeScript"}, []string{"go", "js", "ts"})
98-
if err != nil {
99-
return err
100-
}
87+
language, err := cli.SelectString("Language:", []string{"Go", "JavaScript", "TypeScript"}, []string{"go", "js", "ts"})
88+
if err != nil {
89+
return err
10190
}
10291

10392
file := cgfile.CodeGameFileData{
10493
Game: projectName,
10594
Type: "server",
10695
Lang: language,
10796
}
108-
err := file.Write("")
97+
err = file.Write("")
10998
if err != nil {
110-
return fmt.Errorf("Failed to create .codegame.json: %s", err)
99+
return fmt.Errorf("Failed to create .codegame.json: %w", err)
100+
}
101+
102+
newData := modules.NewServerData{
103+
Lang: language,
104+
LibraryVersion: "latest",
111105
}
112106

113107
switch language {
114-
case "go":
115-
err = modules.Execute("go", "latest", "server", "new", "server")
116-
case "js":
117-
err = modules.Execute("js", "latest", "server", "new", "server")
118-
case "ts":
119-
err = modules.Execute("js", "latest", "server", "new", "server", "--typescript")
108+
case "go", "js", "ts":
109+
err = modules.ExecuteNewServer(newData)
120110
default:
121-
return fmt.Errorf("'new server' is not supported for '%s'", language)
111+
err = fmt.Errorf("'new server' is not supported for '%s'", language)
122112
}
123113
if err != nil {
124114
return err
@@ -156,6 +146,7 @@ func newClient() error {
156146
if err != nil {
157147
return err
158148
}
149+
url = external.TrimURL(url)
159150
api, err := server.NewAPI(url)
160151
if err != nil {
161152
return err
@@ -173,18 +164,7 @@ func newClient() error {
173164
return err
174165
}
175166

176-
var language string
177-
if len(os.Args) >= 4 {
178-
language = strings.ToLower(os.Args[3])
179-
} else {
180-
var err error
181-
language, err = cli.SelectString("Language:", []string{"Go", "JavaScript", "TypeScript"}, []string{"go", "js", "ts"})
182-
if err != nil {
183-
return err
184-
}
185-
}
186-
187-
cgeMajor, cgeMinor, _, err := semver.ParseVersion(cgeVersion)
167+
language, err := cli.SelectString("Language:", []string{"Go", "JavaScript", "TypeScript"}, []string{"go", "js", "ts"})
188168
if err != nil {
189169
return err
190170
}
@@ -193,25 +173,28 @@ func newClient() error {
193173
Game: info.Name,
194174
Type: "client",
195175
Lang: language,
196-
URL: external.TrimURL(url),
176+
URL: url,
197177
}
198178
err = file.Write("")
199179
if err != nil {
200180
return fmt.Errorf("Failed to create .codegame.json: %s", err)
201181
}
202182

183+
newData := modules.NewClientData{
184+
Lang: language,
185+
Name: info.Name,
186+
URL: url,
187+
}
188+
203189
switch language {
204190
case "go":
205-
libraryVersion := external.LibraryVersionFromCGVersion("code-game-project", "go-client", info.CGVersion)
206-
err = modules.Execute("go", libraryVersion, "client", "new", "client", "--library-version="+libraryVersion, "--game-name="+info.Name, "--url="+external.TrimURL(url), fmt.Sprintf("--generate-wrappers=%t", cgeMajor > 0 || cgeMinor >= 3))
207-
case "js":
208-
libraryVersion := external.LibraryVersionFromCGVersion("code-game-project", "javascript-client", info.CGVersion)
209-
err = modules.Execute("js", libraryVersion, "client", "new", "client", "--library-version="+libraryVersion, "--game-name="+info.Name, "--url="+external.TrimURL(url))
210-
case "ts":
211-
libraryVersion := external.LibraryVersionFromCGVersion("code-game-project", "javascript-client", info.CGVersion)
212-
err = modules.Execute("js", libraryVersion, "client", "new", "client", "--typescript", "--library-version="+libraryVersion, "--game-name="+info.Name, "--url="+external.TrimURL(url))
191+
newData.LibraryVersion = external.LibraryVersionFromCGVersion("code-game-project", "go-client", info.CGVersion)
192+
err = modules.ExecuteNewClient(newData)
193+
case "js", "ts":
194+
newData.LibraryVersion = external.LibraryVersionFromCGVersion("code-game-project", "javascript-client", info.CGVersion)
195+
err = modules.ExecuteNewClient(newData)
213196
default:
214-
return fmt.Errorf("'new client' is not supported for '%s'", language)
197+
err = fmt.Errorf("'new client' is not supported for '%s'", language)
215198
}
216199
if err != nil {
217200
return err
@@ -223,7 +206,7 @@ func newClient() error {
223206
}
224207

225208
if language == "go" || language == "ts" {
226-
err = cggenevents.CGGenEvents(cgeVersion, eventsOutput, url, language)
209+
err = cggenevents.CGGenEvents(cgeVersion, eventsOutput, external.BaseURL("http", external.IsTLS(url), url), language)
227210
if err != nil {
228211
return err
229212
}

cmd/root.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package cmd
22

33
import (
44
"errors"
5-
"fmt"
65
"os"
6+
"os/exec"
77

88
"github.com/Bananenpro/cli"
99
"github.com/spf13/cobra"
@@ -31,15 +31,9 @@ func abort(err error) {
3131
return
3232
}
3333

34-
if errors.Is(err, cli.ErrCanceled) {
35-
cli.Print(err.Error())
36-
} else {
34+
if _, ok := err.(*exec.ExitError); !ok && !errors.Is(err, cli.ErrCanceled) {
3735
cli.Error(err.Error())
3836
}
39-
40-
if err == cli.ErrCanceled {
41-
os.Exit(0)
42-
}
4337
os.Exit(1)
4438
}
4539

@@ -50,14 +44,8 @@ func abortf(format string, err error) {
5044
return
5145
}
5246

53-
if errors.Is(err, cli.ErrCanceled) {
54-
cli.Print(format, err)
55-
} else {
56-
cli.Error(fmt.Errorf(format, err).Error())
57-
}
58-
59-
if err == cli.ErrCanceled {
60-
os.Exit(0)
47+
if _, ok := err.(*exec.ExitError); !ok && !errors.Is(err, cli.ErrCanceled) {
48+
cli.Error(err.Error())
6149
}
6250
os.Exit(1)
6351
}

0 commit comments

Comments
 (0)