Class: TargetIO::TrainCompat::Dir

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/target_io/train/dir.rb

Class Method Summary collapse

Class Method Details

.[](*patterns, base: ".", sort: true) ⇒ Object

TODO: chdir, mktmpdir, pwd, home (Used in Resources)



10
11
12
# File 'lib/chef/target_io/train/dir.rb', line 10

def [](*patterns, base: ".", sort: true)
  Dir.glob(patterns, 0, base, sort)
end

.__run_command(cmd) ⇒ Object



79
80
81
# File 'lib/chef/target_io/train/dir.rb', line 79

def __run_command(cmd)
  __transport_connection.run_command(cmd)
end

.__transport_connectionObject



83
84
85
# File 'lib/chef/target_io/train/dir.rb', line 83

def __transport_connection
  Chef.run_context&.transport_connection
end

.delete(dir_name) ⇒ Object



14
15
16
# File 'lib/chef/target_io/train/dir.rb', line 14

def delete(dir_name)
  ::TargetIO::FileUtils.rm_rf(dir_name)
end

.directory?(dir_name) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/chef/target_io/train/dir.rb', line 18

def directory?(dir_name)
  ::TargetIO::File.directory? dir_name
end

.entries(dirname) ⇒ Object



22
23
24
25
26
# File 'lib/chef/target_io/train/dir.rb', line 22

def entries(dirname)
  cmd = "ls -1a #{dirname}"
  output = __run_command(cmd).stdout
  output.split("\n")
end

.glob(pattern, flags = 0, base: ".", sort: true) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chef/target_io/train/dir.rb', line 28

def glob(pattern, flags = 0, base: ".", sort: true)
  raise "Dir.glob flags not supported except FNM_DOTMATCH" unless [0, ::File::FNM_DOTMATCH].include? flags

  pattern  = Array(pattern)
  matchdot = flags || ::File::FNM_DOTMATCH ? "dotglob" : ""

  # TODO: Check for bash remotely
  cmd += <<-BASH4
    shopt -s globstar #{matchdot}
    cd #{base}
    for f in #{pattern.join(" ")}; do
      printf '%s\n' "$f";
    done
  BASH4

  output = __run_command(cmd).stdout
  files  = output.split("\n")
  files.sort! if sort

  files
end

.mkdir(dir_name, mode = nil) ⇒ Object



50
51
52
53
# File 'lib/chef/target_io/train/dir.rb', line 50

def mkdir(dir_name, mode = nil)
  ::TargetIO::FileUtils.mkdir(dir_name)
  ::TargetIO::FileUtils.chmod(dir_name, mode) if mode
end

.mktmpdir(prefix_suffix = nil, *rest, **options) ⇒ Object

Borrowed and adapted from Ruby's Dir::tmpdir and Dir::mktmpdir



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/chef/target_io/train/dir.rb', line 56

def mktmpdir(prefix_suffix = nil, *rest, **options)
  prefix, suffix = ::File.basename(prefix_suffix || "d")
  random = (::Random.urandom(4).unpack1("L") % 36**6).to_s(36)

  tmpdir = ::Dir.tmpdir
  t = Time.now.strftime("%Y%m%d%s")
  path = "#{prefix}#{t}-#{$$}-#{random}" "#{suffix || ""}"
  path = ::File.join(tmpdir, path)

  ::TargetIO::FileUtils.mkdir(path)
  ::TargetIO::FileUtils.chmod(0700, path)

  at_exit do
    ::TargetIO::FileUtils.rm_rf(path)
  end

  path
end


75
76
77
# File 'lib/chef/target_io/train/dir.rb', line 75

def unlink(dir_name)
  ::TargetIO::FileUtils.rmdir(dir_name)
end