hac_cli/lib.rs
1use std::path::Path;
2
3use clap::Parser;
4
5/// How the runtime should behave. Dictated by the flags provided to `Cli`
6#[derive(Debug, PartialEq)]
7pub enum RuntimeBehavior {
8 /// will print all directories `HAC` is looking for a configuration file
9 /// that means. Will print wether or not HAC_CONFIG is set, and if so where
10 /// it points to, will print `$XDG_CONFIG_HOME`, and also `$HOME/.config`
11 PrintConfigPath,
12 /// will print all directories `HAC` is looking for collections, this will
13 /// also print the path specified on the configuration file, if any.
14 PrintDataPath,
15 /// will dump the default configuration to stdout instead of running the
16 /// application.
17 DumpDefaultConfig,
18 /// will run the application with all disk-synchronization disabled. That
19 /// means `HAC` wont't save any files or changes to collection to disk.
20 DryRun,
21 /// the default running behavior of the application, this is the default
22 /// behavior for `HAC`.
23 Run,
24}
25
26#[derive(Parser, Debug)]
27pub struct Cli {
28 /// prints the directory in which the config file is being loaded from
29 #[arg(long)]
30 config_dir: bool,
31 /// dumps the default configuration to stdout.
32 #[arg(long)]
33 config_dump: bool,
34 /// prints the directory in which the collections are being stored
35 #[arg(long)]
36 data_dir: bool,
37 /// wether or not we should sync changes to the disk, when --dry-run is
38 /// specified, no collection, request, or anything will be saved to disk.
39 #[arg(long)]
40 dry_run: bool,
41}
42
43impl Cli {
44 pub fn parse_args() -> RuntimeBehavior {
45 let args = Cli::parse();
46
47 if args.config_dir {
48 return RuntimeBehavior::PrintConfigPath;
49 }
50 if args.data_dir {
51 return RuntimeBehavior::PrintDataPath;
52 }
53 if args.config_dump {
54 return RuntimeBehavior::DumpDefaultConfig;
55 }
56 if args.dry_run {
57 return RuntimeBehavior::DryRun;
58 }
59
60 RuntimeBehavior::Run
61 }
62
63 pub fn print_data_path<P>(data_path: P)
64 where
65 P: AsRef<Path>,
66 {
67 println!(
68 "collections are being stored at: {}",
69 data_path.as_ref().to_string_lossy()
70 );
71 println!("you can change this on the configuration file by specifying `collections_dir`");
72 }
73
74 pub fn print_config_path<P>(maybe_path: Option<P>, usual_path: P)
75 where
76 P: AsRef<Path>,
77 {
78 match maybe_path {
79 Some(config_dir) => {
80 println!(
81 "config is being loaded from: {}",
82 config_dir.as_ref().to_string_lossy()
83 );
84 }
85 None => {
86 println!("no config file was found, the default one is being used");
87 println!("the usual path for the configuration file is at:\n");
88 println!("{}", usual_path.as_ref().to_string_lossy());
89 }
90 }
91 }
92
93 pub fn print_default_config(config_as_str: &str) {
94 println!("{}", config_as_str)
95 }
96}