Class: TargetIO::TrainCompat::Dir

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

Class Method Summary collapse

Methods included from Support

clean_staging, read_file, remote_user, run_command, staging_file, sudo?, transport_connection, upload, write_file

Class Method Details

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



12
13
14
# File 'lib/chef/target_io/train/dir.rb', line 12

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

.delete(dir_name) ⇒ Object



16
17
18
# File 'lib/chef/target_io/train/dir.rb', line 16

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

.directory?(dir_name) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/chef/target_io/train/dir.rb', line 20

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

.entries(dirname) ⇒ Object



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

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

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



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

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" : ""

  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



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

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



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

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)

  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)
  ::TargetIO::FileUtils.chown(remote_user, nil, path) if sudo?

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

  path
end

.tmpdirObject



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

def tmpdir
  ::Dir.tmpdir
end


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

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