Skip to content

Commit f19454f

Browse files
committed
Implement share subcommands
1 parent c54fcbf commit f19454f

File tree

9 files changed

+334
-141
lines changed

9 files changed

+334
-141
lines changed

cmd/session.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package cmd
22

33
import (
4+
"os"
5+
6+
"github.com/Bananenpro/cli"
7+
"github.com/code-game-project/go-utils/sessions"
48
"github.com/spf13/cobra"
59
)
610

@@ -10,6 +14,47 @@ var sessionCmd = &cobra.Command{
1014
Short: "Manage CodeGame sessions.",
1115
}
1216

17+
func selectSession(args []string) (sessions.Session, error) {
18+
var gameURL string
19+
var username string
20+
if len(args) > 0 {
21+
gameURL = args[0]
22+
if len(args) > 1 {
23+
username = args[1]
24+
}
25+
}
26+
27+
if gameURL == "" {
28+
urls, err := sessions.ListGames()
29+
if err != nil {
30+
gameURL, err = cli.Input("Game URL:")
31+
} else {
32+
var index int
33+
index, err = cli.Select("Game URL:", urls)
34+
gameURL = urls[index]
35+
}
36+
if err != nil {
37+
os.Exit(0)
38+
}
39+
}
40+
41+
if username == "" {
42+
usernames, err := sessions.ListUsernames(gameURL)
43+
if err != nil {
44+
username, err = cli.Input("Username:")
45+
} else {
46+
var index int
47+
index, err = cli.Select("Username:", usernames)
48+
username = usernames[index]
49+
}
50+
if err != nil {
51+
os.Exit(0)
52+
}
53+
}
54+
55+
return sessions.LoadSession(gameURL, username)
56+
}
57+
1358
func init() {
1459
rootCmd.AddCommand(sessionCmd)
1560
}

cmd/session_export.go

Lines changed: 2 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,16 @@
11
package cmd
22

33
import (
4-
"bytes"
5-
"encoding/json"
6-
"net/http"
7-
8-
"github.com/Bananenpro/cli"
9-
"github.com/code-game-project/go-utils/sessions"
104
"github.com/spf13/cobra"
115
)
126

137
// sessionExportCmd represents the session export command
148
var sessionExportCmd = &cobra.Command{
159
Use: "export",
16-
Short: "Export a session to share.code-game.org.",
10+
Short: "Export a session to share.code-game.org (same as 'codegame share session').",
1711
Args: cobra.RangeArgs(0, 2),
1812
Run: func(cmd *cobra.Command, args []string) {
19-
var gameURL string
20-
var username string
21-
if len(args) > 0 {
22-
gameURL = args[0]
23-
if len(args) > 1 {
24-
username = args[1]
25-
}
26-
}
27-
28-
if gameURL == "" {
29-
urls, err := sessions.ListGames()
30-
if err != nil {
31-
gameURL, err = cli.Input("Game URL:")
32-
} else {
33-
var index int
34-
index, err = cli.Select("Game URL:", urls)
35-
gameURL = urls[index]
36-
}
37-
if err != nil {
38-
return
39-
}
40-
}
41-
42-
if username == "" {
43-
usernames, err := sessions.ListUsernames(gameURL)
44-
if err != nil {
45-
username, err = cli.Input("Username:")
46-
} else {
47-
var index int
48-
index, err = cli.Select("Username:", usernames)
49-
username = usernames[index]
50-
}
51-
if err != nil {
52-
return
53-
}
54-
}
55-
56-
session, err := sessions.LoadSession(gameURL, username)
57-
abortf("Failed to load session: %s", err)
58-
59-
type reqSession struct {
60-
GameId string `json:"game_id"`
61-
PlayerId string `json:"player_id"`
62-
PlayerSecret string `json:"player_secret"`
63-
}
64-
65-
type request struct {
66-
GameURL string `json:"game_url"`
67-
Username string `json:"username"`
68-
Session reqSession `json:"session"`
69-
}
70-
71-
data := request{
72-
GameURL: gameURL,
73-
Username: username,
74-
Session: reqSession{
75-
GameId: session.GameId,
76-
PlayerId: session.PlayerId,
77-
PlayerSecret: session.PlayerSecret,
78-
},
79-
}
80-
81-
jsonData, err := json.Marshal(data)
82-
abort(err)
83-
84-
resp, err := http.Post("https://share.code-game.org/session", "application/json", bytes.NewBuffer(jsonData))
85-
if err != nil {
86-
cli.Error("Failed to upload session: %s", err)
87-
return
88-
}
89-
if resp.StatusCode != http.StatusCreated {
90-
type response struct {
91-
Error string `json:"error"`
92-
}
93-
var res response
94-
err = json.NewDecoder(resp.Body).Decode(&res)
95-
cli.Error(res.Error)
96-
return
97-
}
98-
99-
type response struct {
100-
Id string `json:"id"`
101-
}
102-
var res response
103-
err = json.NewDecoder(resp.Body).Decode(&res)
104-
abortf("Failed to decode session data from server: %s", err)
105-
cli.Success("Success! You can import your session with the following command:")
106-
cli.PrintColor(cli.Cyan, "codegame session import %s", res.Id)
13+
shareSessionCmd.Run(cmd, args)
10714
},
10815
}
10916

cmd/session_show.go

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package cmd
33
import (
44
"fmt"
55

6-
"github.com/Bananenpro/cli"
7-
"github.com/code-game-project/go-utils/sessions"
86
"github.com/mattn/go-colorable"
97
"github.com/spf13/cobra"
108
)
@@ -15,50 +13,11 @@ var sessionShowCmd = &cobra.Command{
1513
Short: "Show the session data.",
1614
Args: cobra.RangeArgs(0, 2),
1715
Run: func(cmd *cobra.Command, args []string) {
18-
var gameURL string
19-
var username string
20-
if len(args) > 0 {
21-
gameURL = args[0]
22-
if len(args) > 1 {
23-
username = args[1]
24-
}
25-
}
26-
27-
if gameURL == "" {
28-
urls, err := sessions.ListGames()
29-
if err != nil {
30-
gameURL, err = cli.Input("Game URL:")
31-
} else {
32-
var index int
33-
index, err = cli.Select("Game URL:", urls)
34-
gameURL = urls[index]
35-
}
36-
if err != nil {
37-
return
38-
}
39-
}
40-
41-
if username == "" {
42-
usernames, err := sessions.ListUsernames(gameURL)
43-
if err != nil {
44-
username, err = cli.Input("Username:")
45-
} else {
46-
var index int
47-
index, err = cli.Select("Username:", usernames)
48-
username = usernames[index]
49-
}
50-
if err != nil {
51-
return
52-
}
53-
}
54-
55-
session, err := sessions.LoadSession(gameURL, username)
56-
if err != nil {
57-
abort(fmt.Errorf("The session %s@%s does not exist!", username, gameURL))
58-
}
16+
session, err := selectSession(args)
17+
abortf("Failed to load session: %s", err)
5918

6019
out := colorable.NewColorableStdout()
61-
printInfoProperty(out, "Name", fmt.Sprintf("%s@%s", username, gameURL), 14)
20+
printInfoProperty(out, "Name", fmt.Sprintf("%s@%s", session.Username, session.GameURL), 14)
6221
printInfoProperty(out, "Game ID", session.GameId, 14)
6322
printInfoProperty(out, "Player ID", session.PlayerId, 14)
6423
printInfoProperty(out, "Player Secret", session.PlayerSecret, 14)

cmd/share.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
// shareCmd represents the share command
8+
var shareCmd = &cobra.Command{
9+
Use: "share",
10+
Short: "A CLI interface for share.code-game.org.",
11+
}
12+
13+
func init() {
14+
rootCmd.AddCommand(shareCmd)
15+
}

cmd/share_game.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"net/http"
7+
8+
"github.com/Bananenpro/cli"
9+
"github.com/code-game-project/go-utils/sessions"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
// shareGameCmd represents the share game command
14+
var shareGameCmd = &cobra.Command{
15+
Use: "game",
16+
Short: "Share a game with share.code-game.org.",
17+
Args: cobra.RangeArgs(0, 2),
18+
Run: func(cmd *cobra.Command, args []string) {
19+
var gameURL string
20+
var gameId string
21+
22+
if len(args) > 0 {
23+
gameURL = args[0]
24+
if len(args) > 1 {
25+
gameId = args[1]
26+
}
27+
}
28+
29+
var err error
30+
if gameURL == "" {
31+
fromSession, err := cli.YesNo("Select game URL from session?", true)
32+
abort(err)
33+
if fromSession {
34+
urls, err := sessions.ListGames()
35+
abortf("Failed to load games: %s", err)
36+
selected, err := cli.Select("Game URL:", urls)
37+
abort(err)
38+
gameURL = urls[selected]
39+
} else {
40+
gameURL, err = cli.Input("Game URL:")
41+
abort(err)
42+
}
43+
}
44+
45+
if gameId == "" {
46+
fromSession, err := cli.YesNo("Select game ID from session?", true)
47+
abort(err)
48+
if fromSession {
49+
usernames, err := sessions.ListUsernames(gameURL)
50+
abortf("Failed to load usernames: %s", err)
51+
selected, err := cli.Select("Username:", usernames)
52+
abort(err)
53+
session, err := sessions.LoadSession(gameURL, usernames[selected])
54+
abortf("Failed to load session: %s", err)
55+
gameId = session.GameId
56+
} else {
57+
gameId, err = cli.Input("Game ID:")
58+
abort(err)
59+
}
60+
}
61+
62+
type request struct {
63+
GameURL string `json:"game_url"`
64+
GameId string `json:"game_id"`
65+
}
66+
67+
data := request{
68+
GameURL: gameURL,
69+
GameId: gameId,
70+
}
71+
72+
jsonData, err := json.Marshal(data)
73+
abort(err)
74+
75+
resp, err := http.Post("https://share.code-game.org/game", "application/json", bytes.NewBuffer(jsonData))
76+
if err != nil {
77+
cli.Error("Failed to upload data: %s", err)
78+
return
79+
}
80+
if resp.StatusCode != http.StatusCreated {
81+
type response struct {
82+
Error string `json:"error"`
83+
}
84+
var res response
85+
err = json.NewDecoder(resp.Body).Decode(&res)
86+
cli.Error(res.Error)
87+
return
88+
}
89+
90+
type response struct {
91+
Id string `json:"id"`
92+
}
93+
var res response
94+
err = json.NewDecoder(resp.Body).Decode(&res)
95+
abortf("Failed to decode server response: %s", err)
96+
cli.Success("Success! You can view the game details with the following link:")
97+
cli.PrintColor(cli.Cyan, "https://share.code-game.org/%s", res.Id)
98+
},
99+
}
100+
101+
func init() {
102+
shareCmd.AddCommand(shareGameCmd)
103+
}

0 commit comments

Comments
 (0)