@@ -114,6 +114,15 @@ def addToHosts(entries)
114
114
@ui . error "[vagrant-hostsupdater] Failed to add hosts, could not use sudo"
115
115
adviseOnSudo
116
116
end
117
+ elsif Vagrant ::Util ::Platform . windows?
118
+ require 'tmpdir'
119
+ uuid = @machine . id || @machine . config . hostsupdater . id
120
+ tmpPath = File . join ( Dir . tmpdir , 'hosts-' + uuid + '.cmd' )
121
+ File . open ( tmpPath , "w" ) do |tmpFile |
122
+ entries . each { |line | tmpFile . puts ( ">>\" #{ @@hosts_path } \" echo #{ line } " ) }
123
+ end
124
+ sudo ( tmpPath )
125
+ File . delete ( tmpPath )
117
126
else
118
127
content = "\n " + content
119
128
hostsFile = File . open ( @@hosts_path , "a" )
@@ -125,7 +134,7 @@ def addToHosts(entries)
125
134
def removeFromHosts ( options = { } )
126
135
uuid = @machine . id || @machine . config . hostsupdater . id
127
136
hashedId = Digest ::MD5 . hexdigest ( uuid )
128
- if !File . writable_real? ( @@hosts_path )
137
+ if !File . writable_real? ( @@hosts_path ) || Vagrant :: Util :: Platform . windows?
129
138
if !sudo ( %Q(sed -i -e '/#{ hashedId } / d' #@@hosts_path ) )
130
139
@ui . error "[vagrant-hostsupdater] Failed to remove hosts, could not use sudo"
131
140
adviseOnSudo
@@ -151,15 +160,19 @@ def signature(name, uuid = self.uuid)
151
160
def sudo ( command )
152
161
return if !command
153
162
if Vagrant ::Util ::Platform . windows?
154
- `#{ command } `
163
+ require 'win32ole'
164
+ args = command . split ( " " )
165
+ command = args . shift
166
+ sh = WIN32OLE . new ( 'Shell.Application' )
167
+ sh . ShellExecute ( command , args . join ( " " ) , '' , 'runas' , 0 )
155
168
else
156
169
return system ( "sudo #{ command } " )
157
170
end
158
171
end
159
172
160
173
def adviseOnSudo
161
174
@ui . error "[vagrant-hostsupdater] Consider adding the following to your sudoers file:"
162
- @ui . error "[vagrant-hostsupdater] https://github.com/cogitatio/vagrant-hostsupdater#passwordless-sudo "
175
+ @ui . error "[vagrant-hostsupdater] https://github.com/cogitatio/vagrant-hostsupdater#suppressing-prompts-for-elevating-privileges "
163
176
end
164
177
end
165
178
end
0 commit comments