-
Notifications
You must be signed in to change notification settings - Fork 157
system-reinstall-bootc: Handle --help #1651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+57
−43
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,52 +1,31 @@ | ||
| use anyhow::{ensure, Context, Result}; | ||
| use clap::Parser; | ||
| use std::{fs::File, io::BufReader}; | ||
|
|
||
| use anyhow::{Context, Result}; | ||
| use bootc_utils::PathQuotedDisplay; | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| mod cli; | ||
|
|
||
| /// The environment variable that can be used to specify an image. | ||
| const CONFIG_VAR: &str = "BOOTC_REINSTALL_CONFIG"; | ||
|
|
||
| #[derive(Debug, Deserialize, Serialize)] | ||
| #[serde(deny_unknown_fields)] | ||
| pub(crate) struct ReinstallConfig { | ||
| /// The bootc image to install on the system. | ||
| pub(crate) bootc_image: String, | ||
|
|
||
| /// The raw CLI arguments that were used to invoke the program. None if the config was loaded | ||
| /// from a file. | ||
| #[serde(skip_deserializing)] | ||
| cli_flags: Option<Vec<String>>, | ||
| } | ||
|
|
||
| impl ReinstallConfig { | ||
| pub fn parse_from_cli(cli: cli::Cli) -> Self { | ||
| Self { | ||
| bootc_image: cli.bootc_image, | ||
| cli_flags: Some(std::env::args().collect::<Vec<String>>()), | ||
| } | ||
| } | ||
|
|
||
| pub fn load() -> Result<Self> { | ||
| Ok(match std::env::var("BOOTC_REINSTALL_CONFIG") { | ||
| Ok(config_path) => { | ||
| ensure_no_cli_args()?; | ||
|
|
||
| serde_yaml::from_slice( | ||
| &std::fs::read(&config_path) | ||
| .context("reading BOOTC_REINSTALL_CONFIG file {config_path}")?, | ||
| ) | ||
| .context("parsing BOOTC_REINSTALL_CONFIG file {config_path}")? | ||
| } | ||
| Err(_) => ReinstallConfig::parse_from_cli(cli::Cli::parse()), | ||
| }) | ||
| pub fn load() -> Result<Option<Self>> { | ||
| let Some(config) = std::env::var_os(CONFIG_VAR) else { | ||
| return Ok(None); | ||
| }; | ||
| let f = File::open(&config) | ||
| .with_context(|| format!("Opening {}", PathQuotedDisplay::new(&config))) | ||
| .map(BufReader::new)?; | ||
| let r = serde_yaml::from_reader(f) | ||
| .with_context(|| format!("Parsing config from {}", PathQuotedDisplay::new(&config)))?; | ||
| Ok(Some(r)) | ||
| } | ||
| } | ||
|
|
||
| fn ensure_no_cli_args() -> Result<()> { | ||
| let num_args = std::env::args().len(); | ||
|
|
||
| ensure!( | ||
| num_args == 1, | ||
| "BOOTC_REINSTALL_CONFIG is set, but there are {num_args} CLI arguments. BOOTC_REINSTALL_CONFIG is meant to be used with no arguments." | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.