Skip to main content

flowey/
lib.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#![expect(missing_docs)]
5#![forbid(unsafe_code)]
6
7//! The user-facing flowey API.
8//!
9//! Relying on `flowey_core` directly is not advised, as many APIs exposed at
10//! that level are only supposed to be used by flowey _infrastructure_ (e.g: in
11//! `flowey_cli`).
12
13pub use flowey_core::shell_cmd;
14
15/// Shell abstractions for flowey command execution.
16pub mod shell {
17    pub use flowey_core::shell::*;
18}
19
20/// Types and traits for implementing flowey nodes.
21pub mod node {
22    pub mod prelude {
23        // include all user-facing types in the prelude
24        pub use flowey_core::claim_vars;
25        pub use flowey_core::match_arch;
26        pub use flowey_core::node::user_facing::*;
27        pub use flowey_core::read_vars;
28
29        // ...in addition, export various types/traits that node impls are
30        // almost certainly going to require
31        pub use anyhow;
32        pub use anyhow::Context;
33        pub use fs_err;
34        pub use log;
35        pub use serde::Deserialize;
36        pub use serde::Serialize;
37        pub use std::path::Path;
38        pub use std::path::PathBuf;
39
40        /// Extension trait to streamline working with [`Path`] in flowey.
41        pub trait FloweyPathExt {
42            /// Alias for [`std::path::absolute`]
43            fn absolute(&self) -> std::io::Result<PathBuf>;
44
45            /// Helper to make files executable on unix-like platforms
46            fn make_executable(&self) -> std::io::Result<()>;
47
48            /// Helper to check if a file is executable on unix-like platforms
49            fn is_executable(&self) -> std::io::Result<bool>;
50        }
51
52        impl<T> FloweyPathExt for T
53        where
54            T: AsRef<Path>,
55        {
56            fn absolute(&self) -> std::io::Result<PathBuf> {
57                std::path::absolute(self)
58            }
59
60            fn make_executable(&self) -> std::io::Result<()> {
61                #[cfg(unix)]
62                {
63                    use std::os::unix::fs::PermissionsExt;
64                    let path = self.as_ref();
65                    let old_mode = path.metadata()?.permissions().mode();
66                    fs_err::set_permissions(
67                        path,
68                        std::fs::Permissions::from_mode(old_mode | 0o111),
69                    )?;
70                }
71                Ok(())
72            }
73
74            fn is_executable(&self) -> std::io::Result<bool> {
75                #[cfg(unix)]
76                {
77                    use std::os::unix::fs::PermissionsExt;
78                    let path = self.as_ref();
79                    let mode = path.metadata()?.permissions().mode();
80                    Ok(mode & 0o111 != 0)
81                }
82
83                // TODO: Implement for windows. Tracked by https://github.com/microsoft/openvmm/issues/2622
84                #[cfg(not(unix))]
85                {
86                    Ok(true)
87                }
88            }
89        }
90    }
91}
92
93/// Types and traits for implementing flowey pipelines.
94pub mod pipeline {
95    pub mod prelude {
96        pub use flowey_core::pipeline::user_facing::*;
97    }
98}
99
100/// Types and traits for implementing flowey patch functions.
101pub mod patch {
102    pub use flowey_core::patch::*;
103    pub use flowey_core::register_patch;
104}
105
106/// Utility functions.
107pub mod util {
108    pub use flowey_core::util::*;
109}