|
| 1 | +use clap::{Args, Subcommand}; |
| 2 | +use rbxcloud::rbx::{ |
| 3 | + types::{PlaceId, UniverseId}, |
| 4 | + v2::{luau_execution::LuauExecutionTaskLogView, Client}, |
| 5 | +}; |
| 6 | +use tokio::fs; |
| 7 | + |
| 8 | +#[derive(Debug, Subcommand)] |
| 9 | +pub enum LuauExecutionCommands { |
| 10 | + /// Executes Luau code on Roblox |
| 11 | + Execute { |
| 12 | + /// Universe ID of the experience |
| 13 | + #[clap(short, long, value_parser)] |
| 14 | + universe_id: u64, |
| 15 | + |
| 16 | + /// Place ID of the experience |
| 17 | + #[clap(short = 'i', long, value_parser)] |
| 18 | + place_id: u64, |
| 19 | + |
| 20 | + /// Version ID of the experience |
| 21 | + #[clap(short = 'r', long, value_parser)] |
| 22 | + version_id: Option<String>, |
| 23 | + |
| 24 | + /// Script source code |
| 25 | + #[clap(short, long, value_parser)] |
| 26 | + script: Option<String>, |
| 27 | + |
| 28 | + /// Script source code file |
| 29 | + #[clap(short, long, value_parser)] |
| 30 | + filepath: Option<String>, |
| 31 | + |
| 32 | + /// Execution timeout |
| 33 | + #[clap(short, long, value_parser)] |
| 34 | + timeout: Option<String>, |
| 35 | + |
| 36 | + /// Pretty-print the JSON response |
| 37 | + #[clap(short, long, value_parser, default_value_t = false)] |
| 38 | + pretty: bool, |
| 39 | + |
| 40 | + /// Roblox Open Cloud API Key |
| 41 | + #[clap(short, long, value_parser, env = "RBXCLOUD_API_KEY")] |
| 42 | + api_key: String, |
| 43 | + }, |
| 44 | + |
| 45 | + /// Gets information on a previously executed task |
| 46 | + GetTask { |
| 47 | + /// Universe ID of the experience |
| 48 | + #[clap(short, long, value_parser)] |
| 49 | + universe_id: u64, |
| 50 | + |
| 51 | + /// Place ID of the experience |
| 52 | + #[clap(short = 'i', long, value_parser)] |
| 53 | + place_id: u64, |
| 54 | + |
| 55 | + /// Version ID of the experience |
| 56 | + #[clap(short = 'r', long, value_parser)] |
| 57 | + version_id: Option<String>, |
| 58 | + |
| 59 | + /// Luau execution session ID |
| 60 | + #[clap(short, long, value_parser)] |
| 61 | + session_id: String, |
| 62 | + |
| 63 | + /// Luau execution task ID |
| 64 | + #[clap(short, long, value_parser)] |
| 65 | + task_id: String, |
| 66 | + |
| 67 | + /// Pretty-print the JSON response |
| 68 | + #[clap(short, long, value_parser, default_value_t = false)] |
| 69 | + pretty: bool, |
| 70 | + |
| 71 | + /// Roblox Open Cloud API Key |
| 72 | + #[clap(short, long, value_parser, env = "RBXCLOUD_API_KEY")] |
| 73 | + api_key: String, |
| 74 | + }, |
| 75 | + |
| 76 | + /// Retrieves logs on a previously executed task |
| 77 | + GetLogs { |
| 78 | + /// Universe ID of the experience |
| 79 | + #[clap(short, long, value_parser)] |
| 80 | + universe_id: u64, |
| 81 | + |
| 82 | + /// Place ID of the experience |
| 83 | + #[clap(short = 'i', long, value_parser)] |
| 84 | + place_id: u64, |
| 85 | + |
| 86 | + /// Version ID of the experience |
| 87 | + #[clap(short = 'r', long, value_parser)] |
| 88 | + version_id: Option<String>, |
| 89 | + |
| 90 | + /// Luau execution session ID |
| 91 | + #[clap(short, long, value_parser)] |
| 92 | + session_id: String, |
| 93 | + |
| 94 | + /// Luau execution task ID |
| 95 | + #[clap(short, long, value_parser)] |
| 96 | + task_id: String, |
| 97 | + |
| 98 | + /// Max page size |
| 99 | + #[clap(short, long, value_parser)] |
| 100 | + max_page_size: Option<u32>, |
| 101 | + |
| 102 | + /// Next page token |
| 103 | + #[clap(short = 'n', long, value_parser)] |
| 104 | + page_token: Option<String>, |
| 105 | + |
| 106 | + /// Log view type |
| 107 | + #[clap(short = 'w', long, value_enum)] |
| 108 | + view: Option<LuauExecutionTaskLogView>, |
| 109 | + |
| 110 | + /// Pretty-print the JSON response |
| 111 | + #[clap(short, long, value_parser, default_value_t = false)] |
| 112 | + pretty: bool, |
| 113 | + |
| 114 | + /// Roblox Open Cloud API Key |
| 115 | + #[clap(short, long, value_parser, env = "RBXCLOUD_API_KEY")] |
| 116 | + api_key: String, |
| 117 | + }, |
| 118 | +} |
| 119 | + |
| 120 | +#[derive(Debug, Args)] |
| 121 | +pub struct Luau { |
| 122 | + #[clap(subcommand)] |
| 123 | + command: LuauExecutionCommands, |
| 124 | +} |
| 125 | + |
| 126 | +impl Luau { |
| 127 | + pub async fn run(self) -> anyhow::Result<Option<String>> { |
| 128 | + match self.command { |
| 129 | + LuauExecutionCommands::Execute { |
| 130 | + universe_id, |
| 131 | + place_id, |
| 132 | + version_id, |
| 133 | + script, |
| 134 | + filepath, |
| 135 | + timeout, |
| 136 | + pretty, |
| 137 | + api_key, |
| 138 | + } => { |
| 139 | + let client = Client::new(&api_key); |
| 140 | + |
| 141 | + let luau = client.luau(UniverseId(universe_id), PlaceId(place_id), version_id); |
| 142 | + |
| 143 | + let mut script_source: Option<String> = None; |
| 144 | + if script.is_some() { |
| 145 | + script_source = script; |
| 146 | + } |
| 147 | + |
| 148 | + if script_source.is_none() { |
| 149 | + if let Some(path) = filepath { |
| 150 | + let src_bytes = fs::read(path).await?; |
| 151 | + script_source = Some(String::from_utf8(src_bytes)?); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + let src = script_source.expect("script or filepath must be set"); |
| 156 | + |
| 157 | + let res = luau.create_task(src, timeout).await; |
| 158 | + match res { |
| 159 | + Ok(data) => { |
| 160 | + let r = if pretty { |
| 161 | + serde_json::to_string_pretty(&data)? |
| 162 | + } else { |
| 163 | + serde_json::to_string(&data)? |
| 164 | + }; |
| 165 | + Ok(Some(r)) |
| 166 | + } |
| 167 | + Err(err) => Err(anyhow::anyhow!(err)), |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + LuauExecutionCommands::GetTask { |
| 172 | + universe_id, |
| 173 | + place_id, |
| 174 | + version_id, |
| 175 | + session_id, |
| 176 | + task_id, |
| 177 | + pretty, |
| 178 | + api_key, |
| 179 | + } => { |
| 180 | + let client = Client::new(&api_key); |
| 181 | + |
| 182 | + let luau = client.luau(UniverseId(universe_id), PlaceId(place_id), version_id); |
| 183 | + |
| 184 | + let res = luau.get_task(session_id, task_id).await; |
| 185 | + match res { |
| 186 | + Ok(data) => { |
| 187 | + let r = if pretty { |
| 188 | + serde_json::to_string_pretty(&data)? |
| 189 | + } else { |
| 190 | + serde_json::to_string(&data)? |
| 191 | + }; |
| 192 | + Ok(Some(r)) |
| 193 | + } |
| 194 | + Err(err) => Err(anyhow::anyhow!(err)), |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + LuauExecutionCommands::GetLogs { |
| 199 | + universe_id, |
| 200 | + place_id, |
| 201 | + version_id, |
| 202 | + session_id, |
| 203 | + task_id, |
| 204 | + max_page_size, |
| 205 | + page_token, |
| 206 | + view, |
| 207 | + pretty, |
| 208 | + api_key, |
| 209 | + } => { |
| 210 | + let client = Client::new(&api_key); |
| 211 | + |
| 212 | + let luau = client.luau(UniverseId(universe_id), PlaceId(place_id), version_id); |
| 213 | + |
| 214 | + let res = luau |
| 215 | + .get_logs( |
| 216 | + session_id, |
| 217 | + task_id, |
| 218 | + view.unwrap_or(LuauExecutionTaskLogView::Flat), |
| 219 | + max_page_size, |
| 220 | + page_token, |
| 221 | + ) |
| 222 | + .await; |
| 223 | + match res { |
| 224 | + Ok(data) => { |
| 225 | + let r = if pretty { |
| 226 | + serde_json::to_string_pretty(&data)? |
| 227 | + } else { |
| 228 | + serde_json::to_string(&data)? |
| 229 | + }; |
| 230 | + Ok(Some(r)) |
| 231 | + } |
| 232 | + Err(err) => Err(anyhow::anyhow!(err)), |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + } |
| 237 | +} |
0 commit comments