1use std::path::PathBuf;
2
3use crate::{Dofigen, DofigenContext, Error, Resource, Result, lock::LockFile};
4
5pub fn get_file_path(path: &Option<String>) -> Result<String> {
6 if let Some(path) = path {
7 Ok(path.clone())
8 } else {
9 let mut files = vec!["dofigen.yml", "dofigen.yaml", "dofigen.json"];
10 files.retain(|f| std::path::Path::new(f).exists());
11 if files.is_empty() {
12 return Err(Error::Custom("No Dofigen file found".into()));
13 }
14 Ok(files[0].into())
15 }
16}
17
18pub fn get_lockfile_path(path: String) -> Option<PathBuf> {
19 if path == "-" {
20 None
21 } else {
22 Some(PathBuf::from(path).with_extension("lock"))
23 }
24}
25
26pub fn get_image_from_path(path: String, context: &mut DofigenContext) -> Result<Dofigen> {
27 if path == "-" {
28 context.parse_from_reader(std::io::stdin())
29 } else {
30 context.parse_from_resource(path.parse()?)
31 }
32}
33
34pub fn load_lockfile(path: Option<PathBuf>) -> Option<LockFile> {
35 path.map(|path| {
36 if path.exists() {
37 let mut context = DofigenContext::new();
38 context.display_updates = false;
39 Resource::File(path).load(&mut context).ok()
40 } else {
41 None
42 }
43 })
44 .flatten()
45}