Skip to content

Commit fa2711c

Browse files
committed
Allow setting file permissions with create_file
Previously, to restrict the permissions of a file created using create_file, chmod could be called right after. However, that leaves a short amount of time between when the file is first created and when the permissions are updated where another user could read the file. This commit enables passing file permissions to create_file so that permissions can be set when the file is created.
1 parent 376e141 commit fa2711c

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/thor/actions/create_file.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def invoke!
6060
invoke_with_conflict_check do
6161
require "fileutils"
6262
FileUtils.mkdir_p(File.dirname(destination))
63-
File.open(destination, "wb") { |f| f.write render }
63+
File.open(destination, "wb", config[:perm]) { |f| f.write render }
6464
end
6565
given_destination
6666
end

spec/actions/create_file_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ def silence!
3333
expect(File.exist?(File.join(destination_root, "doc/config.rb"))).to be true
3434
end
3535

36+
it "allows setting file permissions" do
37+
create_file("config/private.key", perm: 0600)
38+
invoke!
39+
40+
stat = File.stat(File.join(destination_root, "config/private.key"))
41+
expect(stat.mode.to_s(8)).to eq "100600"
42+
end
43+
3644
it "does not create a file if pretending" do
3745
create_file("doc/config.rb", {}, :pretend => true)
3846
invoke!

0 commit comments

Comments
 (0)