blob: 204e54173ce0eab6fcf5b1f99753e09cca88ec3e [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
14 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/debug"
15)
16
17// Returns the absolute path of the specified path. This is an unwrapped
18// version of `filepath.Abs`.
19func absPath(path string) string {
20 result, err := filepath.Abs(path)
21 if err != nil {
22 log.Fatal("Failed to determine absolute path for ", path, ": ", err)
23 }
24 return result
25}
26
27// Parses the specified JSON file into a binary version (i.e. serialized
28// flatbuffer). This uses the `flatc` binary and the JSON's corresponding
29// `.fbs` file.
30func parseJson(fbsPath string, jsonPath string) []byte {
31 // Work inside a temporary directory since `flatc` doesn't allow us to
32 // customize the name of the output file.
33 dir, err := ioutil.TempDir("", "webserver_debug_cli")
34 if err != nil {
35 log.Fatal("Failed to create temporary directory: ", err)
36 }
37 defer os.RemoveAll(dir)
38
39 // Turn these paths absolute so that it everything still works from
40 // inside the temporary directory.
41 absFlatcPath := absPath("external/com_github_google_flatbuffers/flatc")
42 absFbsPath := absPath(fbsPath)
43
44 // Create a symlink to the .fbs file so that the output filename that
45 // `flatc` generates is predictable. I.e. `fb.json` gets serialized
46 // into `fb.bin`.
47 jsonSymlink := filepath.Join(dir, "fb.json")
48 os.Symlink(jsonPath, jsonSymlink)
49
50 // Execute the `flatc` command.
51 flatcCommand := exec.Command(absFlatcPath, "--binary", absFbsPath, jsonSymlink)
52 flatcCommand.Dir = dir
Philipp Schradercbf5c6a2022-02-27 23:25:19 -080053 output, err := flatcCommand.CombinedOutput()
Philipp Schraderd9096a32022-02-24 17:53:09 -080054 if err != nil {
Philipp Schradercbf5c6a2022-02-27 23:25:19 -080055 log.Fatal("Failed to execute flatc: ", err, ": ", string(output))
Philipp Schraderd9096a32022-02-24 17:53:09 -080056 }
57
58 // Read the serialized flatbuffer and return it.
59 binaryPath := filepath.Join(dir, "fb.bin")
60 binaryFb, err := os.ReadFile(binaryPath)
61 if err != nil {
62 log.Fatal("Failed to read flatc output ", binaryPath, ": ", err)
63 }
64 return binaryFb
65}
66
67func main() {
68 // Parse command line arguments.
69 addressPtr := flag.String("address", "http://localhost:8080",
70 "The end point where the server is listening.")
71 submitDataScoutingPtr := flag.String("submitDataScouting", "",
72 "If specified, parse the file as a SubmitDataScouting JSON request.")
Philipp Schradercbf5c6a2022-02-27 23:25:19 -080073 requestAllMatchesPtr := flag.String("requestAllMatches", "",
74 "If specified, parse the file as a RequestAllMatches JSON request.")
Philipp Schraderd9096a32022-02-24 17:53:09 -080075 flag.Parse()
76
77 // Handle the actual arguments.
78 if *submitDataScoutingPtr != "" {
79 log.Printf("Sending SubmitDataScouting to %s", *addressPtr)
80 binaryRequest := parseJson(
81 "scouting/webserver/requests/messages/submit_data_scouting.fbs",
82 *submitDataScoutingPtr)
83 response, err := debug.SubmitDataScouting(*addressPtr, binaryRequest)
84 if err != nil {
85 log.Fatal("Failed SubmitDataScouting: ", err)
86 }
Philipp Schradercbf5c6a2022-02-27 23:25:19 -080087 log.Printf("%+v", *response)
88 }
89 if *requestAllMatchesPtr != "" {
90 log.Printf("Sending RequestAllMatches to %s", *addressPtr)
91 binaryRequest := parseJson(
92 "scouting/webserver/requests/messages/request_all_matches.fbs",
93 *requestAllMatchesPtr)
94 response, err := debug.RequestAllMatches(*addressPtr, binaryRequest)
95 if err != nil {
96 log.Fatal("Failed RequestAllMatches: ", err)
97 }
98 log.Printf("%+v", *response)
Philipp Schraderd9096a32022-02-24 17:53:09 -080099 }
100}