blob: 6f2de1d4e3173b4d97ee095d6359b6af7eacbc80 [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
68func main() {
69 // Parse command line arguments.
Philipp Schrader30005e42022-03-06 13:53:58 -080070 indentPtr := flag.String("indent", " ",
71 "The indentation to use for the result dumping. Default is a space.")
Philipp Schraderd9096a32022-02-24 17:53:09 -080072 addressPtr := flag.String("address", "http://localhost:8080",
73 "The end point where the server is listening.")
74 submitDataScoutingPtr := flag.String("submitDataScouting", "",
75 "If specified, parse the file as a SubmitDataScouting JSON request.")
Philipp Schradercbf5c6a2022-02-27 23:25:19 -080076 requestAllMatchesPtr := flag.String("requestAllMatches", "",
77 "If specified, parse the file as a RequestAllMatches JSON request.")
Philipp Schraderd1c4bef2022-02-28 22:51:30 -080078 requestMatchesForTeamPtr := flag.String("requestMatchesForTeam", "",
79 "If specified, parse the file as a RequestMatchesForTeam JSON request.")
Philipp Schraderacf96232022-03-01 22:03:30 -080080 requestDataScoutingPtr := flag.String("requestDataScouting", "",
81 "If specified, parse the file as a RequestDataScouting JSON request.")
Philipp Schraderd3fac192022-03-02 20:35:46 -080082 refreshMatchListPtr := flag.String("refreshMatchList", "",
83 "If specified, parse the file as a RefreshMatchList JSON request.")
Philipp Schraderd9096a32022-02-24 17:53:09 -080084 flag.Parse()
85
Philipp Schrader30005e42022-03-06 13:53:58 -080086 spew.Config.Indent = *indentPtr
87
Philipp Schraderd9096a32022-02-24 17:53:09 -080088 // Handle the actual arguments.
89 if *submitDataScoutingPtr != "" {
90 log.Printf("Sending SubmitDataScouting to %s", *addressPtr)
91 binaryRequest := parseJson(
92 "scouting/webserver/requests/messages/submit_data_scouting.fbs",
93 *submitDataScoutingPtr)
94 response, err := debug.SubmitDataScouting(*addressPtr, binaryRequest)
95 if err != nil {
96 log.Fatal("Failed SubmitDataScouting: ", err)
97 }
Philipp Schraderd3fac192022-03-02 20:35:46 -080098 spew.Dump(*response)
Philipp Schradercbf5c6a2022-02-27 23:25:19 -080099 }
100 if *requestAllMatchesPtr != "" {
101 log.Printf("Sending RequestAllMatches to %s", *addressPtr)
102 binaryRequest := parseJson(
103 "scouting/webserver/requests/messages/request_all_matches.fbs",
104 *requestAllMatchesPtr)
105 response, err := debug.RequestAllMatches(*addressPtr, binaryRequest)
106 if err != nil {
107 log.Fatal("Failed RequestAllMatches: ", err)
108 }
Philipp Schraderd3fac192022-03-02 20:35:46 -0800109 spew.Dump(*response)
Philipp Schraderd9096a32022-02-24 17:53:09 -0800110 }
Philipp Schraderd1c4bef2022-02-28 22:51:30 -0800111 if *requestMatchesForTeamPtr != "" {
112 log.Printf("Sending RequestMatchesForTeam to %s", *addressPtr)
113 binaryRequest := parseJson(
114 "scouting/webserver/requests/messages/request_matches_for_team.fbs",
115 *requestMatchesForTeamPtr)
116 response, err := debug.RequestMatchesForTeam(*addressPtr, binaryRequest)
117 if err != nil {
118 log.Fatal("Failed RequestMatchesForTeam: ", err)
119 }
Philipp Schraderd3fac192022-03-02 20:35:46 -0800120 spew.Dump(*response)
Philipp Schraderd1c4bef2022-02-28 22:51:30 -0800121 }
Philipp Schraderacf96232022-03-01 22:03:30 -0800122 if *requestDataScoutingPtr != "" {
123 log.Printf("Sending RequestDataScouting to %s", *addressPtr)
124 binaryRequest := parseJson(
125 "scouting/webserver/requests/messages/request_data_scouting.fbs",
126 *requestDataScoutingPtr)
127 response, err := debug.RequestDataScouting(*addressPtr, binaryRequest)
128 if err != nil {
129 log.Fatal("Failed RequestDataScouting: ", err)
130 }
Philipp Schraderd3fac192022-03-02 20:35:46 -0800131 spew.Dump(*response)
132 }
133 if *refreshMatchListPtr != "" {
134 log.Printf("Sending RefreshMatchList to %s", *addressPtr)
135 binaryRequest := parseJson(
136 "scouting/webserver/requests/messages/refresh_match_list.fbs",
137 *refreshMatchListPtr)
138 response, err := debug.RefreshMatchList(*addressPtr, binaryRequest)
139 if err != nil {
140 log.Fatal("Failed RefreshMatchList: ", err)
141 }
142 spew.Dump(*response)
Philipp Schraderacf96232022-03-01 22:03:30 -0800143 }
Philipp Schraderd9096a32022-02-24 17:53:09 -0800144}