1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) 2021-2022 Miguel Barreto and others
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

//! This module contains the [CreateAction] that creates a new directory
//! when executed

extern crate strict_yaml_rust;

use derivative::Derivative;
use dotfiles_core::action::Action;
use dotfiles_core::error::DotfilesError;
use dotfiles_core::path::convert_path_to_absolute;
use dotfiles_core::path::process_home_dir_in_path;
use dotfiles_core_macros::ConditionalAction;
use filesystem::FakeFileSystem;
use filesystem::FileSystem;
use filesystem::OsFileSystem;

use getset::Getters;
use log::info;

use std::io::ErrorKind;
use std::path::Path;
use std::path::PathBuf;

/// [CreateAction] creates a new [directory](CreateAction::directory) when executed
#[derive(Derivative, ConditionalAction, Getters)]
#[derivative(Debug, PartialEq)]
pub struct CreateAction<'a, F: FileSystem> {
  /// Skips this action if it is running in a CI environment.
  skip_in_ci: bool,
  /// FileSystem to use to create the directory.
  ///
  /// Having a filesystem instance here allows us to use fakes/mocks to use
  /// in unit tests.
  #[derivative(Debug = "ignore", PartialEq = "ignore")]
  fs: &'a F,
  /// Directory to create. Can be absolute or relative.
  #[get = "pub"]
  directory: String,
  /// Force creation of the directory and all its parents if they do not
  /// exist already.
  ///
  /// Setting [`create_parent_dirs`](CreateAction::create_parent_dirs) to `true` is equivalent to
  /// using the `-p` flag in `mkdir`.
  #[get = "pub"]
  create_parent_dirs: bool,
  /// Current directory that will be used to determine relative file locations if necessary. It
  /// must match the parent directory of the configuration file that declared this action.
  #[get = "pub"]
  current_dir: PathBuf,
}

/// A native create action that works on the real filesystem.
pub type NativeCreateAction<'a> = CreateAction<'a, OsFileSystem>;
/// A Fake create action that works on a fake test filesystem.
pub type FakeCreateAction<'a> = CreateAction<'a, FakeFileSystem>;

impl<'a, F: FileSystem> CreateAction<'a, F> {
  /// Constructs a new instance of CreateAction
  pub fn new(
    fs: &'a F,
    skip_in_ci: bool,
    directory: String,
    create_parent_dirs: bool,
    current_dir: PathBuf,
  ) -> Result<Self, DotfilesError> {
    let action = CreateAction {
      skip_in_ci,
      fs,
      directory,
      create_parent_dirs,
      current_dir,
    };
    log::trace!("Creating new {:?}", action);
    Ok(action)
  }
}

impl<F: FileSystem> Action<'_> for CreateAction<'_, F> {
  /// Creates the [`directory`](CreateAction::directory).
  ///
  /// # Errors
  /// - The parent directory does not exist and
  ///   [`create_parent_dirs`](CreateAction::create_parent_dirs) is false.
  /// - There is already a directory, file or symlink with the same name.
  /// - Permission denied.
  fn execute(&self) -> Result<(), DotfilesError> {
    fn create_dir<F: FileSystem>(
      fs: &'_ F,
      directory: &str,
      create_parent_dirs: bool,
      current_dir: &Path,
    ) -> Result<(), DotfilesError> {
      let path = PathBuf::from(directory.to_owned());
      let path = process_home_dir_in_path(&path);
      let path = convert_path_to_absolute(&path, Some(current_dir))?;

      if create_parent_dirs {
        fs.create_dir_all(path)
      } else {
        fs.create_dir(path)
      }
      .or_else(|io_error| {
        if let ErrorKind::AlreadyExists = io_error.kind() {
          Ok(())
        } else {
          Err(DotfilesError::from_io_error(io_error))
        }
      })
    }
    create_dir(
      self.fs,
      &self.directory,
      self.create_parent_dirs,
      &self.current_dir,
    )
    .map(|_| {
      info!("Created directory {}", &self.directory);
    })
  }
}