Skip to content

Commit

Permalink
Allow setting file permissions with create_file
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
skipkayhil committed May 5, 2023
1 parent 376e141 commit 81f9965
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/thor/actions/create_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def invoke!
invoke_with_conflict_check do
require "fileutils"
FileUtils.mkdir_p(File.dirname(destination))
File.open(destination, "wb") { |f| f.write render }
File.open(destination, "wb", config[:perm]) { |f| f.write render }
end
given_destination
end
Expand Down
8 changes: 8 additions & 0 deletions spec/actions/create_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ def silence!
expect(File.exist?(File.join(destination_root, "doc/config.rb"))).to be true
end

it "allows setting file permissions" do
create_file("config/private.key", :perm => 0o600)
invoke!

stat = File.stat(File.join(destination_root, "config/private.key"))
expect(stat.mode.to_s(8)).to eq "100600"
end

it "does not create a file if pretending" do
create_file("doc/config.rb", {}, :pretend => true)
invoke!
Expand Down

0 comments on commit 81f9965

Please sign in to comment.