Skip to content

Commit 39b8be5

Browse files
committed
Improve error handling
1 parent c5d038b commit 39b8be5

File tree

7 files changed

+88
-12
lines changed

7 files changed

+88
-12
lines changed

cli/output.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,53 @@ import (
55
"fmt"
66
)
77

8+
var inProgress = false
9+
10+
func IsInProgress() bool {
11+
return inProgress
12+
}
13+
814
func Info(format string, a ...any) {
15+
if inProgress {
16+
fmt.Println()
17+
inProgress = false
18+
}
919
fmt.Printf("%s\n", fmt.Sprintf(format, a...))
1020
}
1121

1222
func Begin(format string, a ...any) {
1323
fmt.Printf(format, a...)
24+
inProgress = true
1425
}
1526

1627
func Finish() {
17-
fmt.Print(" done.\n")
28+
if inProgress {
29+
fmt.Print(" done.\n")
30+
inProgress = false
31+
}
1832
}
1933

2034
func Success(format string, a ...any) {
35+
if inProgress {
36+
fmt.Println()
37+
inProgress = false
38+
}
2139
fmt.Printf("\x1b[32m%s\x1b[0m\n", fmt.Sprintf(format, a...))
2240
}
2341

2442
func Warn(format string, a ...any) {
43+
if inProgress {
44+
fmt.Println()
45+
inProgress = false
46+
}
2547
fmt.Printf("\x1b[33mWARNING: %s\x1b[0m\n", fmt.Sprintf(format, a...))
2648
}
2749

2850
func Error(format string, a ...any) error {
51+
if inProgress {
52+
fmt.Println()
53+
inProgress = false
54+
}
2955
message := fmt.Sprintf(format, a...)
3056
fmt.Printf("\x1b[1;31mERROR: %s\x1b[0m\n", message)
3157
return errors.New(message)

commands/new.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func New() error {
1919
project = strings.ToLower(pflag.Arg(1))
2020
} else {
2121
var err error
22-
project, err = cli.Select("Which type of project would you like to create?", []string{"Game Server", "Game Client"}, []string{"server", "client"})
22+
project, err = cli.Select("Which type of project would you like to create?", []string{"Game Client", "Game Server"}, []string{"client", "server"})
2323
if err != nil {
2424
return err
2525
}
@@ -126,7 +126,7 @@ func getCodeGameInfo(baseURL string) (string, string, error) {
126126
CGVersion string `json:"cg_version"`
127127
}
128128
res, err := http.Get(baseURL + "/info")
129-
if err != nil || res.StatusCode != http.StatusOK {
129+
if err != nil || res.StatusCode != http.StatusOK || !external.HasContentType(res.Header, "application/json") {
130130
return "", "", cli.Error("Couldn't access /info endpoint.")
131131
}
132132
defer res.Body.Close()
@@ -142,7 +142,7 @@ func getCodeGameInfo(baseURL string) (string, string, error) {
142142

143143
func getCGEVersion(baseURL string) (string, error) {
144144
res, err := http.Get(baseURL + "/events")
145-
if err != nil || res.StatusCode != http.StatusOK {
145+
if err != nil || res.StatusCode != http.StatusOK || !external.HasContentType(res.Header, "text/plain") {
146146
return "", cli.Error("Couldn't access /events endpoint.")
147147
}
148148
defer res.Body.Close()

commands/new_go.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ func newClientGo(projectName, serverURL, cgVersion string) error {
3333
return err
3434
}
3535

36-
err = external.ExecuteInDir(projectName, "goimports", "-w", "main.go")
37-
if err != nil {
38-
cli.Warn("Failed to add import statements: %s", err)
36+
if !external.IsInstalled("goimports") {
37+
cli.Warn("Failed to add import statements: 'goimports' is not installed!")
38+
return nil
3939
}
40+
41+
external.ExecuteInDir(projectName, "goimports", "-w", "main.go")
4042
cli.Finish()
4143

4244
return nil
@@ -51,7 +53,9 @@ func createGoTemplate(projectName, serverURL string) error {
5153
cli.Begin("Creating project template...")
5254
out, err := external.ExecuteInDirHidden(projectName, "go", "mod", "init", module)
5355
if err != nil {
54-
fmt.Println(out)
56+
if out != "" {
57+
cli.Error(out)
58+
}
5559
return err
5660
}
5761

@@ -84,8 +88,8 @@ func installGoLibrary(projectName, cgVersion string) error {
8488

8589
if clientVersion == "latest" {
8690
out, err := external.ExecuteInDirHidden(projectName, "go", "get")
87-
if err != nil {
88-
fmt.Println(out)
91+
if err != nil && out != "" {
92+
cli.Error(out)
8993
}
9094
return err
9195
}

external/external.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"math"
8+
"mime"
89
"net/http"
910
"os"
1011
"os/exec"
@@ -24,6 +25,10 @@ func IsInstalled(programName string) bool {
2425
}
2526

2627
func Execute(programName string, args ...string) error {
28+
if _, err := exec.LookPath(programName); err != nil {
29+
cli.Error("'%s' ist not installed!", programName)
30+
return err
31+
}
2732
cmd := exec.Command(programName, args...)
2833
cmd.Stdin = os.Stdin
2934
cmd.Stdout = os.Stdout
@@ -32,6 +37,10 @@ func Execute(programName string, args ...string) error {
3237
}
3338

3439
func ExecuteInDir(workingDir, programName string, args ...string) error {
40+
if _, err := exec.LookPath(programName); err != nil {
41+
cli.Error("'%s' ist not installed!", programName)
42+
return err
43+
}
3544
cmd := exec.Command(programName, args...)
3645
cmd.Dir = workingDir
3746
cmd.Stdin = os.Stdin
@@ -41,12 +50,20 @@ func ExecuteInDir(workingDir, programName string, args ...string) error {
4150
}
4251

4352
func ExecuteHidden(programName string, args ...string) (string, error) {
53+
if _, err := exec.LookPath(programName); err != nil {
54+
cli.Error("'%s' ist not installed!", programName)
55+
return "", err
56+
}
4457
cmd := exec.Command(programName, args...)
4558
out, err := cmd.CombinedOutput()
4659
return string(out), err
4760
}
4861

4962
func ExecuteInDirHidden(workingDir, programName string, args ...string) (string, error) {
63+
if _, err := exec.LookPath(programName); err != nil {
64+
cli.Error("'%s' ist not installed!", programName)
65+
return "", err
66+
}
5067
cmd := exec.Command(programName, args...)
5168
cmd.Dir = workingDir
5269
out, err := cmd.CombinedOutput()
@@ -83,7 +100,7 @@ func OpenBrowser(url string) error {
83100

84101
func GithubTagFromVersion(owner, repo, version string) (string, error) {
85102
res, err := http.Get(fmt.Sprintf("https://api.github.com/repos/%s/%s/tags", owner, repo))
86-
if err != nil || res.StatusCode != http.StatusOK {
103+
if err != nil || res.StatusCode != http.StatusOK || !HasContentType(res.Header, "application/json") {
87104
return "", cli.Error("Couldn't access git tags from 'github.com/%s/%s'.", owner, repo)
88105
}
89106
defer res.Body.Close()
@@ -190,3 +207,20 @@ func ClientVersionFromCGVersion(owner, repo, cgVersion string) string {
190207
cli.Warn("No compatible client library version found. Using latest client library version.")
191208
return "latest"
192209
}
210+
func HasContentType(h http.Header, mimetype string) bool {
211+
contentType := h.Get("Content-type")
212+
if contentType == "" {
213+
return mimetype == "application/octet-stream"
214+
}
215+
216+
for _, v := range strings.Split(contentType, ",") {
217+
t, _, err := mime.ParseMediaType(v)
218+
if err != nil {
219+
break
220+
}
221+
if t == mimetype {
222+
return true
223+
}
224+
}
225+
return false
226+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ go 1.18
55
require (
66
github.com/AlecAivazis/survey/v2 v2.3.4
77
github.com/adrg/xdg v0.4.0
8+
github.com/code-game-project/go-client v0.7.0
89
github.com/ogier/pflag v0.0.1
910
)
1011

1112
require (
13+
github.com/google/uuid v1.3.0 // indirect
14+
github.com/gorilla/websocket v1.5.0 // indirect
1215
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
1316
github.com/mattn/go-colorable v0.1.2 // indirect
1417
github.com/mattn/go-isatty v0.0.8 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63n
44
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w=
55
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
66
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
7+
github.com/code-game-project/go-client v0.7.0 h1:XLwh4yKjStEwfa0AZd6ybIP1QbATxaHiU/RyozF6puw=
8+
github.com/code-game-project/go-client v0.7.0/go.mod h1:ZhvVYMFPFipoWXrLJlKaIN9NSYl9ucvHTk9vVdqEj24=
79
github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI=
810
github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
911
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1012
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1113
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
14+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
15+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
16+
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
17+
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
1218
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog=
1319
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68=
1420
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=

main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,14 @@ func main() {
4646
cli.Error(err.Error())
4747
}
4848
default:
49-
cli.Error("Unknown command:", strings.ToLower(pflag.Arg(0)))
49+
cli.Error("Unknown command: %s", strings.ToLower(pflag.Arg(0)))
5050
pflag.Usage()
5151
os.Exit(1)
5252
}
5353
if err != nil {
54+
if cli.IsInProgress() {
55+
fmt.Println()
56+
}
5457
os.Exit(1)
5558
}
5659
}

0 commit comments

Comments
 (0)