dotfiles_actions/exec/
directive.rs

1// Copyright (c) 2021-2022 Miguel Barreto and others
2//
3// Permission is hereby granted, free of charge, to any person obtaining
4// a copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to
8// permit persons to whom the Software is furnished to do so, subject to
9// the following conditions:
10//
11// The above copyright notice and this permission notice shall be
12// included in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22//! This module defines [ExecDirective] which represents commands to execute in a shell.
23
24use dotfiles_core::{action::SKIP_IN_CI_SETTING, directive::HasDirectiveData};
25use dotfiles_core_macros::Directive;
26use std::{collections::HashMap, marker::PhantomData, path::Path};
27use strict_yaml_rust::StrictYaml;
28
29use dotfiles_core::{
30  action::ActionParser, directive::DirectiveData, error::DotfilesError,
31  settings::initialize_settings_object, yaml_util, Setting,
32};
33
34use crate::exec::action::ExecAction;
35
36/// Name of the Exec directive
37pub const DIRECTIVE_NAME: &str = "exec";
38/// Echo the command to run before running it.
39pub const ECHO_SETTING: &str = "echo";
40/// Command to run
41pub const COMMAND_SETTING: &str = "cmd";
42/// Optional description for the command to run
43pub const DESCRIPTION_SETTING: &str = "description";
44
45/// Initialize the defaults for the BrewDirective.
46pub fn init_directive_data() -> DirectiveData {
47  DirectiveData::from(
48    DIRECTIVE_NAME.into(),
49    initialize_settings_object(&[
50      (ECHO_SETTING.to_owned(), Setting::Boolean(false)),
51      (SKIP_IN_CI_SETTING.to_owned(), Setting::Boolean(false)),
52    ]),
53  )
54}
55
56/// A directive that can build [ExecAction]s to run commands
57#[derive(Directive, Clone)]
58pub struct ExecDirective<'a> {
59  data: DirectiveData,
60  phantom_data: PhantomData<&'a DirectiveData>,
61}
62
63impl<'a> Default for ExecDirective<'a> {
64  fn default() -> Self {
65    Self {
66      data: init_directive_data(),
67      phantom_data: PhantomData,
68    }
69  }
70}
71
72impl<'a> ActionParser<'a> for ExecDirective<'a> {
73  type ActionType = ExecAction<'a>;
74  fn parse_action(
75    &'a self,
76    settings: &HashMap<String, Setting>,
77    yaml: &StrictYaml,
78    current_dir: &Path,
79  ) -> Result<ExecAction<'a>, DotfilesError> {
80    ExecAction::new(
81      yaml_util::get_boolean_setting_from_yaml_or_context(
82        SKIP_IN_CI_SETTING,
83        yaml,
84        settings,
85        self.defaults(),
86      )?,
87      yaml_util::get_string_content_or_keyed_value(yaml, Some(COMMAND_SETTING))?,
88      yaml_util::get_string_setting_from_yaml_or_context(
89        DESCRIPTION_SETTING,
90        yaml,
91        settings,
92        self.defaults(),
93      )
94      .ok(),
95      yaml_util::get_boolean_setting_from_yaml_or_context(
96        ECHO_SETTING,
97        yaml,
98        settings,
99        self.defaults(),
100      )?,
101      current_dir,
102    )
103  }
104}