|
| 1 | +use crate::{book::Book, runtime::CommandLine}; |
| 2 | + |
| 3 | +use super::ValidateArgs; |
| 4 | + |
| 5 | +pub(crate) async fn validate(args: ValidateArgs) -> anyhow::Result<()> { |
| 6 | + let book = Book::from_path(args.path.clone(), None)?; |
| 7 | + |
| 8 | + // we need at least one page |
| 9 | + if book.pages.is_empty() { |
| 10 | + return Err(anyhow::anyhow!("no pages found in {:?}", &args.path)); |
| 11 | + } |
| 12 | + |
| 13 | + for (page_path, page) in book.pages { |
| 14 | + log::info!("validating {:?} ...", page_path); |
| 15 | + |
| 16 | + // and at least one function per page, at least what's the point of the page? |
| 17 | + if page.functions.is_empty() { |
| 18 | + return Err(anyhow::anyhow!("no functions found in {:?}", page_path)); |
| 19 | + } else if page.name.is_empty() { |
| 20 | + // set by Book::from_path if not specified |
| 21 | + return Err(anyhow::anyhow!("page name is empty in {:?}", page_path)); |
| 22 | + } else if page.categories.is_empty() { |
| 23 | + // set by Book::from_path if not specified |
| 24 | + return Err(anyhow::anyhow!( |
| 25 | + "page categories are empty in {:?}", |
| 26 | + page_path |
| 27 | + )); |
| 28 | + } |
| 29 | + |
| 30 | + for (func_name, func) in page.functions { |
| 31 | + // the model needs at least a name and a description |
| 32 | + if func_name.is_empty() { |
| 33 | + return Err(anyhow::anyhow!("function name is empty in {:?}", page_path)); |
| 34 | + } else if func.description.is_empty() { |
| 35 | + return Err(anyhow::anyhow!( |
| 36 | + "function description is empty in {:?}", |
| 37 | + page_path |
| 38 | + )); |
| 39 | + } |
| 40 | + |
| 41 | + if func.parameters.is_empty() { |
| 42 | + return Err(anyhow::anyhow!( |
| 43 | + "function {} parameters are empty in {:?}", |
| 44 | + func_name, |
| 45 | + page_path |
| 46 | + )); |
| 47 | + } |
| 48 | + |
| 49 | + // make sure the function resolves to a valid command line |
| 50 | + let cmdline = func.execution.get_command_line().map_err(|e| { |
| 51 | + anyhow::anyhow!( |
| 52 | + "error while getting command line for function {}: {}", |
| 53 | + func_name, |
| 54 | + e |
| 55 | + ) |
| 56 | + })?; |
| 57 | + |
| 58 | + if cmdline.is_empty() { |
| 59 | + return Err(anyhow::anyhow!( |
| 60 | + "command line is empty for function {} in {:?}", |
| 61 | + func_name, |
| 62 | + page_path |
| 63 | + )); |
| 64 | + } |
| 65 | + |
| 66 | + let cmdline = CommandLine::from_vec(&cmdline).map_err(|e| { |
| 67 | + anyhow::anyhow!( |
| 68 | + "error while parsing command line for function {}: {}", |
| 69 | + func_name, |
| 70 | + e |
| 71 | + ) |
| 72 | + })?; |
| 73 | + |
| 74 | + // validate container requirements - a container is required if: |
| 75 | + let container = if !cmdline.app_in_path { |
| 76 | + // the binary is not in $PATH |
| 77 | + if let Some(container) = &func.container { |
| 78 | + Some(container) |
| 79 | + } else { |
| 80 | + return Err(anyhow::anyhow!( |
| 81 | + "binary for function {} in {:?} not in $PATH and container not specified", |
| 82 | + func_name, |
| 83 | + page_path |
| 84 | + )); |
| 85 | + } |
| 86 | + } else if func.container.is_some() && func.container.as_ref().unwrap().force { |
| 87 | + // it's set and forced |
| 88 | + Some(func.container.as_ref().unwrap()) |
| 89 | + } else { |
| 90 | + None |
| 91 | + }; |
| 92 | + |
| 93 | + // validate the container if any |
| 94 | + if let Some(container) = container { |
| 95 | + if args.skip_docker { |
| 96 | + // or not :P |
| 97 | + log::warn!("skipping container resolution for function {}", func_name); |
| 98 | + } else { |
| 99 | + // this will pull or build the image |
| 100 | + container.resolve().await.map_err(|e| { |
| 101 | + anyhow::anyhow!( |
| 102 | + "error while resolving container for function {} in {}: {}", |
| 103 | + func_name, |
| 104 | + page_path, |
| 105 | + e |
| 106 | + ) |
| 107 | + })?; |
| 108 | + |
| 109 | + // if volumes are defined make sure they exist |
| 110 | + if let Some(volumes) = &container.volumes { |
| 111 | + for volume in volumes { |
| 112 | + let (on_host, on_guest) = |
| 113 | + volume.split_once(':').unwrap_or((volume, volume)); |
| 114 | + |
| 115 | + let on_host = shellexpand::full(on_host) |
| 116 | + .map_err(|e| { |
| 117 | + anyhow::anyhow!( |
| 118 | + "error while expanding volume path for function {}: {}", |
| 119 | + func_name, |
| 120 | + e |
| 121 | + ) |
| 122 | + })? |
| 123 | + .to_string(); |
| 124 | + |
| 125 | + if !std::path::Path::new(&on_host).exists() { |
| 126 | + return Err(anyhow::anyhow!( |
| 127 | + "page {}, function {}, path {} for volume '{}' does not exist", |
| 128 | + page_path, |
| 129 | + func_name, |
| 130 | + on_host, |
| 131 | + on_guest |
| 132 | + )); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + log::info!(" {} - ok", func_name); |
| 140 | + log::debug!(" cmdline = {:?}", cmdline); |
| 141 | + if let Some(container) = container { |
| 142 | + log::debug!(" container = {:?}", container); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + Ok(()) |
| 148 | +} |
0 commit comments