Use optional fields on Synced Folder instead of empty string checks

Addresses concerns raised in discussion here
https://github.com/hashicorp/vagrant-ruby/pull/219#discussion_r816966056
and makes it so we don't have to change any plugin code to make things
work.

Depends on https://github.com/hashicorp/vagrant-plugin-sdk/pull/133
This commit is contained in:
Paul Hinze 2022-03-01 13:30:00 -06:00
parent 6585cd55af
commit 7c1d2e5368
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
5 changed files with 19 additions and 25 deletions

View File

@ -231,11 +231,12 @@ func (m *Machine) SyncedFolders() (folders []*core.MachineSyncedFolder, err erro
folders = []*core.MachineSyncedFolder{}
for _, folder := range syncedFolders {
if folder.Type == "" {
if folder.Type == nil {
// TODO: get default synced folder type
folder.Type = "virtualbox"
defaultType := "virtualbox"
folder.Type = &defaultType
}
plg, err := m.project.basis.component(m.ctx, component.SyncedFolderType, folder.Type)
plg, err := m.project.basis.component(m.ctx, component.SyncedFolderType, *folder.Type)
if err != nil {
return nil, err
}

View File

@ -776,11 +776,11 @@ Google::Protobuf::DescriptorPool.generated_pool.build do
optional :config, :message, 3, "google.protobuf.Any"
optional :create, :bool, 4
optional :disabled, :bool, 5
optional :group, :string, 6
proto3_optional :group, :string, 6
optional :id, :string, 7
repeated :mount_options, :string, 8
optional :owner, :string, 9
optional :type, :string, 10
proto3_optional :owner, :string, 9
proto3_optional :type, :string, 10
end
add_message "hashicorp.vagrant.sdk.Vagrantfile.PushConfig" do
optional :name, :string, 1

View File

@ -113,7 +113,7 @@ module VagrantPlugins
folder_protos.map do |fp|
{
plugin: SyncedFolder.load(fp.plugin, broker: broker),
folder: fp.folder.to_h,
folder: _cleaned_folder_hash(fp.folder),
}
end
end
@ -122,6 +122,15 @@ module VagrantPlugins
def uid
client.uid(Empty.new).uid
end
def _cleaned_folder_hash(folder)
folder_hash = folder.to_h
folder_hash.delete_if do |k, v|
hazzer = :"has_#{k}?"
folder.respond_to?(hazzer) && !folder.send(hazzer)
end
folder_hash
end
end
end
end

View File

@ -71,12 +71,8 @@ module VagrantPlugins
end
# Folder options
if opts[:owner].to_s == ""
opts[:owner] = ssh_info[:username]
end
if opts[:group].to_s == ""
opts[:group] = ssh_info[:username]
end
opts[:owner] ||= ssh_info[:username]
opts[:group] ||= ssh_info[:username]
# set log level
log_level = ssh_info[:log_level] || "FATAL"

View File

@ -180,18 +180,6 @@ describe VagrantPlugins::SyncedFolderRSync::RsyncHelper do
subject.rsync_single(machine, ssh_info, opts)
end
it "should populate :owner and :group from ssh_info[:username] when values are empty strings" do
opts[:owner] = ""
opts[:group] = ""
ssh_info[:username] = "userfromssh"
expect(guest).to receive(:capability).with(:rsync_post, a_hash_including(
owner: "userfromssh",
group: "userfromssh",
))
subject.rsync_single(machine, ssh_info, opts)
end
end
context "with rsync_ownership option" do