Skip to content

Commit 9c97a8e

Browse files
committed
Implement gen-events command
1 parent e2f1148 commit 9c97a8e

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

cmd/genEvents.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"net/http"
7+
"os"
8+
"path/filepath"
9+
"strings"
10+
11+
"github.com/adrg/xdg"
12+
"github.com/code-game-project/go-utils/cggenevents"
13+
"github.com/code-game-project/go-utils/exec"
14+
"github.com/spf13/cobra"
15+
)
16+
17+
// genEventsCmd represents the genEvents command
18+
var genEventsCmd = &cobra.Command{
19+
Use: "gen-events",
20+
Short: "Generate event definitions from CGE files.",
21+
Args: cobra.ExactArgs(1),
22+
Run: func(cmd *cobra.Command, args []string) {
23+
filename := args[0]
24+
var cge []byte
25+
var err error
26+
if strings.HasPrefix(filename, "http://") || strings.HasPrefix(filename, "https://") {
27+
if !strings.HasSuffix(filename, "/api/events") && !strings.HasSuffix(filename, ".cge") {
28+
if strings.HasSuffix(filename, "/api") {
29+
filename += "/events"
30+
} else if strings.HasSuffix(filename, "/") {
31+
filename += "api/events"
32+
} else {
33+
filename += "/api/events"
34+
}
35+
}
36+
resp, err := http.Get(filename)
37+
if err != nil {
38+
abort(fmt.Errorf("Failed to reach url '%s': %s", filename, err))
39+
}
40+
if resp.StatusCode != http.StatusOK {
41+
abort(fmt.Errorf("Failed to download CGE file from url '%s'", filename))
42+
}
43+
if !strings.Contains(resp.Header.Get("Content-Type"), "text/plain") {
44+
abort(fmt.Errorf("Unsupported content type at '%s': expected %s, got %s\n", filename, "text/plain", resp.Header.Get("Content-Type")))
45+
}
46+
cge, err = io.ReadAll(resp.Body)
47+
abortf("Failed to read CGE file: %s", err)
48+
} else {
49+
cge, err = os.ReadFile(filename)
50+
abortf("Failed to read CGE file: ", err)
51+
}
52+
53+
cgeVersion, err := cggenevents.ParseCGEVersion(string(cge))
54+
abortf("Failed to determine CGE file version: %s", err)
55+
56+
output, err := cmd.Flags().GetString("output")
57+
abort(err)
58+
languages, err := cmd.Flags().GetStringSlice("languages")
59+
abort(err)
60+
61+
cgGenEvents, err := cggenevents.InstallCGGenEvents(cgeVersion)
62+
abortf("Failed to install cg-gen-events: %s", err)
63+
_, err = exec.Execute(false, filepath.Join(xdg.DataHome, "codegame", "bin", "cg-gen-events", cgGenEvents), filename, "-o", output, "-l", strings.Join(languages, ","))
64+
if err != nil {
65+
os.Exit(1)
66+
}
67+
},
68+
}
69+
70+
func init() {
71+
rootCmd.AddCommand(genEventsCmd)
72+
genEventsCmd.Flags().StringP("output", "o", ".", "The directory where every file will be generated into. (Will be created if it does not exist.)")
73+
genEventsCmd.Flags().StringSliceP("languages", "l", []string{""}, "A list of target languages.")
74+
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ go 1.18
44

55
require (
66
github.com/Bananenpro/cli v0.2.1
7-
github.com/code-game-project/go-utils v0.2.2
7+
github.com/adrg/xdg v0.4.0
8+
github.com/code-game-project/go-utils v0.2.3
89
github.com/gomarkdown/markdown v0.0.0-20220731190611-dcdaee8e7a53
910
github.com/mattn/go-colorable v0.1.12
1011
github.com/spf13/cobra v1.5.0
1112
)
1213

1314
require (
1415
github.com/AlecAivazis/survey/v2 v2.3.5 // indirect
15-
github.com/adrg/xdg v0.4.0 // indirect
1616
github.com/inconshreveable/mousetrap v1.0.0 // indirect
1717
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
1818
github.com/mattn/go-isatty v0.0.14 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63n
66
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w=
77
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
88
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
9-
github.com/code-game-project/go-utils v0.2.2 h1:YFT3GAqCTD8CmFWiv7MZfgNol1ayHdH11//dVNj0wLI=
10-
github.com/code-game-project/go-utils v0.2.2/go.mod h1:kQ6kH9XDzdM2pnJUI1lw61Gp8XOams/E2dKABa1mBI8=
9+
github.com/code-game-project/go-utils v0.2.3 h1:nyejoWxktnX9SywtcV8v5BF0xS9M3kwy1v7Xl3RyHp4=
10+
github.com/code-game-project/go-utils v0.2.3/go.mod h1:kQ6kH9XDzdM2pnJUI1lw61Gp8XOams/E2dKABa1mBI8=
1111
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
1212
github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI=
1313
github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=

0 commit comments

Comments
 (0)