Skip to content

Commit 8b06757

Browse files
committed
Respect share_url config
1 parent 2668fe7 commit 8b06757

File tree

10 files changed

+56
-21
lines changed

10 files changed

+56
-21
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ Remove a session:
7070
codegame session remove
7171
```
7272

73-
Export a session to *share.code-game.org*:
73+
Export a session to [CodeGame Share](https://share.code-game.org):
7474
```
7575
codegame session export
7676
```
7777

78-
Import a session from *share.code-game.org*:
78+
Import a session from [CodeGame Share](https://share.code-game.org):
7979
```
8080
codegame session import <id>
8181
```
@@ -92,7 +92,7 @@ Create a new game on a server:
9292
codegame game create <url>
9393
```
9494

95-
### Sharing
95+
### Sharing with [CodeGame Share](https://share.code-game.org)
9696

9797
Share a game:
9898
```

cmd/session.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"errors"
45
"os"
56

67
"github.com/Bananenpro/cli"
@@ -26,12 +27,17 @@ func selectSession(args []string) (sessions.Session, error) {
2627

2728
if gameURL == "" {
2829
urls, err := sessions.ListGames()
30+
if len(urls) == 0 {
31+
abort(errors.New("no sessions available"))
32+
}
2933
if err != nil {
3034
gameURL, err = cli.Input("Game URL:")
3135
} else {
3236
var index int
3337
index, err = cli.Select("Game URL:", urls)
34-
gameURL = urls[index]
38+
if err == nil {
39+
gameURL = urls[index]
40+
}
3541
}
3642
if err != nil {
3743
os.Exit(0)
@@ -40,12 +46,17 @@ func selectSession(args []string) (sessions.Session, error) {
4046

4147
if username == "" {
4248
usernames, err := sessions.ListUsernames(gameURL)
49+
if len(usernames) == 0 {
50+
abort(errors.New("no sessions available"))
51+
}
4352
if err != nil {
4453
username, err = cli.Input("Username:")
4554
} else {
4655
var index int
4756
index, err = cli.Select("Username:", usernames)
48-
username = usernames[index]
57+
if err == nil {
58+
username = usernames[index]
59+
}
4960
}
5061
if err != nil {
5162
os.Exit(0)

cmd/session_export.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
// sessionExportCmd represents the session export command
88
var sessionExportCmd = &cobra.Command{
99
Use: "export",
10-
Short: "Export a session to share.code-game.org (same as 'codegame share session').",
10+
Short: "Export a session to CodeGame Share (same as 'codegame share session').",
1111
Args: cobra.RangeArgs(0, 2),
1212
Run: func(cmd *cobra.Command, args []string) {
1313
shareSessionCmd.Run(cmd, args)

cmd/session_import.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ import (
66
"net/http"
77

88
"github.com/Bananenpro/cli"
9+
"github.com/code-game-project/go-utils/config"
10+
"github.com/code-game-project/go-utils/external"
911
"github.com/code-game-project/go-utils/sessions"
1012
"github.com/spf13/cobra"
1113
)
1214

1315
// sessionImportCmd represents the session import command
1416
var sessionImportCmd = &cobra.Command{
1517
Use: "import",
16-
Short: "Import a session from share.code-game.org.",
18+
Short: "Import a session from CodeGame Share.",
1719
Args: cobra.RangeArgs(0, 1),
1820
Run: func(cmd *cobra.Command, args []string) {
1921
var id string
@@ -27,8 +29,12 @@ var sessionImportCmd = &cobra.Command{
2729
}
2830
}
2931

30-
resp, err := http.Get(fmt.Sprintf("https://share.code-game.org/%s?type=session", id))
31-
abortf("Failed to contact share.code-game.org: %s", err)
32+
conf := config.Load()
33+
shareURL := external.TrimURL(conf.ShareURL)
34+
baseURL := external.BaseURL("http", external.IsTLS(shareURL), shareURL)
35+
36+
resp, err := http.Get(fmt.Sprintf(baseURL+"/%s?type=session", id))
37+
abortf(fmt.Sprintf("Failed to contact %s: %s", conf.ShareURL, "%s"), err)
3238

3339
type resSession struct {
3440
GameId string `json:"game_id"`

cmd/share.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
// shareCmd represents the share command
99
var shareCmd = &cobra.Command{
1010
Use: "share",
11-
Short: "A CLI for share.code-game.org.",
11+
Short: "A CLI for CodeGame Share.",
1212
}
1313

1414
func findGameURL() string {

cmd/share_game.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"net/http"
77

88
"github.com/Bananenpro/cli"
9+
"github.com/code-game-project/go-utils/config"
10+
"github.com/code-game-project/go-utils/external"
911
"github.com/code-game-project/go-utils/sessions"
1012
"github.com/google/uuid"
1113
"github.com/spf13/cobra"
@@ -14,7 +16,7 @@ import (
1416
// shareGameCmd represents the share game command
1517
var shareGameCmd = &cobra.Command{
1618
Use: "game",
17-
Short: "Share a game with share.code-game.org.",
19+
Short: "Share a game with CodeGame Share.",
1820
Args: cobra.RangeArgs(0, 2),
1921
Run: func(cmd *cobra.Command, args []string) {
2022
var gameURL string
@@ -94,7 +96,11 @@ var shareGameCmd = &cobra.Command{
9496
jsonData, err := json.Marshal(data)
9597
abort(err)
9698

97-
resp, err := http.Post("https://share.code-game.org/game", "application/json", bytes.NewBuffer(jsonData))
99+
conf := config.Load()
100+
shareURL := external.TrimURL(conf.ShareURL)
101+
baseURL := external.BaseURL("http", external.IsTLS(shareURL), shareURL)
102+
103+
resp, err := http.Post(baseURL+"/game", "application/json", bytes.NewBuffer(jsonData))
98104
if err != nil {
99105
cli.Error("Failed to upload data: %s", err)
100106
return
@@ -116,7 +122,7 @@ var shareGameCmd = &cobra.Command{
116122
err = json.NewDecoder(resp.Body).Decode(&res)
117123
abortf("Failed to decode server response: %s", err)
118124
cli.Success("Success! You can view the game details with the following link:")
119-
cli.PrintColor(cli.Cyan, "https://share.code-game.org/%s", res.Id)
125+
cli.PrintColor(cli.Cyan, baseURL+"/%s", res.Id)
120126
},
121127
}
122128

cmd/share_session.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import (
66
"net/http"
77

88
"github.com/Bananenpro/cli"
9+
"github.com/code-game-project/go-utils/config"
10+
"github.com/code-game-project/go-utils/external"
911
"github.com/spf13/cobra"
1012
)
1113

1214
// shareSessionCmd represents the share session command
1315
var shareSessionCmd = &cobra.Command{
1416
Use: "session",
15-
Short: "Share a session with share.code-game.org (same as 'codegame session export').",
17+
Short: "Share a session with CodeGame Share (same as 'codegame session export').",
1618
Args: cobra.RangeArgs(0, 2),
1719
Run: func(cmd *cobra.Command, args []string) {
1820
session, err := selectSession(args)
@@ -43,7 +45,11 @@ var shareSessionCmd = &cobra.Command{
4345
jsonData, err := json.Marshal(data)
4446
abort(err)
4547

46-
resp, err := http.Post("https://share.code-game.org/session", "application/json", bytes.NewBuffer(jsonData))
48+
conf := config.Load()
49+
shareURL := external.TrimURL(conf.ShareURL)
50+
baseURL := external.BaseURL("http", external.IsTLS(shareURL), shareURL)
51+
52+
resp, err := http.Post(baseURL+"/session", "application/json", bytes.NewBuffer(jsonData))
4753
if err != nil {
4854
cli.Error("Failed to upload session: %s", err)
4955
return

cmd/share_spectate.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import (
66
"net/http"
77

88
"github.com/Bananenpro/cli"
9+
"github.com/code-game-project/go-utils/config"
10+
"github.com/code-game-project/go-utils/external"
911
"github.com/spf13/cobra"
1012
)
1113

1214
// shareSpectateCmd represents the share spectate command
1315
var shareSpectateCmd = &cobra.Command{
1416
Use: "spectate",
15-
Short: "Share a spectate link with share.code-game.org.",
17+
Short: "Share a spectate link with CodeGame Share.",
1618
Args: cobra.RangeArgs(0, 2),
1719
Run: func(cmd *cobra.Command, args []string) {
1820
var gameURL string
@@ -63,7 +65,11 @@ var shareSpectateCmd = &cobra.Command{
6365
jsonData, err := json.Marshal(data)
6466
abort(err)
6567

66-
resp, err := http.Post("https://share.code-game.org/spectate", "application/json", bytes.NewBuffer(jsonData))
68+
conf := config.Load()
69+
shareURL := external.TrimURL(conf.ShareURL)
70+
baseURL := external.BaseURL("http", external.IsTLS(shareURL), shareURL)
71+
72+
resp, err := http.Post(baseURL+"/spectate", "application/json", bytes.NewBuffer(jsonData))
6773
if err != nil {
6874
cli.Error("Failed to upload data: %s", err)
6975
return
@@ -85,7 +91,7 @@ var shareSpectateCmd = &cobra.Command{
8591
err = json.NewDecoder(resp.Body).Decode(&res)
8692
abortf("Failed to decode server response: %s", err)
8793
cli.Success("Success! You can spectate the game with the following link:")
88-
cli.PrintColor(cli.Cyan, "https://share.code-game.org/%s", res.Id)
94+
cli.PrintColor(cli.Cyan, baseURL+"/%s", res.Id)
8995
},
9096
}
9197

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.19
55
require (
66
github.com/Bananenpro/cli v0.3.0
77
github.com/adrg/xdg v0.4.0
8-
github.com/code-game-project/go-utils v0.3.0
8+
github.com/code-game-project/go-utils v0.3.1
99
github.com/gomarkdown/markdown v0.0.0-20221013030248-663e2500819c
1010
github.com/google/uuid v1.3.0
1111
github.com/mattn/go-colorable v0.1.13

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.3.0 h1:FD09rCsuia6o3Sf/JkQk4j5JGU84loh2d24YS1Tvz4I=
10-
github.com/code-game-project/go-utils v0.3.0/go.mod h1:/ws9iYkZCnZvS9g2aqdxKwSnU5AEeI7SE1mbB+x4ggg=
9+
github.com/code-game-project/go-utils v0.3.1 h1:c1qKSPWbVVc4ygU3YBl0wNBu1yjMDoGzGLAWePK9sUo=
10+
github.com/code-game-project/go-utils v0.3.1/go.mod h1:/ws9iYkZCnZvS9g2aqdxKwSnU5AEeI7SE1mbB+x4ggg=
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)