vrl/stdlib/
get_hostname.rs

1use crate::compiler::prelude::*;
2
3#[cfg(not(target_arch = "wasm32"))]
4fn get_hostname() -> Resolved {
5    Ok(hostname::get()
6        .map_err(|error| format!("failed to get hostname: {error}"))?
7        .to_string_lossy()
8        .into())
9}
10
11#[derive(Clone, Copy, Debug)]
12pub struct GetHostname;
13
14impl Function for GetHostname {
15    fn identifier(&self) -> &'static str {
16        "get_hostname"
17    }
18
19    fn usage(&self) -> &'static str {
20        "Returns the local system's hostname."
21    }
22
23    fn category(&self) -> &'static str {
24        Category::System.as_ref()
25    }
26
27    fn internal_failure_reasons(&self) -> &'static [&'static str] {
28        &["Internal hostname resolution failed."]
29    }
30
31    fn return_kind(&self) -> u16 {
32        kind::BYTES
33    }
34
35    #[cfg(not(target_arch = "wasm32"))]
36    fn compile(
37        &self,
38        _state: &state::TypeState,
39        _ctx: &mut FunctionCompileContext,
40        _: ArgumentList,
41    ) -> Compiled {
42        Ok(GetHostnameFn.as_expr())
43    }
44
45    #[cfg(target_arch = "wasm32")]
46    fn compile(
47        &self,
48        _state: &state::TypeState,
49        ctx: &mut FunctionCompileContext,
50        _: ArgumentList,
51    ) -> Compiled {
52        Ok(super::WasmUnsupportedFunction::new(ctx.span(), TypeDef::bytes().fallible()).as_expr())
53    }
54
55    fn examples(&self) -> &'static [Example] {
56        &[example! {
57            title: "Get hostname",
58            source: "get_hostname!()",
59            result: Ok("my-hostname"),
60            deterministic: false,
61        }]
62    }
63}
64
65#[cfg(not(target_arch = "wasm32"))]
66#[derive(Debug, Clone)]
67struct GetHostnameFn;
68
69#[cfg(not(target_arch = "wasm32"))]
70impl FunctionExpression for GetHostnameFn {
71    fn resolve(&self, _: &mut Context) -> Resolved {
72        get_hostname()
73    }
74
75    fn type_def(&self, _: &state::TypeState) -> TypeDef {
76        TypeDef::bytes().fallible()
77    }
78}