Module: ChaosDetector::Utils::FSUtil

Defined in:
lib/chaos_detector/utils/fs_util.rb

Class Method Summary collapse

Class Method Details

.ensure_dirpath(dirpath) ⇒ Object

Ensure directory and all its parents exist, like (mkdir -p):

Raises:

  • (ArgumentError)


18
19
20
21
22
# File 'lib/chaos_detector/utils/fs_util.rb', line 18

def ensure_dirpath(dirpath)
  raise ArgumentError, '#ensure_paths_to_file requires dirpath' if nay? dirpath

  FileUtils.mkdir_p(dirpath)
end

.ensure_paths_to_file(filepath) ⇒ Object

Ensure file’s directory and all its parents exist, like (mkdir -p):

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
# File 'lib/chaos_detector/utils/fs_util.rb', line 34

def ensure_paths_to_file(filepath)
  raise ArgumentError, '#ensure_paths_to_file requires filepath' if nay? filepath

  dirpath = File.split(filepath).first
  raise "dirpath couldn't be obtained from #{filepath}" if nay? dirpath

  ensure_dirpath(dirpath)
end

.nay?(obj) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/chaos_detector/utils/fs_util.rb', line 43

def nay?(obj)
  ChaosDetector::Utils::CoreUtil.naught?(obj)
end

.rel_path(dir_path, from_path:) ⇒ Object

Relative path:



11
12
13
14
15
# File 'lib/chaos_detector/utils/fs_util.rb', line 11

def rel_path(dir_path, from_path:)
  pathname = Pathname.new(dir_path)
  base_path = Pathname.new(from_path).cleanpath
  pathname.relative_path_from(base_path).to_s
end

.safe_file_write(filepath, content: nil) ⇒ Object

Ensure file’s directory and all its parents exist, then write given string to it:

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
# File 'lib/chaos_detector/utils/fs_util.rb', line 25

def safe_file_write(filepath, content: nil)
  raise ArgumentError, '#write_to_file requires filepath' if nay? filepath

  ensure_paths_to_file(filepath)
  File.write(filepath, content)
  filepath
end