cdk_ansible_cli/
lib.rs

1//!
2//! This is a crate for `cdk-ansible` command
3//!
4//! # Usage
5//!
6//! ```bash
7//! cdk-ansible module --help
8//! ```
9//!
10
11use anyhow::{Context as _, Result};
12use clap::{
13    Parser as _,
14    error::{ContextKind, ContextValue},
15};
16use serde::Deserialize;
17use std::ffi::OsString;
18use std::path::Path;
19
20/// CLI arguments
21mod arg;
22/// Settings layer
23mod settings;
24/// Define subcommands
25mod subcommand;
26/// Define utils
27mod utils;
28/// Define version
29mod version;
30
31use arg::{Cli, Commands};
32
33#[derive(Debug, Clone, Default, Deserialize)]
34/// Options for the application.
35///
36/// Not implemented yet.
37struct Options {
38    #[expect(dead_code, reason = "not implemented yet")]
39    /// Phantom data
40    val1: String,
41}
42
43/// Load [`Options`] from a app config file
44fn read_file(path: &Path) -> Result<Options> {
45    let _content =
46        fs_err::read_to_string(path).context(format!("Failed to read file: {}", path.display()))?;
47    let options: Options = Options::default();
48    Ok(options)
49}
50
51#[derive(Debug, Clone)]
52/// Filesystem options
53/// Not implemented yet.
54#[expect(dead_code, reason = "not implemented yet")]
55struct FilesystemOptions(Options);
56
57impl FilesystemOptions {
58    /// Load [`FilesystemOptions`] from a app config file
59    pub fn from_file(file: &Path) -> Result<Self> {
60        let options: Options = read_file(file)?;
61        Ok(Self(options))
62    }
63}
64
65/// Main entry point of the application
66///
67/// This function is the main entry point of the application.
68/// It parses the command line arguments, reads the configuration file,
69/// and then executes the appropriate subcommand.
70///
71/// # Arguments
72///
73/// * `args` - The command line arguments to parse.
74///
75/// # Returns
76///
77/// Returns a `Result` with the result of the subcommand.
78///
79/// # Errors
80///
81/// Use [`anyhow::Result`] to handle errors.
82///
83#[inline]
84pub fn run<I, T>(args: I) -> Result<()>
85where
86    I: IntoIterator<Item = T>,
87    T: Into<OsString> + Clone,
88{
89    let cli = match Cli::try_parse_from(args) {
90        Ok(cli) => cli,
91        Err(mut err) => {
92            #[expect(clippy::single_match, reason = "better readability")]
93            #[expect(clippy::pattern_type_mismatch, reason = "I don't know")]
94            match err.get(ContextKind::InvalidSubcommand) {
95                Some(ContextValue::String(subcommand)) => match subcommand.as_str() {
96                    "help" => {
97                        err.insert(
98                            ContextKind::InvalidSubcommand,
99                            ContextValue::String("help".to_owned()),
100                        );
101                    }
102                    "module" => {
103                        err.insert(
104                            ContextKind::InvalidSubcommand,
105                            ContextValue::String("module".to_owned()),
106                        );
107                    }
108                    _ => {}
109                },
110                _ => {}
111            }
112            err.exit()
113        }
114    };
115
116    let filesystem = if let Some(config_file) = cli.top_level.config_file.as_ref() {
117        Some(FilesystemOptions::from_file(config_file)?)
118    } else {
119        None
120    };
121
122    #[expect(clippy::todo, reason = "implement later")]
123    if let Some(_filesystem) = filesystem {
124        todo!("TODO: read as global settings");
125    }
126
127    // TODO: implement later
128    let _global_settings = settings::GlobalSettings::resolve(&cli.top_level.global_args);
129
130    let result = match *cli.command {
131        #[expect(clippy::todo, reason = "implement later")]
132        Commands::Help(_help_args) => {
133            todo!("TODO: implement help command");
134        }
135        Commands::Module(module_args) => subcommand::module::module(module_args),
136    };
137    result.context("Failed to run command")
138}