blue_build/
commands.rs

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