blob: ec38270e8bea08077c7ef3d6db33079133abaa25 [file] [log] [blame]
Philipp Schraderd9096a32022-02-24 17:53:09 -08001// This binary lets users interact with the scouting web server in order to
2// debug it. Run with `--help` to see all the options.
3
4package main
5
6import (
7 "flag"
8 "io/ioutil"
9 "log"
10 "os"
11 "os/exec"
12 "path/filepath"
13
Philipp Schraderd3fac192022-03-02 20:35:46 -080014 "github.com/davecgh/go-spew/spew"
Philipp Schraderd9096a32022-02-24 17:53:09 -080015 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/debug"
16)
17
18// Returns the absolute path of the specified path. This is an unwrapped
19// version of `filepath.Abs`.
20func absPath(path string) string {
21 result, err := filepath.Abs(path)
22 if err != nil {
23 log.Fatal("Failed to determine absolute path for ", path, ": ", err)
24 }
25 return result
26}
27
28// Parses the specified JSON file into a binary version (i.e. serialized
29// flatbuffer). This uses the `flatc` binary and the JSON's corresponding
30// `.fbs` file.
31func parseJson(fbsPath string, jsonPath string) []byte {
32 // Work inside a temporary directory since `flatc` doesn't allow us to
33 // customize the name of the output file.
34 dir, err := ioutil.TempDir("", "webserver_debug_cli")
35 if err != nil {
36 log.Fatal("Failed to create temporary directory: ", err)
37 }
38 defer os.RemoveAll(dir)
39
40 // Turn these paths absolute so that it everything still works from
41 // inside the temporary directory.
42 absFlatcPath := absPath("external/com_github_google_flatbuffers/flatc")
43 absFbsPath := absPath(fbsPath)
44
45 // Create a symlink to the .fbs file so that the output filename that
46 // `flatc` generates is predictable. I.e. `fb.json` gets serialized
47 // into `fb.bin`.
48 jsonSymlink := filepath.Join(dir, "fb.json")
49 os.Symlink(jsonPath, jsonSymlink)
50
51 // Execute the `flatc` command.
52 flatcCommand := exec.Command(absFlatcPath, "--binary", absFbsPath, jsonSymlink)
53 flatcCommand.Dir = dir
Philipp Schradercbf5c6a2022-02-27 23:25:19 -080054 output, err := flatcCommand.CombinedOutput()
Philipp Schraderd9096a32022-02-24 17:53:09 -080055 if err != nil {
Philipp Schradercbf5c6a2022-02-27 23:25:19 -080056 log.Fatal("Failed to execute flatc: ", err, ": ", string(output))
Philipp Schraderd9096a32022-02-24 17:53:09 -080057 }
58
59 // Read the serialized flatbuffer and return it.
60 binaryPath := filepath.Join(dir, "fb.bin")
61 binaryFb, err := os.ReadFile(binaryPath)
62 if err != nil {
63 log.Fatal("Failed to read flatc output ", binaryPath, ": ", err)
64 }
65 return binaryFb
66}
67
Philipp Schrader02ec8e42022-03-26 16:37:18 -070068func maybePerformRequest[T interface{}](fbName, fbsPath, requestJsonPath, address string, requester func(string, []byte) (*T, error)) {
69 if requestJsonPath != "" {
70 log.Printf("Sending %s to %s", fbName, address)
71 binaryRequest := parseJson(fbsPath, requestJsonPath)
72 response, err := requester(address, binaryRequest)
73 if err != nil {
74 log.Fatalf("Failed %s: %v", fbName, err)
75 }
76 spew.Dump(*response)
77 }
78}
79
Philipp Schraderd9096a32022-02-24 17:53:09 -080080func main() {
81 // Parse command line arguments.
Philipp Schrader30005e42022-03-06 13:53:58 -080082 indentPtr := flag.String("indent", " ",
83 "The indentation to use for the result dumping. Default is a space.")
Philipp Schraderd9096a32022-02-24 17:53:09 -080084 addressPtr := flag.String("address", "http://localhost:8080",
85 "The end point where the server is listening.")
86 submitDataScoutingPtr := flag.String("submitDataScouting", "",
87 "If specified, parse the file as a SubmitDataScouting JSON request.")
Filip Kujawaf882e022022-12-14 13:14:08 -080088 submitDriverRankingPtr := flag.String("submitDriverRanking", "",
89 "If specified, parse the file as a submitDriverRanking JSON request.")
90 submitNotesPtr := flag.String("submitNotes", "",
91 "If specified, parse the file as a submitNotes JSON request.")
Philipp Schradercbf5c6a2022-02-27 23:25:19 -080092 requestAllMatchesPtr := flag.String("requestAllMatches", "",
93 "If specified, parse the file as a RequestAllMatches JSON request.")
Philipp Schraderd1c4bef2022-02-28 22:51:30 -080094 requestMatchesForTeamPtr := flag.String("requestMatchesForTeam", "",
95 "If specified, parse the file as a RequestMatchesForTeam JSON request.")
Philipp Schraderacf96232022-03-01 22:03:30 -080096 requestDataScoutingPtr := flag.String("requestDataScouting", "",
97 "If specified, parse the file as a RequestDataScouting JSON request.")
Filip Kujawaf882e022022-12-14 13:14:08 -080098 requestAllDriverRankingsPtr := flag.String("requestAllDriverRankings", "",
99 "If specified, parse the file as a requestAllDriverRankings JSON request.")
100 requestAllNotesPtr := flag.String("requestAllNotes", "",
101 "If specified, parse the file as a requestAllNotes JSON request.")
Philipp Schraderd3fac192022-03-02 20:35:46 -0800102 refreshMatchListPtr := flag.String("refreshMatchList", "",
103 "If specified, parse the file as a RefreshMatchList JSON request.")
Philipp Schraderd9096a32022-02-24 17:53:09 -0800104 flag.Parse()
105
Philipp Schrader30005e42022-03-06 13:53:58 -0800106 spew.Config.Indent = *indentPtr
107
Philipp Schraderfe583842022-04-08 19:47:07 -0700108 // Disable pointer addresses. They're not useful for our purposes.
109 spew.Config.DisablePointerAddresses = true
110
Philipp Schraderd9096a32022-02-24 17:53:09 -0800111 // Handle the actual arguments.
Philipp Schrader02ec8e42022-03-26 16:37:18 -0700112 maybePerformRequest(
113 "SubmitDataScouting",
114 "scouting/webserver/requests/messages/submit_data_scouting.fbs",
115 *submitDataScoutingPtr,
116 *addressPtr,
117 debug.SubmitDataScouting)
118
119 maybePerformRequest(
Filip Kujawaf882e022022-12-14 13:14:08 -0800120 "submitNotes",
121 "scouting/webserver/requests/messages/submit_notes.fbs",
122 *submitNotesPtr,
123 *addressPtr,
124 debug.SubmitNotes)
125
126 maybePerformRequest(
127 "submitDriverRanking",
128 "scouting/webserver/requests/messages/submit_driver_ranking.fbs",
129 *submitDriverRankingPtr,
130 *addressPtr,
131 debug.SubmitDriverRanking)
132
133 maybePerformRequest(
Philipp Schrader02ec8e42022-03-26 16:37:18 -0700134 "RequestAllMatches",
135 "scouting/webserver/requests/messages/request_all_matches.fbs",
136 *requestAllMatchesPtr,
137 *addressPtr,
138 debug.RequestAllMatches)
139
140 maybePerformRequest(
141 "RequestMatchesForTeam",
142 "scouting/webserver/requests/messages/request_matches_for_team.fbs",
143 *requestMatchesForTeamPtr,
144 *addressPtr,
145 debug.RequestMatchesForTeam)
146
147 maybePerformRequest(
148 "RequestDataScouting",
149 "scouting/webserver/requests/messages/request_data_scouting.fbs",
150 *requestDataScoutingPtr,
151 *addressPtr,
152 debug.RequestDataScouting)
153
154 maybePerformRequest(
Filip Kujawaf882e022022-12-14 13:14:08 -0800155 "requestAllDriverRankings",
156 "scouting/webserver/requests/messages/request_all_driver_rankings.fbs",
157 *requestAllDriverRankingsPtr,
158 *addressPtr,
159 debug.RequestAllDriverRankings)
160
161 maybePerformRequest(
162 "requestAllNotes",
163 "scouting/webserver/requests/messages/request_all_notes.fbs",
164 *requestAllNotesPtr,
165 *addressPtr,
166 debug.RequestAllNotes)
167
168 maybePerformRequest(
Philipp Schrader02ec8e42022-03-26 16:37:18 -0700169 "RefreshMatchList",
170 "scouting/webserver/requests/messages/refresh_match_list.fbs",
171 *refreshMatchListPtr,
172 *addressPtr,
173 debug.RefreshMatchList)
Philipp Schraderd9096a32022-02-24 17:53:09 -0800174}