dotfiles_actions/exec/
directive.rs1use 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
36pub const DIRECTIVE_NAME: &str = "exec";
38pub const ECHO_SETTING: &str = "echo";
40pub const COMMAND_SETTING: &str = "cmd";
42pub const DESCRIPTION_SETTING: &str = "description";
44
45pub 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#[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}