|
| 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 | +// sessionExportCmd represents the session export command |
| 14 | +var sessionExportCmd = &cobra.Command{ |
| 15 | + Use: "export", |
| 16 | + Short: "Export a session to share.code-game.org.", |
| 17 | + Args: cobra.RangeArgs(0, 2), |
| 18 | + 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) |
| 107 | + }, |
| 108 | +} |
| 109 | + |
| 110 | +func init() { |
| 111 | + sessionCmd.AddCommand(sessionExportCmd) |
| 112 | +} |
0 commit comments