Skip to content

Commit 7fe47ac

Browse files
tarkahermo
authored andcommitted
moss: Add flag to disable system-model warning
1 parent 8ea69d4 commit 7fe47ac

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

moss/src/cli/mod.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ pub fn process() -> Result<(), Error> {
161161
let matches = command().get_matches_from(args);
162162

163163
let show_version = matches.get_one::<bool>("version").is_some_and(|v| *v);
164+
let verbose = matches.get_flag("verbose");
164165

165166
if show_version {
166167
println!("moss {}", tools_buildinfo::get_full_version());
@@ -185,7 +186,7 @@ pub fn process() -> Result<(), Error> {
185186
}
186187

187188
// Print the version, but not if the user is using the version subcommand
188-
if matches.get_flag("verbose")
189+
if verbose
189190
&& let Some(command) = matches.subcommand_name()
190191
&& command != "version"
191192
{
@@ -197,8 +198,12 @@ pub fn process() -> Result<(), Error> {
197198

198199
let installation = Installation::open(root, cache.cloned())?;
199200

200-
if installation.system_model.is_some() {
201-
print_system_model_warning(&installation);
201+
if let Some(system_model) = installation.system_model.as_ref() {
202+
if !system_model.disable_warning {
203+
print_system_model_warning(&installation, false);
204+
} else if verbose {
205+
print_system_model_warning(&installation, true);
206+
}
202207
}
203208

204209
match matches.subcommand() {
@@ -265,20 +270,23 @@ fn replace_aliases(args: env::Args) -> Vec<String> {
265270
args
266271
}
267272

268-
fn print_system_model_warning(installation: &Installation) {
269-
eprintln!(
270-
"{}: {path:?} is present & therefore active.
271-
Hence:
273+
fn print_system_model_warning(installation: &Installation, first_line_only: bool) {
274+
let path = installation.system_model_path();
275+
276+
eprintln!("{}: {path:?} is present & therefore active.", "INFO".green());
277+
278+
if !first_line_only {
279+
eprintln!(
280+
"Hence:
272281
- The system-model is the source of truth and defines all
273282
repositories & installed packages.
274283
- Any changes made via `moss` commands will be temporary
275284
until the system-model is updated.
276285
- The system state can be reverted to match the system-model state
277286
by doing a `moss sync`.
278287
- To disable the system-model, remove or rename {path:?}.",
279-
"INFO".green(),
280-
path = installation.system_model_path(),
281-
);
288+
);
289+
}
282290
}
283291

284292
#[derive(Debug, Error)]

moss/src/system_model.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ mod update;
1616

1717
#[derive(Debug, Clone)]
1818
pub struct SystemModel {
19+
pub disable_warning: bool,
1920
pub repositories: repository::Map,
2021
pub packages: BTreeSet<dependency::Provider>,
2122
encoded: String,
@@ -43,6 +44,7 @@ pub fn create(repositories: repository::Map, packages: BTreeSet<dependency::Prov
4344
let encoded = encode(&repositories, &packages);
4445

4546
SystemModel {
47+
disable_warning: false,
4648
repositories,
4749
packages,
4850
encoded,

moss/src/system_model/decode.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ use crate::{Provider, Repository, SystemModel, dependency, repository};
66
pub fn decode(content: &str) -> Result<SystemModel, Error> {
77
let document: KdlDocument = content.parse().map_err(Error::ParseKdlDocument)?;
88

9+
let disable_warning = document
10+
.get_arg("disable_warning")
11+
.map(|value| {
12+
value
13+
.as_bool()
14+
.ok_or(Error::InvalidRootValue("disable_warning", "bool", value.to_string()))
15+
})
16+
.transpose()?;
17+
918
let packages = document
1019
.get("packages")
1120
.map(|node| node.iter_children().map(decode_package).collect::<Result<_, _>>())
@@ -19,6 +28,7 @@ pub fn decode(content: &str) -> Result<SystemModel, Error> {
1928
.unwrap_or_default();
2029

2130
Ok(SystemModel {
31+
disable_warning: disable_warning.unwrap_or_default(),
2232
repositories,
2333
packages,
2434
encoded: content.to_owned(),
@@ -35,7 +45,7 @@ fn decode_repository(node: &KdlNode) -> Result<(repository::Id, Repository), Err
3545

3646
let description = get_child_value(node, "description")
3747
.map(|value| {
38-
value.as_string().ok_or(Error::InvalidValue(
48+
value.as_string().ok_or(Error::InvalidNodeValue(
3949
"repository",
4050
name.to_owned(),
4151
"description",
@@ -47,7 +57,7 @@ fn decode_repository(node: &KdlNode) -> Result<(repository::Id, Repository), Err
4757
.unwrap_or_default();
4858
let uri = get_child_value(node, "uri")
4959
.map(|value| {
50-
value.as_string().ok_or(Error::InvalidValue(
60+
value.as_string().ok_or(Error::InvalidNodeValue(
5161
"repository",
5262
name.to_owned(),
5363
"uri",
@@ -61,7 +71,7 @@ fn decode_repository(node: &KdlNode) -> Result<(repository::Id, Repository), Err
6171
.map_err(|err| Error::ParseRepositoryUri(err, name.to_owned()))?;
6272
let enabled = get_child_value(node, "enabled")
6373
.map(|value| {
64-
value.as_bool().ok_or(Error::InvalidValue(
74+
value.as_bool().ok_or(Error::InvalidNodeValue(
6575
"repository",
6676
name.to_owned(),
6777
"uri",
@@ -73,7 +83,7 @@ fn decode_repository(node: &KdlNode) -> Result<(repository::Id, Repository), Err
7383
.unwrap_or(true);
7484
let priority = get_child_value(node, "priority")
7585
.map(|value| {
76-
let int = value.as_integer().ok_or(Error::InvalidValue(
86+
let int = value.as_integer().ok_or(Error::InvalidNodeValue(
7787
"repository",
7888
name.to_owned(),
7989
"priority",
@@ -106,8 +116,10 @@ fn get_child_value<'a>(node: &'a KdlNode, name: &str) -> Option<&'a KdlValue> {
106116

107117
#[derive(Debug, Error)]
108118
pub enum Error {
119+
#[error("invalid value for {0}, expected {1} got {2}")]
120+
InvalidRootValue(&'static str, &'static str, String),
109121
#[error("invalid value for {0} {1} {2}, expected {3} got {4}")]
110-
InvalidValue(&'static str, String, &'static str, &'static str, String),
122+
InvalidNodeValue(&'static str, String, &'static str, &'static str, String),
111123
#[error("missing {0} for {1} {2}")]
112124
MissingValue(&'static str, &'static str, String),
113125
#[error("parse as kdl document")]

0 commit comments

Comments
 (0)