|
1 | | -use crate::agent_control::defaults::AGENT_CONTROL_LOCAL_DATA_DIR; |
| 1 | +use crate::{ |
| 2 | + agent_control::defaults::{ |
| 3 | + AGENT_CONTROL_DATA_DIR, AGENT_CONTROL_LOCAL_DATA_DIR, AGENT_CONTROL_LOG_DIR, SUB_AGENT_DIR, |
| 4 | + }, |
| 5 | + config_migrate::migration::defaults::NEWRELIC_INFRA_AGENT_TYPE_CONFIG_MAPPING, |
| 6 | +}; |
2 | 7 | use clap::Parser; |
3 | | -use std::path::PathBuf; |
| 8 | +use std::{error::Error, fs, path::PathBuf}; |
4 | 9 |
|
5 | 10 | #[derive(Parser, Debug)] |
6 | 11 | #[command(author, version, about, long_about = None)] // Read from `Cargo.toml` |
7 | 12 | pub struct Cli { |
8 | | - /// Overrides the default local configuration path `/etc/newrelic-agent-control/`. |
9 | | - #[cfg(debug_assertions)] |
| 13 | + /// Local data path used by Agent Control. |
| 14 | + #[arg(long, default_value_os_t = PathBuf::from(AGENT_CONTROL_LOCAL_DATA_DIR))] |
| 15 | + local_dir: PathBuf, |
| 16 | + |
| 17 | + /// Remote data path used by Agent Control. |
| 18 | + #[arg(long, default_value_os_t = PathBuf::from(AGENT_CONTROL_DATA_DIR))] |
| 19 | + remote_dir: PathBuf, |
| 20 | + |
| 21 | + /// Logs path used by Agent Control. |
| 22 | + #[arg(long, default_value_os_t = PathBuf::from(AGENT_CONTROL_LOG_DIR))] |
| 23 | + logs_dir: PathBuf, |
| 24 | + |
| 25 | + /// Provides an external configuration mapping for the migration of agents to Agent Control. |
10 | 26 | #[arg(long)] |
11 | | - local_dir: Option<PathBuf>, |
| 27 | + migration_config_file: Option<PathBuf>, |
12 | 28 | } |
13 | 29 |
|
14 | 30 | impl Cli { |
15 | 31 | /// Parses command line arguments |
16 | | - pub fn init_config_migrate_cli() -> Self { |
| 32 | + pub fn load() -> Self { |
17 | 33 | // Get command line args |
18 | 34 | Self::parse() |
19 | 35 | } |
20 | 36 |
|
21 | 37 | pub fn local_data_dir(&self) -> PathBuf { |
22 | | - #[cfg(debug_assertions)] |
23 | | - if let Some(path) = &self.local_dir { |
24 | | - return path.clone(); |
25 | | - } |
| 38 | + self.local_dir.to_path_buf() |
| 39 | + } |
| 40 | + |
| 41 | + pub fn local_sub_agent_data_dir(&self) -> PathBuf { |
| 42 | + self.local_dir.join(SUB_AGENT_DIR) |
| 43 | + } |
| 44 | + |
| 45 | + pub fn remote_data_dir(&self) -> PathBuf { |
| 46 | + self.remote_dir.to_path_buf() |
| 47 | + } |
26 | 48 |
|
27 | | - PathBuf::from(AGENT_CONTROL_LOCAL_DATA_DIR) |
| 49 | + pub fn log_dir(&self) -> PathBuf { |
| 50 | + self.logs_dir.to_path_buf() |
| 51 | + } |
| 52 | + |
| 53 | + pub fn get_migration_config_str(&self) -> Result<String, Box<dyn Error>> { |
| 54 | + if let Some(path) = &self.migration_config_file { |
| 55 | + fs::read_to_string(path).map_err(|e| { |
| 56 | + format!( |
| 57 | + "Could not read provided migration config file ({}): {}", |
| 58 | + path.display(), |
| 59 | + e |
| 60 | + ) |
| 61 | + .into() |
| 62 | + }) |
| 63 | + } else { |
| 64 | + Ok(NEWRELIC_INFRA_AGENT_TYPE_CONFIG_MAPPING.to_owned()) |
| 65 | + } |
28 | 66 | } |
29 | 67 | } |
0 commit comments