cdk_ansible_cli/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use cdk_ansible_static::EnvVars;
use clap::{command, Args, Parser, Subcommand, ValueEnum};
use std::path::PathBuf;

pub mod version;

#[derive(Parser)]
#[command(name = "cdk-ansible", author, long_version = crate::version::version().to_string())]
#[command(about = ".")]
#[command(propagate_version = true)]
#[command(
    after_help = "Use `cdk-ansible help` for more details.",
    after_long_help = "",
    // disable_help_flag = true,
    disable_help_subcommand = true,
    disable_version_flag = true
)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Box<Commands>,
    #[command(flatten)]
    pub top_level: TopLevelArgs,
}

#[derive(Parser)]
#[command(disable_version_flag = true)]
pub struct TopLevelArgs {
    #[command(flatten)]
    pub global_args: Box<GlobalArgs>,

    #[arg(
        global = true,
        long,
        env = EnvVars::CDK_ANSIBLE_CONFIG_FILE,
        help_heading = "Global options"
    )]
    pub config_file: Option<PathBuf>,

    /// Display the version.
    #[arg(global = true, short = 'V', long, action = clap::ArgAction::Version, help_heading = "Global options")]
    version: Option<bool>,
}

#[derive(Parser, Debug, Clone)]
#[command(next_help_heading = "Global options", next_display_order = 1000)]
#[allow(clippy::struct_excessive_bools)]
pub struct GlobalArgs {
    /// Do not print any output.
    #[arg(global = true, long, short, conflicts_with = "verbose")]
    pub quiet: bool,

    /// Use verbose output.
    ///
    /// You can configure fine-grained logging using the `RUST_LOG` environment variable.
    /// (<https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives>)
    #[arg(global = true, action = clap::ArgAction::Count, long, short, conflicts_with = "quiet")]
    pub verbose: u8,
}

#[derive(Subcommand)]
#[allow(clippy::large_enum_variant)]
pub enum Commands {
    #[command(next_help_heading = "Show help")]
    Help(HelpArgs),
    #[command(next_help_heading = "Create Rust code from ansible module")]
    Module(ModuleArgs),
    #[command(next_help_heading = "Create Ansible playbooks from Rust code")]
    Synth(SynthArgs),
}

#[derive(Args, Debug)]
pub struct HelpArgs {
    /// Disable pager when printing help
    #[arg(long)]
    pub no_pager: bool,

    pub command: Option<Vec<String>>,
}

#[derive(Args, Debug, Clone)]
pub struct ModuleArgs {
    /// Default value is defined at `cdk_ansible::settings::ModuleSettings`
    #[arg(short, long, required = false)]
    pub pkg_prefix: Option<String>,
    /// Specifies the level at which Cargo packages are created:
    /// - 'namespace': Creates a package at the namespace level
    /// - 'collection': Creates a package at the collection level
    /// - 'module': Creates a package at the module level
    /// - 'None': Does not create any packages
    ///
    /// Package names follow this pattern:
    /// - namespace: 'cdkam_<namespace>'
    /// - collection: 'cdkam_<namespace>_<collection>'
    /// - module: 'cdkam_<namespace>_<collection>_<module>'
    #[arg(long, required = false, value_enum)]
    pub pkg_unit: Option<PkgUnit>,
    /// Default value is defined at `cdk_ansible::settings::ModuleSettings`
    #[arg(long, required = false)]
    pub output_dir: Option<PathBuf>,
    /// Default value is defined at `cdk_ansible::settings::ModuleSettings`
    #[arg(long, required = false)]
    pub no_cache: bool,
    /// Default value is defined at `cdk_ansible::settings::ModuleSettings`
    #[arg(long, required = false)]
    pub cache_dir: Option<PathBuf>,
    #[arg(
        long,
        required = false,
        help = "Specify the ansible module name. If not specified, all modules accessible from your ansible environment will be generated."
    )]
    pub module_name: Option<String>,
}

#[derive(Debug, Clone, ValueEnum, Eq, PartialEq)]
pub enum PkgUnit {
    Namespace,
    Collection,
    Module,
    None,
}

#[derive(Args, Debug, Clone)]
pub struct SynthArgs {
    #[arg(short, long, required = true)]
    #[arg(help = "Ansible project root directory")]
    pub output_dir: PathBuf,
    #[arg(short, long, required = false)]
    #[arg(help = "Ansible inventory directory. Default is '<output>/inventory/'")]
    pub inventory_dir: Option<PathBuf>,
    #[arg(short, long, required = false)]
    #[arg(help = "Ansible playbooks directory. Default is '<output>/playbooks/'")]
    pub playbooks_dir: Option<PathBuf>,
}