Skip to main content

blue_build/
commands.rs

1use std::path::PathBuf;
2
3use blue_build_utils::constants::BB_NO_LOG_FILTER;
4use log::error;
5
6use clap::{Parser, Subcommand, crate_authors};
7use clap_verbosity_flag::{InfoLevel, Verbosity};
8
9use crate::shadow;
10
11pub mod bug_report;
12pub mod build;
13pub mod completions;
14pub mod generate;
15pub mod generate_iso;
16pub mod init;
17pub mod login;
18pub mod prune;
19pub mod switch;
20pub mod validate;
21
22pub trait BlueBuildCommand {
23    /// Runs the command and returns a result
24    /// of the execution
25    ///
26    /// # Errors
27    /// Can return an `anyhow` Error
28    fn try_run(&mut self) -> miette::Result<()>;
29
30    /// Runs the command and exits if there is an error.
31    fn run(&mut self) {
32        if let Err(e) = self.try_run() {
33            error!("Failed:\n{e:?}");
34            std::process::exit(1);
35        }
36        std::process::exit(0);
37    }
38}
39
40#[derive(Parser, Debug)]
41#[clap(
42    name = "BlueBuild",
43    about,
44    long_about = None,
45    author=crate_authors!(),
46    version=shadow::PKG_VERSION,
47    long_version=shadow::CLAP_LONG_VERSION,
48)]
49pub struct BlueBuildArgs {
50    #[command(subcommand)]
51    pub command: CommandArgs,
52
53    /// Allows printing logs from all crates.
54    ///
55    /// WARNING: This can print sensitive information.
56    /// Be sure to run this in a non-public setting.
57    #[arg(long, env = BB_NO_LOG_FILTER)]
58    pub no_log_filter: bool,
59
60    /// The directory to output build logs.
61    #[arg(long)]
62    pub log_out: Option<PathBuf>,
63
64    #[clap(flatten)]
65    pub verbosity: Verbosity<InfoLevel>,
66}
67
68#[derive(Debug, Subcommand)]
69pub enum CommandArgs {
70    /// Build an image from a recipe
71    Build(build::BuildCommand),
72
73    /// Generate a Containerfile from a recipe
74    #[clap(visible_alias = "template")]
75    Generate(generate::GenerateCommand),
76
77    /// Generate an ISO for an image or recipe.
78    GenerateIso(generate_iso::GenerateIsoCommand),
79
80    /// Switch your current OS onto the image
81    /// being built.
82    ///
83    /// This will create a tarball of your image at
84    /// `/etc/bluebuild/` and invoke `rpm-ostree` to
85    /// rebase/upgrade onto the image using `oci-archive`.
86    ///
87    /// NOTE: This can only be used if you have `rpm-ostree`
88    /// installed. This image will not be signed.
89    #[command(
90        visible_alias("update"),
91        visible_alias("upgrade"),
92        visible_alias("rebase")
93    )]
94    Switch(switch::SwitchCommand),
95
96    /// Login to all services used for building.
97    Login(login::LoginCommand),
98
99    /// Create a new bluebuild project.
100    New(init::NewCommand),
101
102    /// Create a new bluebuild project.
103    Init(init::InitCommand),
104
105    /// Validate your recipe file and display
106    /// errors to help fix problems.
107    Validate(Box<validate::ValidateCommand>),
108
109    /// Clean up cache and images for build drivers.
110    Prune(prune::PruneCommand),
111
112    /// Create a pre-populated GitHub issue with information about your configuration
113    BugReport(bug_report::BugReportCommand),
114
115    /// Generate shell completions for your shell to stdout
116    Completions(completions::CompletionsCommand),
117}
118
119#[cfg(test)]
120mod test {
121    use clap::CommandFactory;
122
123    use super::BlueBuildArgs;
124
125    #[test]
126    fn test_cli() {
127        BlueBuildArgs::command().debug_assert();
128    }
129}