Skip to content

Commit ecaff9b

Browse files
committed
Add session show command
1 parent 7d8e950 commit ecaff9b

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

cmd/session_show.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/Bananenpro/cli"
7+
"github.com/code-game-project/go-utils/sessions"
8+
"github.com/mattn/go-colorable"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
// sessionShowCmd represents the session show command
13+
var sessionShowCmd = &cobra.Command{
14+
Use: "show",
15+
Short: "Show the session data.",
16+
Args: cobra.RangeArgs(0, 2),
17+
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+
}
59+
60+
out := colorable.NewColorableStdout()
61+
printInfoProperty(out, "Name", fmt.Sprintf("%s@%s", username, gameURL), 14)
62+
printInfoProperty(out, "Game ID", session.GameId, 14)
63+
printInfoProperty(out, "Player ID", session.PlayerId, 14)
64+
printInfoProperty(out, "Player Secret", session.PlayerSecret, 14)
65+
},
66+
}
67+
68+
func init() {
69+
sessionCmd.AddCommand(sessionShowCmd)
70+
}

0 commit comments

Comments
 (0)