Skip to content

Commit 10d31df

Browse files
committed
better support for temporary
1 parent a3b7454 commit 10d31df

File tree

2 files changed

+71
-28
lines changed

2 files changed

+71
-28
lines changed

cli/src/main.rs

Lines changed: 66 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,37 @@ struct Cli {
1919
#[command(subcommand)]
2020
command: Commands,
2121
/// Verbose mode - sets the RUST_LOG level to info, defaults to warning level
22-
#[clap(long, short, action, default_value = "false")]
22+
#[clap(long, short, action, default_value = "false", global = true)]
2323
verbose: bool,
2424
/// Debug mode - sets the RUST_LOG level to debug, defaults to warning level
25-
#[clap(long, action, default_value = "false")]
25+
#[clap(long, action, default_value = "false", global = true)]
2626
debug: bool,
2727
/// Directories to search for ontologies. If not provided, the current directory is used.
28-
#[clap(long, short, num_args = 1..)]
28+
#[clap(long, short, num_args = 1.., global = true)]
2929
search_directories: Option<Vec<PathBuf>>,
3030
/// Resolution policy for determining which ontology to use when there are multiple with the same name
31-
#[clap(long, short, default_value = "default")]
31+
#[clap(long, short, default_value = "default", global = true)]
3232
policy: Option<String>,
3333
/// Temporary (non-persistent) mode - will not save the environment to disk
34-
#[clap(long, short, action)]
34+
#[clap(long, short, action, global = true)]
3535
temporary: bool,
3636
/// Require ontology names to be unique; will raise an error if multiple ontologies have the same name
37-
#[clap(long, action)]
37+
#[clap(long, action, global = true)]
3838
require_ontology_names: bool,
3939
/// Strict mode - will raise an error if an ontology is not found
40-
#[clap(long, action, default_value = "false")]
40+
#[clap(long, action, default_value = "false", global = true)]
4141
strict: bool,
4242
/// Offline mode - will not attempt to fetch ontologies from the web
43-
#[clap(long, short, action, default_value = "false")]
43+
#[clap(long, short, action, default_value = "false", global = true)]
4444
offline: bool,
4545
/// Glob patterns for which files to include, defaults to ['*.ttl','*.xml','*.n3']
46-
#[clap(long, short, num_args = 1..)]
46+
#[clap(long, short, num_args = 1.., global = true)]
4747
includes: Vec<String>,
4848
/// Glob patterns for which files to exclude, defaults to []
49-
#[clap(long, short, num_args = 1..)]
49+
#[clap(long, short, num_args = 1.., global = true)]
5050
excludes: Vec<String>,
5151
/// Do not search for ontologies in the search directories
52-
#[clap(long = "no-search", short = 'n', action)]
52+
#[clap(long = "no-search", short = 'n', action, global = true)]
5353
no_search: bool,
5454
}
5555

@@ -102,7 +102,7 @@ enum Commands {
102102
/// Print out the current state of the ontology environment
103103
Dump {
104104
/// Filter the output to only include ontologies that contain the given string in their
105-
/// name
105+
/// name. Leave empty to include all ontologies.
106106
contains: Option<String>,
107107
},
108108
/// Generate a PDF of the dependency graph
@@ -124,6 +124,26 @@ enum Commands {
124124
Reset,
125125
}
126126

127+
impl ToString for Commands {
128+
fn to_string(&self) -> String {
129+
match self {
130+
Commands::Init { .. } => "Init".to_string(),
131+
Commands::Version => "Version".to_string(),
132+
Commands::Status => "Status".to_string(),
133+
Commands::Refresh => "Refresh".to_string(),
134+
Commands::GetClosure { .. } => "GetClosure".to_string(),
135+
Commands::Add { .. } => "Add".to_string(),
136+
Commands::ListOntologies => "ListOntologies".to_string(),
137+
Commands::ListLocations => "ListLocations".to_string(),
138+
Commands::Dump { .. } => "Dump".to_string(),
139+
Commands::DepGraph { .. } => "DepGraph".to_string(),
140+
Commands::Dependents { .. } => "Dependents".to_string(),
141+
Commands::Doctor => "Doctor".to_string(),
142+
Commands::Reset => "Reset".to_string(),
143+
}
144+
}
145+
}
146+
127147
fn main() -> Result<()> {
128148
let cmd = Cli::parse();
129149

@@ -147,24 +167,30 @@ fn main() -> Result<()> {
147167
cmd.temporary,
148168
)?;
149169

150-
// create the env object to use in the subcommand.
151-
// - if temporary is true, create a new env object each time
152-
// - if temporary is false, load the env from the .ontoenv directory if it exists
153-
let mut env = if cmd.temporary {
154-
let mut env = OntoEnv::init(config, false)?;
155-
env.update()?;
156-
env
157-
} else {
158-
// if the command is NOT init and the .ontoenv directory doesn't exist, raise an error
159-
let path = current_dir()?;
160-
if let Commands::Init { .. } = cmd.command
161-
&& !path.exists()
162-
{
170+
// if not temporary and not init, check if the .ontoenv directory exists
171+
// if it does not exist, raise an error
172+
if !cmd.temporary && cmd.command.to_string() != "Init" {
173+
let path = current_dir()?.join(".ontoenv");
174+
if !path.exists() {
163175
return Err(anyhow::anyhow!(
164176
"OntoEnv not found. Run `ontoenv init` to create a new OntoEnv."
165177
));
166178
}
167-
OntoEnv::load_from_directory(path)?
179+
}
180+
181+
// create the env object to use in the subcommand.
182+
// - if temporary is true, create a new env object each time
183+
// - if temporary is false, load the env from the .ontoenv directory if it exists
184+
let mut env: Option<OntoEnv> = if cmd.temporary {
185+
// Create a new OntoEnv object in temporary mode
186+
let mut e = OntoEnv::init(config.clone(), false)?;
187+
e.update()?;
188+
Some(e)
189+
} else if cmd.command.to_string() != "Init" {
190+
Some(OntoEnv::load_from_directory(current_dir()?)?)
191+
} else {
192+
// Create a new OntoEnv object in non-temporary mode
193+
None
168194
};
169195

170196
match cmd.command {
@@ -175,9 +201,12 @@ fn main() -> Result<()> {
175201
// if temporary, raise an error
176202
if cmd.temporary {
177203
return Err(anyhow::anyhow!(
178-
"Cannot initialize in temporary mode. Run `ontoenv init` to create a new OntoEnv."
204+
"Cannot initialize in temporary mode. Run `ontoenv init` without --temporary."
179205
));
180206
}
207+
208+
let mut env = OntoEnv::init(config, overwrite)?;
209+
181210
// if an ontology config file is provided, load it and add the ontologies
182211
if let Some(file) = ontology_list_file {
183212
let file = File::open(file)?;
@@ -198,6 +227,7 @@ fn main() -> Result<()> {
198227
);
199228
}
200229
Commands::Status => {
230+
let env = env.as_ref().unwrap();
201231
// load env from .ontoenv/ontoenv.json
202232
let status = env.status()?;
203233
// pretty print the status
@@ -210,6 +240,7 @@ fn main() -> Result<()> {
210240
"Cannot refresh in temporary mode. Run `ontoenv init` to create a new OntoEnv."
211241
));
212242
}
243+
let env = env.as_mut().unwrap();
213244
env.update()?;
214245
env.save_to_directory()?;
215246
}
@@ -221,6 +252,7 @@ fn main() -> Result<()> {
221252
} => {
222253
// make ontology an IRI
223254
let iri = NamedNode::new(ontology).map_err(|e| anyhow::anyhow!(e.to_string()))?;
255+
let env = env.as_mut().unwrap();
224256

225257
let graphid = env
226258
.resolve(ResolveTarget::Graph(iri.clone()))
@@ -246,12 +278,14 @@ fn main() -> Result<()> {
246278
(None, Some(file)) => OntologyLocation::File(PathBuf::from(file)),
247279
_ => return Err(anyhow::anyhow!("Must specify either --url or --file")),
248280
};
281+
let env = env.as_mut().unwrap();
249282

250283
env.add(location, true)?;
251284
env.save_to_directory()?;
252285
}
253286
Commands::ListOntologies => {
254287
// print list of ontology URLs from env.onologies.values() sorted alphabetically
288+
let env = env.as_ref().unwrap();
255289
let mut ontologies: Vec<&GraphIdentifier> = env.ontologies().keys().collect();
256290
ontologies.sort_by(|a, b| a.name().cmp(&b.name()));
257291
ontologies.dedup_by(|a, b| a.name() == b.name());
@@ -260,16 +294,19 @@ fn main() -> Result<()> {
260294
}
261295
}
262296
Commands::ListLocations => {
297+
let env = env.as_ref().unwrap();
263298
let mut ontologies: Vec<&GraphIdentifier> = env.ontologies().keys().collect();
264299
ontologies.sort_by(|a, b| a.location().as_str().cmp(b.location().as_str()));
265300
for ont in ontologies {
266301
println!("{}", ont.location().as_str());
267302
}
268303
}
269304
Commands::Dump { contains } => {
305+
let env = env.as_ref().unwrap();
270306
env.dump(contains.as_deref());
271307
}
272308
Commands::DepGraph { roots, output } => {
309+
let env = env.as_mut().unwrap();
273310
let dot = if let Some(roots) = roots {
274311
let roots: Vec<GraphIdentifier> = roots
275312
.iter()
@@ -298,6 +335,7 @@ fn main() -> Result<()> {
298335
}
299336
}
300337
Commands::Dependents { ontologies } => {
338+
let env = env.as_mut().unwrap();
301339
for ont in ontologies {
302340
let iri = NamedNode::new(ont).map_err(|e| anyhow::anyhow!(e.to_string()))?;
303341
let dependents = env.get_dependents(&iri)?;
@@ -308,6 +346,7 @@ fn main() -> Result<()> {
308346
}
309347
}
310348
Commands::Doctor => {
349+
let env = env.as_mut().unwrap();
311350
env.doctor();
312351
}
313352
Commands::Reset => {

lib/src/api.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ impl OntoEnv {
7474
/// Saves the current environment to the .ontoenv directory.
7575
pub fn save_to_directory(&self) -> Result<()> {
7676
if self.config.temporary {
77-
return Err(anyhow::anyhow!("Cannot save a temporary environment"));
77+
warn!("Cannot save a temporary environment");
78+
if self.config.strict {
79+
return Err(anyhow::anyhow!("Cannot save a temporary environment"));
80+
}
81+
return Ok(());
7882
}
7983
let ontoenv_dir = self.config.root.join(".ontoenv");
8084
info!("Saving ontology environment to: {:?}", ontoenv_dir);

0 commit comments

Comments
 (0)