Skip to content

Commit 46d487e

Browse files
committed
cleaning up and providing force flag for reset
1 parent 587d7cd commit 46d487e

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

cli/src/main.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ enum Commands {
120120
/// Run the doctor to check the environment for issues
121121
Doctor,
122122
/// Reset the ontology environment by removing the .ontoenv directory
123-
Reset,
123+
Reset {
124+
#[clap(long, short, action = clap::ArgAction::Set, default_value = "false")]
125+
force: bool,
126+
},
124127
}
125128

126129
impl ToString for Commands {
@@ -138,7 +141,7 @@ impl ToString for Commands {
138141
Commands::DepGraph { .. } => "DepGraph".to_string(),
139142
Commands::Dependents { .. } => "Dependents".to_string(),
140143
Commands::Doctor => "Doctor".to_string(),
141-
Commands::Reset => "Reset".to_string(),
144+
Commands::Reset { .. } => "Reset".to_string(),
142145
}
143146
}
144147
}
@@ -165,24 +168,25 @@ fn main() -> Result<()> {
165168
false,
166169
cmd.temporary,
167170
)?;
168-
config.print();
171+
if cmd.verbose || cmd.debug {
172+
config.print();
173+
}
174+
let ontoenv_exists = current_dir()?.join(".ontoenv").exists();
169175

170176
// create the env object to use in the subcommand.
171177
// - if temporary is true, create a new env object each time
172178
// - if temporary is false, load the env from the .ontoenv directory if it exists
173-
let mut env: Option<OntoEnv> = None;
174-
175-
if cmd.temporary {
179+
let env: Option<OntoEnv> = if cmd.temporary {
176180
// Create a new OntoEnv object in temporary mode
177181
let mut e = OntoEnv::init(config.clone(), false)?;
178182
e.update()?;
179-
env = Some(e);
180-
} else if cmd.command.to_string() != "Init" {
183+
Some(e)
184+
} else if cmd.command.to_string() != "Init" && ontoenv_exists{
181185
// if .ontoenv exists, load it
182-
if current_dir()?.join(".ontoenv").exists() {
183-
env = Some(OntoEnv::load_from_directory(current_dir()?, false)?); // no read-only
184-
}
185-
}
186+
Some(OntoEnv::load_from_directory(current_dir()?, false)?) // no read-only
187+
} else {
188+
None
189+
};
186190

187191
match cmd.command {
188192
Commands::Init {
@@ -334,14 +338,14 @@ fn main() -> Result<()> {
334338
let env = require_ontoenv(env)?;
335339
env.doctor();
336340
}
337-
Commands::Reset => {
341+
Commands::Reset { force } => {
338342
// remove .ontoenv directory
339343
let path = current_dir()?.join(".ontoenv");
340344
println!("Removing .ontoenv directory at {}...", path.display());
341-
if path.exists() {
345+
if path.exists() && !force {
342346
// check delete? [y/N]
343347
let mut input = String::new();
344-
println!("Are you sure you want to delete the .ontoenv directory? [y/N]");
348+
println!("Are you sure you want to delete the .ontoenv directory? [y/N] ");
345349
std::io::stdin()
346350
.read_line(&mut input)
347351
.expect("Failed to read line");
@@ -360,6 +364,6 @@ fn main() -> Result<()> {
360364

361365
fn require_ontoenv(env: Option<OntoEnv>) -> Result<OntoEnv> {
362366
env.ok_or_else(|| {
363-
anyhow::anyhow!("OntoEnv not found. Run `ontoenv init` to create a new OntoEnv or use -t/--temporary to create a temporary environment.")
367+
anyhow::anyhow!("OntoEnv not found. Run `ontoenv init` to create a new OntoEnv or use -t/--temporary to use a temporary environment.")
364368
})
365369
}

0 commit comments

Comments
 (0)