use crate::cli::args::{ColorChoice, GlobalOpts};
use anyhow::Result;
use colored::control;
use nginx_discovery::system;
use std::path::PathBuf;
pub fn setup_colors(choice: ColorChoice) {
match choice {
ColorChoice::Always => control::set_override(true),
ColorChoice::Never => control::set_override(false),
ColorChoice::Auto => {
if atty::is(atty::Stream::Stdout) {
control::set_override(true);
} else {
control::set_override(false);
}
}
}
}
pub fn find_config(global: &GlobalOpts) -> Result<PathBuf> {
if let Some(ref path) = global.config {
return Ok(path.clone());
}
if global.verbose {
eprintln!("Auto-detecting NGINX configuration...");
}
let common_paths = [
"/etc/nginx/nginx.conf",
"/usr/local/nginx/conf/nginx.conf",
"/usr/local/etc/nginx/nginx.conf",
"nginx.conf",
];
for path in &common_paths {
let p = PathBuf::from(path);
if p.exists() {
if global.verbose {
eprintln!("Found config at: {}", p.display());
}
return Ok(p);
}
}
if let Ok(_nginx_path) = system::find_nginx() {
if global.verbose {
eprintln!("Trying to detect config from nginx binary...");
}
let default_path = PathBuf::from("/etc/nginx/nginx.conf");
if default_path.exists() {
return Ok(default_path);
}
}
anyhow::bail!("Could not find NGINX configuration file. Please specify with --config")
}
mod atty {
pub enum Stream {
Stdout,
}
pub fn is(_: Stream) -> bool {
std::env::var("TERM").is_ok()
}
}