(&self)
| 124 | impl Invoke { |
| 125 | #[tracing::instrument(skip(self), target = "cargo_lambda")] |
| 126 | pub async fn run(&self) -> Result<()> { |
| 127 | tracing::trace!(options = ?self, "invoking function"); |
| 128 | |
| 129 | let data = if let Some(file) = &self.data_file { |
| 130 | read_to_string(file) |
| 131 | .into_diagnostic() |
| 132 | .wrap_err("error reading data file")? |
| 133 | } else if let Some(data) = &self.data_ascii { |
| 134 | data.clone() |
| 135 | } else if let Some(example) = &self.data_example { |
| 136 | let name = example_name(example); |
| 137 | |
| 138 | let cache = dirs::cache_dir() |
| 139 | .map(|p| p.join("cargo-lambda").join("invoke-fixtures").join(&name)); |
| 140 | |
| 141 | match cache { |
| 142 | Some(cache) if !self.skip_cache && cache.exists() => { |
| 143 | tracing::debug!(?cache, "using example from cache"); |
| 144 | read_to_string(cache) |
| 145 | .into_diagnostic() |
| 146 | .wrap_err("error reading data file")? |
| 147 | } |
| 148 | _ if self.skip_cache => download_example(&name, None, None).await?, |
| 149 | _ => download_example(&name, cache, None).await?, |
| 150 | } |
| 151 | } else { |
| 152 | return Err(InvokeError::MissingPayload.into()); |
| 153 | }; |
| 154 | |
| 155 | let text = if self.remote { |
| 156 | self.invoke_remote(&data).await? |
| 157 | } else { |
| 158 | self.invoke_local(&data).await? |
| 159 | }; |
| 160 | |
| 161 | let text = match &self.output_format { |
| 162 | OutputFormat::Text => text, |
| 163 | OutputFormat::Json => { |
| 164 | let obj: Value = from_str(&text) |
| 165 | .into_diagnostic() |
| 166 | .wrap_err("failed to serialize response into json")?; |
| 167 | |
| 168 | to_string_pretty(&obj) |
| 169 | .into_diagnostic() |
| 170 | .wrap_err("failed to format json output")? |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | println!("{text}"); |
| 175 | |
| 176 | Ok(()) |
| 177 | } |
| 178 | |
| 179 | async fn invoke_remote(&self, data: &str) -> Result<String> { |
| 180 | if self.function_name == DEFAULT_PACKAGE_FUNCTION { |
nothing calls this directly
no test coverage detected