Skip to content

Commit 16986a0

Browse files
committed
Move logic for temporary directory to util class
1 parent 45254e2 commit 16986a0

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

lib/octocatalog-diff/util/scriptrunner.rb

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
require 'open3'
77
require 'shellwords'
88

9+
require_relative 'util'
10+
911
module OctocatalogDiff
1012
module Util
1113
# This is a utility class to execute a built-in script.
@@ -93,20 +95,7 @@ def log(priority, message, logger = @logger)
9395
# @return [String] Path to tempfile containing script
9496
def temp_script(script)
9597
raise Errno::ENOENT, "Script '#{script}' not found" unless File.file?(script)
96-
temp_dir = if existing_tempdir
97-
Dir.mktmpdir('ocd-scriptrunner-', existing_tempdir)
98-
else
99-
temp_dir_local = Dir.mktmpdir('ocd-scriptrunner-')
100-
at_exit do
101-
begin
102-
FileUtils.remove_entry_secure temp_dir_local
103-
rescue Errno::ENOENT # rubocop:disable Lint/HandleExceptions
104-
# OK if the directory doesn't exist since we're trying to remove it anyway
105-
end
106-
end
107-
temp_dir_local
108-
end
109-
98+
temp_dir = OctocatalogDiff::Util::Util.temp_dir('ocd-scriptrunner', existing_tempdir)
11099
temp_file = File.join(temp_dir, File.basename(script))
111100
File.open(temp_file, 'w') { |f| f.write(File.read(script)) }
112101
FileUtils.chmod 0o755, temp_file

lib/octocatalog-diff/util/util.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
# Handy methods that are not tied to one particular class
44

5+
require 'fileutils'
6+
57
module OctocatalogDiff
68
module Util
79
# Helper class to construct catalogs, performing all necessary steps such as
@@ -43,6 +45,38 @@ def self.deep_dup(object)
4345
safe_dup(object)
4446
end
4547
end
48+
49+
# Utility Method!
50+
# This creates a temporary directory. If the base directory is specified, then we
51+
# do not remove the temporary directory at exit, because we assume that something
52+
# else will remove the base directory.
53+
#
54+
# prefix - A String with the prefix for the temporary directory
55+
# basedir - A String with the directory in which to make the tempdir
56+
#
57+
# Returns the full path to the temporary directory.
58+
def self.temp_dir(prefix = 'ocd-', basedir = nil)
59+
# If the base directory is specified, make sure it exists, and then create the
60+
# temporary directory within it.
61+
if basedir
62+
unless File.directory?(basedir)
63+
raise Errno::ENOENT, "temp_dir: Base dir #{basedir.inspect} does not exist!"
64+
end
65+
return Dir.mktmpdir(prefix, basedir)
66+
end
67+
68+
# If the base directory was not specified, then create a temporary directory, and
69+
# send the `at_exit` to clean it up at the conclusion.
70+
the_dir = Dir.mktmpdir(prefix)
71+
at_exit do
72+
begin
73+
FileUtils.remove_entry_secure the_dir
74+
rescue Errno::ENOENT # rubocop:disable Lint/HandleExceptions
75+
# OK if the directory doesn't exist since we're trying to remove it anyway
76+
end
77+
end
78+
the_dir
79+
end
4680
end
4781
end
4882
end

0 commit comments

Comments
 (0)