Skip to content

Commit a668495

Browse files
committed
Implement session list and session remove commands
1 parent a098f83 commit a668495

File tree

5 files changed

+134
-9
lines changed

5 files changed

+134
-9
lines changed

cmd/session.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+
// sessionCmd represents the session command
8+
var sessionCmd = &cobra.Command{
9+
Use: "session",
10+
Short: "Manage CodeGame sessions.",
11+
}
12+
13+
func init() {
14+
rootCmd.AddCommand(sessionCmd)
15+
}

cmd/session_list.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cmd
2+
3+
import (
4+
"github.com/Bananenpro/cli"
5+
"github.com/code-game-project/go-utils/sessions"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
// sessionListCmd represents the session list command
10+
var sessionListCmd = &cobra.Command{
11+
Use: "list",
12+
Short: "List all available sessions.",
13+
Run: func(cmd *cobra.Command, args []string) {
14+
sessionList, err := sessions.ListSessions()
15+
if err != nil {
16+
cli.Error("Failed to retrieve session list: %s", err)
17+
return
18+
}
19+
for game, usernames := range sessionList {
20+
cli.PrintColor(cli.CyanBold, game)
21+
for _, u := range usernames {
22+
cli.Print(" - %s", u)
23+
}
24+
}
25+
if len(sessionList) == 0 {
26+
cli.Print("No sessions stored.")
27+
}
28+
},
29+
}
30+
31+
func init() {
32+
sessionCmd.AddCommand(sessionListCmd)
33+
}

cmd/session_remove.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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/spf13/cobra"
9+
)
10+
11+
// sessionRemoveCmd represents the session remove command
12+
var sessionRemoveCmd = &cobra.Command{
13+
Use: "remove",
14+
Short: "Remove a session.",
15+
Args: cobra.RangeArgs(0, 2),
16+
Run: func(cmd *cobra.Command, args []string) {
17+
var gameURL string
18+
var username string
19+
var err error
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+
yes, err := cli.YesNo(fmt.Sprintf("Are you sure you want to remove '%s/%s'?", gameURL, username), false)
56+
if !yes {
57+
if err == nil {
58+
cli.Error("Canceled.")
59+
}
60+
return
61+
}
62+
63+
err = sessions.Session{
64+
GameURL: gameURL,
65+
Username: username,
66+
}.Remove()
67+
if err != nil {
68+
cli.Error("Failed to remove session: %s", err)
69+
return
70+
}
71+
cli.Success("Successfully removed session.")
72+
},
73+
}
74+
75+
func init() {
76+
sessionCmd.AddCommand(sessionRemoveCmd)
77+
}

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ go 1.18
44

55
require (
66
github.com/Bananenpro/cli v0.2.1
7-
github.com/code-game-project/go-utils v0.1.0
8-
github.com/gomarkdown/markdown v0.0.0-20220627144906-e9a81102ebeb
7+
github.com/code-game-project/go-utils v0.2.2
8+
github.com/gomarkdown/markdown v0.0.0-20220731190611-dcdaee8e7a53
99
github.com/mattn/go-colorable v0.1.12
1010
github.com/spf13/cobra v1.5.0
1111
)
@@ -18,7 +18,7 @@ require (
1818
github.com/mattn/go-isatty v0.0.14 // indirect
1919
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
2020
github.com/spf13/pflag v1.0.5 // indirect
21-
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
21+
golang.org/x/sys v0.0.0-20220731174439-a90be440212d // indirect
2222
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect
2323
golang.org/x/text v0.3.7 // indirect
2424
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ 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.1.0 h1:mtxld853WsjVeDFP4l5hMGaq7thXDLslkxC3MEdyPMY=
10-
github.com/code-game-project/go-utils v0.1.0/go.mod h1:kQ6kH9XDzdM2pnJUI1lw61Gp8XOams/E2dKABa1mBI8=
9+
github.com/code-game-project/go-utils v0.2.2 h1:YFT3GAqCTD8CmFWiv7MZfgNol1ayHdH11//dVNj0wLI=
10+
github.com/code-game-project/go-utils v0.2.2/go.mod h1:kQ6kH9XDzdM2pnJUI1lw61Gp8XOams/E2dKABa1mBI8=
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=
1414
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1515
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1616
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
17-
github.com/gomarkdown/markdown v0.0.0-20220627144906-e9a81102ebeb h1:5b/eFaSaKPFG9ygDBaPKkydKU5nFJYk08g9jPIVogMg=
18-
github.com/gomarkdown/markdown v0.0.0-20220627144906-e9a81102ebeb/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
17+
github.com/gomarkdown/markdown v0.0.0-20220731190611-dcdaee8e7a53 h1:JguE3sS3yLzLiCTCnsmzVFuTvTMDJALbzCgurwY5G/0=
18+
github.com/gomarkdown/markdown v0.0.0-20220731190611-dcdaee8e7a53/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
1919
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog=
2020
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68=
2121
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
@@ -48,8 +48,8 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc
4848
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4949
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5050
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
51-
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 h1:WIoqL4EROvwiPdUtaip4VcDdpZ4kha7wBWZrbVKCIZg=
52-
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
51+
golang.org/x/sys v0.0.0-20220731174439-a90be440212d h1:Sv5ogFZatcgIMMtBSTTAgMYsicp25MXBubjXNDKwm80=
52+
golang.org/x/sys v0.0.0-20220731174439-a90be440212d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5353
golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY=
5454
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 h1:Q5284mrmYTpACcm+eAKjKJH48BBwSyfJqmmGDTtT8Vc=
5555
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=

0 commit comments

Comments
 (0)