Skip to content

Commit 6bf74f0

Browse files
committed
Refactor
- Centralize error handling - Use cobra - Rename util dir to pkg - Create pkg/server package
1 parent 556d029 commit 6bf74f0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+785
-1395
lines changed

LICENSE

Lines changed: 0 additions & 674 deletions
Large diffs are not rendered by default.

cmd/build.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
3+
4+
*/
5+
package cmd
6+
7+
import (
8+
"fmt"
9+
10+
"github.com/code-game-project/codegame-cli/pkg/cgfile"
11+
"github.com/code-game-project/codegame-cli/pkg/modules"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
// buildCmd represents the build command
16+
var buildCmd = &cobra.Command{
17+
Use: "build",
18+
Short: "Build the current project.",
19+
DisableFlagParsing: true,
20+
Run: func(cmd *cobra.Command, args []string) {
21+
rootRelative, err := cgfile.FindProjectRootRelative()
22+
abort(err)
23+
24+
data, err := cgfile.LoadCodeGameFile(rootRelative)
25+
abortf("failed to load .codegame.json: %w", err)
26+
27+
cmdArgs := []string{"build"}
28+
cmdArgs = append(cmdArgs, args...)
29+
30+
switch data.Lang {
31+
case "go":
32+
err = modules.Execute("go", "latest", data.Type, cmdArgs...)
33+
abort(err)
34+
default:
35+
abort(fmt.Errorf("'build' is not supported for '%s'", data.Lang))
36+
}
37+
},
38+
}
39+
40+
func init() {
41+
rootCmd.AddCommand(buildCmd)
42+
}

cmd/changeUrl.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
3+
4+
*/
5+
package cmd
6+
7+
import (
8+
"errors"
9+
"os"
10+
"strings"
11+
12+
"github.com/Bananenpro/cli"
13+
"github.com/code-game-project/codegame-cli/pkg/cgfile"
14+
"github.com/code-game-project/codegame-cli/pkg/server"
15+
"github.com/spf13/cobra"
16+
)
17+
18+
// changeUrlCmd represents the changeUrl command
19+
var changeUrlCmd = &cobra.Command{
20+
Use: "changeUrl",
21+
Short: "Permanently switch to a different game URL.",
22+
Args: cobra.RangeArgs(0, 1),
23+
Run: func(cmd *cobra.Command, args []string) {
24+
root, err := cgfile.FindProjectRoot()
25+
abort(err)
26+
27+
var url string
28+
if len(os.Args) >= 3 {
29+
url = strings.ToLower(os.Args[2])
30+
} else {
31+
var err error
32+
url, err = cli.Input("New game URL:")
33+
abort(err)
34+
}
35+
api, err := server.NewAPI(url)
36+
abort(err)
37+
38+
config, err := cgfile.LoadCodeGameFile(root)
39+
abort(err)
40+
41+
if config.Type != "client" {
42+
abort(errors.New("project is not a client"))
43+
}
44+
45+
info, err := api.FetchGameInfo()
46+
abort(err)
47+
if info.Name != config.Game {
48+
abort(errors.New("The URL points to a different game."))
49+
}
50+
51+
prevURL := config.URL
52+
53+
config.URL = url
54+
err = config.Write(root)
55+
abort(err)
56+
57+
err = update()
58+
if err != nil {
59+
config.URL = prevURL
60+
config.Write(root)
61+
abort(err)
62+
}
63+
},
64+
}
65+
66+
func init() {
67+
rootCmd.AddCommand(changeUrlCmd)
68+
}

cmd/docs.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
7+
_ "embed"
8+
9+
"github.com/Bananenpro/cli"
10+
"github.com/code-game-project/codegame-cli/pkg/cggenevents"
11+
"github.com/code-game-project/codegame-cli/pkg/exec"
12+
"github.com/code-game-project/codegame-cli/pkg/server"
13+
"github.com/gomarkdown/markdown"
14+
"github.com/gomarkdown/markdown/html"
15+
"github.com/gomarkdown/markdown/parser"
16+
"github.com/spf13/cobra"
17+
)
18+
19+
//go:embed templates/css/docs.css
20+
var docsStyle string
21+
22+
var docsCmd = &cobra.Command{
23+
Use: "docs",
24+
Short: "View the documention of CodeGame or a specific game in your webbrowser.",
25+
Args: cobra.RangeArgs(0, 1),
26+
Run: func(cmd *cobra.Command, args []string) {
27+
if len(args) == 0 {
28+
cli.Print("Opening documentation...")
29+
err := exec.OpenBrowser("https://docs.code-game.org")
30+
abort(err)
31+
}
32+
33+
api, err := server.NewAPI(args[0])
34+
abort(err)
35+
36+
url := api.BaseURL()
37+
38+
cge, err := api.GetCGEFile()
39+
abort(err)
40+
41+
cgeVersion, err := cggenevents.ParseCGEVersion(cge)
42+
abort(err)
43+
44+
err = cggenevents.CGGenEvents(cgeVersion, os.TempDir(), url, "markdown")
45+
abort(err)
46+
47+
md, err := os.ReadFile(filepath.Join(os.TempDir(), "event_docs.md"))
48+
abort(err)
49+
50+
md = markdown.NormalizeNewlines(md)
51+
text := markdown.ToHTML(md, parser.NewWithExtensions(parser.CommonExtensions|parser.AutoHeadingIDs), html.NewRenderer(html.RendererOptions{
52+
CSS: filepath.Join(os.TempDir(), "event_docs.css"),
53+
Flags: html.CommonFlags | html.CompletePage,
54+
}))
55+
56+
os.Remove(filepath.Join(os.TempDir(), "event_docs.md"))
57+
58+
err = os.WriteFile(filepath.Join(os.TempDir(), "event_docs.html"), text, 0644)
59+
abort(err)
60+
61+
err = os.WriteFile(filepath.Join(os.TempDir(), "event_docs.css"), []byte(docsStyle), 0644)
62+
abort(err)
63+
64+
cli.Print("Opening documentation...")
65+
66+
err = exec.OpenBrowser(filepath.Join(os.TempDir(), "event_docs.html"))
67+
abort(err)
68+
},
69+
}
70+
71+
func init() {
72+
rootCmd.AddCommand(docsCmd)
73+
}

cmd/info.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
3+
4+
*/
5+
package cmd
6+
7+
import (
8+
"fmt"
9+
"io"
10+
"os"
11+
"strings"
12+
"unicode/utf8"
13+
14+
"github.com/Bananenpro/cli"
15+
"github.com/code-game-project/codegame-cli/pkg/external"
16+
"github.com/code-game-project/codegame-cli/pkg/server"
17+
"github.com/mattn/go-colorable"
18+
"github.com/spf13/cobra"
19+
)
20+
21+
// infoCmd represents the info command
22+
var infoCmd = &cobra.Command{
23+
Use: "info",
24+
Short: "Display some information about a game server.",
25+
Args: cobra.MaximumNArgs(1),
26+
Run: func(cmd *cobra.Command, args []string) {
27+
var url string
28+
if len(args) > 0 {
29+
url = strings.ToLower(os.Args[0])
30+
} else {
31+
var err error
32+
url, err = cli.Input("Game server URL:")
33+
abort(err)
34+
}
35+
api, err := server.NewAPI(url)
36+
abort(err)
37+
38+
info, err := api.FetchGameInfo()
39+
abort(err)
40+
41+
printInfo(info)
42+
43+
cli.PrintColor(cli.Yellow, "\nTo view the documentation of this game run:\n%s docs %s", os.Args[0], external.TrimURL(url))
44+
},
45+
}
46+
47+
func init() {
48+
rootCmd.AddCommand(infoCmd)
49+
}
50+
51+
func printInfo(info server.GameInfo) {
52+
out := colorable.NewColorableStdout()
53+
printInfoProperty(out, "Display Name", info.DisplayName, 17)
54+
printInfoProperty(out, "Name", info.Name, 17)
55+
printInfoProperty(out, "Description", info.Description, 17)
56+
printInfoProperty(out, "Version", info.Version, 17)
57+
printInfoProperty(out, "CodeGame Version", info.CGVersion, 17)
58+
printInfoProperty(out, "Repository", info.RepositoryURL, 17)
59+
}
60+
61+
func printInfoProperty(out io.Writer, name, value string, labelWidth int) {
62+
if value == "" {
63+
return
64+
}
65+
66+
label := name + ":"
67+
if labelWidth-utf8.RuneCountInString(label) > 0 {
68+
label += strings.Repeat(" ", labelWidth-utf8.RuneCountInString(label))
69+
}
70+
71+
fmt.Fprintf(out, "\x1b[36m%s\x1b[0m %s\n", label, value)
72+
}

0 commit comments

Comments
 (0)