summaryrefslogtreecommitdiffstats
path: root/netdata.spec.in
diff options
context:
space:
mode:
authorAlon Bar-Lev <alon.barlev@gmail.com>2017-02-19 14:03:20 -0500
committerAlon Bar-Lev <alon.barlev@gmail.com>2017-02-26 21:33:12 +0200
commitf5999d10c3f77ff5ca1a96d0a2185387f80547e2 (patch)
tree5d6f5e634460410c77b136818d5fc4a76bb4fa5d /netdata.spec.in
parentcacc1a70bcb6926a7b56f9f3a2e8948aea29e145 (diff)
spec: use global instead of define
Signed-off-by: Alon Bar-Lev <alon.barlev@gmail.com>
Diffstat (limited to 'netdata.spec.in')
-rw-r--r--netdata.spec.in20
1 files changed, 10 insertions, 10 deletions
diff --git a/netdata.spec.in b/netdata.spec.in
index 62fef6d9cc..86d3db909c 100644
--- a/netdata.spec.in
+++ b/netdata.spec.in
@@ -1,20 +1,20 @@
-%define contentdir %{_datadir}/netdata
+%global contentdir %{_datadir}/netdata
%if 0%{?suse_version}
-%define distro_post %service_add_post netdata.service
-%define distro_preun %service_del_preun netdata.service
-%define distro_postun %service_del_postun netdata.service
-%define distro_buildrequires BuildRequires:\ systemd-rpm-macros
+%global distro_post %service_add_post netdata.service
+%global distro_preun %service_del_preun netdata.service
+%global distro_postun %service_del_postun netdata.service
+%global distro_buildrequires BuildRequires:\ systemd-rpm-macros
%else
-%define distro_post %systemd_post netdata.service
-%define distro_preun %systemd_preun netdata.service
-%define distro_postun %systemd_postun_with_restart netdata.service
-%define distro_buildrequires %{nil}
+%global distro_post %systemd_post netdata.service
+%global distro_preun %systemd_preun netdata.service
+%global distro_postun %systemd_postun_with_restart netdata.service
+%global distro_buildrequires %{nil}
%endif
# This is temporary and should eventually be resolved. This bypasses
# the default rhel __os_install_post which throws a python compile
# error.
-%define __os_install_post %{nil}
+%global __os_install_post %{nil}
#
# Conditional build:
a id='n233' href='#n233'>233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
use {
    super::*,
    crate::{
        app::*,
        path,
    },
    ahash::AHashMap,
    regex::Captures,
    std::path::{Path, PathBuf},
};

/// a temporary structure gathering selection and invocation
/// parameters and able to generate an executable string from
/// a verb's execution pattern
pub struct ExecutionStringBuilder<'b> {
    /// the current file selection
    pub sel_info: SelInfo<'b>,

    /// the current root of the app
    root: &'b Path,

    /// the selection in the other panel, when there are exactly two
    other_file: Option<&'b PathBuf>,

    /// parsed arguments
    invocation_values: Option<AHashMap<String, String>>,
}

impl<'b> ExecutionStringBuilder<'b> {
    /// constructor to use when there's no invocation string
    /// (because we're in the process of building one, for example
    /// when a verb is triggered from a key shortcut)
    pub fn without_invocation(
         sel_info: SelInfo<'b>,
         app_state: &'b AppState,
    ) -> Self {
        Self {
            sel_info,
            root: &app_state.root,
            other_file: app_state.other_panel_path.as_ref(),
            invocation_values: None,
        }
    }
    pub fn with_invocation(
        invocation_parser: &Option<InvocationParser>,
        sel_info: SelInfo<'b>,
        app_state: &'b AppState,
        invocation_args: Option<&String>,
    ) -> Self {
        let invocation_values = invocation_parser
            .as_ref()
            .zip(invocation_args.as_ref())
            .and_then(|(parser, args)| parser.parse(args));
        Self {
            sel_info,
            root: &app_state.root,
            other_file: app_state.other_panel_path.as_ref(),
            invocation_values,
        }
    }
    fn get_raw_replacement<F>(
        &self,
        f: F
    ) -> Option<String>
    where
        F: Fn(Option<Selection<'_>>) -> Option<String>
    {
        match self.sel_info {
            SelInfo::None => f(None),
            SelInfo::One(sel) => f(Some(sel)),
            SelInfo::More(stage) => {
                let mut sels = stage.paths().iter()
                    .map(|path| Selection {
                        path,
                        line: 0,
                        stype: SelectionType::from(path),
                        is_exe: false,
                    });
                f(sels.next())
                    .filter(|first_rcr| {
                        for sel in sels {
                            let rcr = f(Some(sel));
                            if rcr.as_ref() != Some(first_rcr) {
                                return false;
                            }
                        }
                        true
                    })
            }
        }
    }
    fn get_raw_capture_replacement(&self, ec: &Captures<'_>) -> Option<String> {
        self.get_raw_replacement(|sel| {
            self.get_raw_sel_capture_replacement(ec, sel)
        })
    }
    /// return the standard replacement (ie not one from the invocation)
    fn get_raw_sel_name_standard_replacement(
        &self,
        name: &str,
        sel: Option<Selection<'_>>,
    ) -> Option<String> {
        debug!("repl name : {:?}", name);
        match name {
            "root" => Some(path_to_string(self.root)),
            "line" => sel.map(|s| s.line.to_string()),
            "file" => sel.map(|s| s.path)
                .map(path_to_string),
            "file-name" => sel.map(|s| s.path)
                .and_then(|path| path.file_name())
                .and_then(|oss| oss.to_str())
                .map(|s| s.to_string()),
            "file-stem" => sel.map(|s| s.path)
                .and_then(|path| path.file_stem())
                .and_then(|oss| oss.to_str())
                .map(|s| s.to_string()),
            "file-extension" => {
                debug!("expending file extension");
                sel.map(|s| s.path)
                .and_then(|path| path.extension())
                .and_then(|oss| oss.to_str())
                .map(|s| s.to_string())
            }
            "file-dot-extension" => {
                debug!("expending file dot extension");
                sel.map(|s| s.path)
                .and_then(|path| path.extension())
                .and_then(|oss| oss.to_str())
                .map(|ext| format!(".{ext}"))
                .or_else(|| Some("".to_string()))
            }
            "directory" => sel.map(|s| path::closest_dir(s.path))
                .map(path_to_string),
            "parent" => sel.and_then(|s| s.path.parent())
                .map(path_to_string),
            "other-panel-file" => self.other_file
                .map(path_to_string),
            "other-panel-filename" => self.other_file
                .and_then(|path| path.file_name())
                .and_then(|oss| oss.to_str())
                .map(|s| s.to_string()),
            "other-panel-directory" => self
                .other_file
                .map(|p| path::closest_dir(p))
                .as_ref()
                .map(path_to_string),
            "other-panel-parent" => self
                .other_file
                .and_then(|p| p.parent())
                .map(path_to_string),
            "git-root" => {
                debug!("finding git root");
                sel.and_then(|s| match git2::Repository::discover(s.path) {
                    Ok(repo) => repo.workdir().map(path_to_string),
                    Err(err) => {
                        warn!(
                            "Failed to open Git repository at {}: {err}",
                            s.path.display()
                        );
                        None
                    }
                })
            }
            _ => None,
        }
    }
    fn get_raw_sel_capture_replacement(
        &self,
        ec: &Captures<'_>,
        sel: Option<Selection<'_>>,
    ) -> Option<String> {
        let name = ec.get(1).unwrap().as_str();
        self.get_raw_sel_name_standard_replacement(name, sel)
            .or_else(||{
                // it's not one of the standard group names, so we'll look
                // into the ones provided by the invocation pattern
                self.invocation_values.as_ref()
                    .and_then(|map| map.get(name))
                    .and_then(|value| {
                        if let Some(fmt) = ec.get(2) {
                            match fmt.as_str() {
                                "path-from-directory" => {
                                    sel.map(|s| path::closest_dir(s.path))
                                        .map(|dir| path::path_str_from(dir, value))
                                }
                                "path-from-parent" => {
                                     sel.and_then(|s| s.path.parent())
                                        .map(|dir| path::path_str_from(dir, value))
                                }
                                _ => Some(format!("invalid format: {:?}", fmt.as_str())),
                            }
                        } else {
                            Some(value.to_string())
                        }
                    })
            })
    }
    fn get_capture_replacement(&self, ec: &Captures<'_>) -> String {
        self.get_raw_capture_replacement(ec)
            .unwrap_or_else(|| ec[0].to_string())
    }
    fn get_sel_capture_replacement(
        &self,
        ec: &Captures<'_>,
        sel: Option<Selection<'_>>,
    ) -> String {