@@ -62,8 +62,9 @@ func New() HostAPI {
62
62
}
63
63
64
64
func getVolumeSize (volumeID string ) (int64 , error ) {
65
- cmd := fmt .Sprintf ("(Get-Volume -UniqueId \" %s\" | Get-partition).Size" , volumeID )
66
- out , err := utils .RunPowershellCmd (cmd )
65
+ cmd := `(Get-Volume -UniqueId "$Env:volumeID" | Get-partition).Size`
66
+ cmdEnv := fmt .Sprintf ("volumeID=%s" , volumeID )
67
+ out , err := utils .RunPowershellCmd (cmd , cmdEnv )
67
68
68
69
if err != nil || len (out ) == 0 {
69
70
return - 1 , fmt .Errorf ("error getting size of the partition from mount. cmd %s, output: %s, error: %v" , cmd , string (out ), err )
@@ -98,8 +99,9 @@ func (volumeAPI) ListVolumesOnDisk(diskNumber uint32, partitionNumber uint32) (v
98
99
99
100
// FormatVolume - Formats a volume with the NTFS format.
100
101
func (volumeAPI ) FormatVolume (volumeID string ) (err error ) {
101
- cmd := fmt .Sprintf ("Get-Volume -UniqueId \" %s\" | Format-Volume -FileSystem ntfs -Confirm:$false" , volumeID )
102
- out , err := utils .RunPowershellCmd (cmd )
102
+ cmd := `Get-Volume -UniqueId "$Env:volumeID" | Format-Volume -FileSystem ntfs -Confirm:$false`
103
+ cmdEnv := fmt .Sprintf ("volumeID=%s" , volumeID )
104
+ out , err := utils .RunPowershellCmd (cmd , cmdEnv )
103
105
104
106
if err != nil {
105
107
return fmt .Errorf ("error formatting volume. cmd: %s, output: %s, error: %v" , cmd , string (out ), err )
@@ -115,8 +117,10 @@ func (volumeAPI) WriteVolumeCache(volumeID string) (err error) {
115
117
116
118
// IsVolumeFormatted - Check if the volume is formatted with the pre specified filesystem(typically ntfs).
117
119
func (volumeAPI ) IsVolumeFormatted (volumeID string ) (bool , error ) {
118
- cmd := fmt .Sprintf ("(Get-Volume -UniqueId \" %s\" -ErrorAction Stop).FileSystemType" , volumeID )
119
- out , err := utils .RunPowershellCmd (cmd )
120
+ cmd := `(Get-Volume -UniqueId "$Env:volumeID" -ErrorAction Stop).FileSystemType`
121
+ cmdEnv := fmt .Sprintf ("volumeID=%s" , volumeID )
122
+ out , err := utils .RunPowershellCmd (cmd , cmdEnv )
123
+
120
124
if err != nil {
121
125
return false , fmt .Errorf ("error checking if volume is formatted. cmd: %s, output: %s, error: %v" , cmd , string (out ), err )
122
126
}
@@ -129,8 +133,12 @@ func (volumeAPI) IsVolumeFormatted(volumeID string) (bool, error) {
129
133
130
134
// MountVolume - mounts a volume to a path. This is done using the Add-PartitionAccessPath for presenting the volume via a path.
131
135
func (volumeAPI ) MountVolume (volumeID , path string ) error {
132
- cmd := fmt .Sprintf ("Get-Volume -UniqueId \" %s\" | Get-Partition | Add-PartitionAccessPath -AccessPath %s" , volumeID , path )
133
- out , err := utils .RunPowershellCmd (cmd )
136
+ cmd := `Get-Volume -UniqueId "$Env:volumeID" | Get-Partition | Add-PartitionAccessPath -AccessPath $Env:mountpath`
137
+ cmdEnv := []string {}
138
+ cmdEnv = append (cmdEnv , fmt .Sprintf ("volumeID=%s" , volumeID ))
139
+ cmdEnv = append (cmdEnv , fmt .Sprintf ("mountpath=%s" , path ))
140
+ out , err := utils .RunPowershellCmd (cmd , cmdEnv ... )
141
+
134
142
if err != nil {
135
143
return fmt .Errorf ("error mount volume to path. cmd: %s, output: %s, error: %v" , cmd , string (out ), err )
136
144
}
@@ -143,8 +151,13 @@ func (volumeAPI) UnmountVolume(volumeID, path string) error {
143
151
if err := writeCache (volumeID ); err != nil {
144
152
return err
145
153
}
146
- cmd := fmt .Sprintf ("Get-Volume -UniqueId \" %s\" | Get-Partition | Remove-PartitionAccessPath -AccessPath %s" , volumeID , path )
147
- out , err := utils .RunPowershellCmd (cmd )
154
+
155
+ cmd := `Get-Volume -UniqueId "$Env:volumeID" | Get-Partition | Remove-PartitionAccessPath -AccessPath $Env:mountpath`
156
+ cmdEnv := []string {}
157
+ cmdEnv = append (cmdEnv , fmt .Sprintf ("volumeID=%s" , volumeID ))
158
+ cmdEnv = append (cmdEnv , fmt .Sprintf ("mountpath=%s" , path ))
159
+ out , err := utils .RunPowershellCmd (cmd , cmdEnv ... )
160
+
148
161
if err != nil {
149
162
return fmt .Errorf ("error getting driver letter to mount volume. cmd: %s, output: %s,error: %v" , cmd , string (out ), err )
150
163
}
@@ -160,8 +173,9 @@ func (volumeAPI) ResizeVolume(volumeID string, size int64) error {
160
173
var finalSize int64
161
174
var outString string
162
175
if size == 0 {
163
- cmd = fmt .Sprintf ("Get-Volume -UniqueId \" %s\" | Get-partition | Get-PartitionSupportedSize | Select SizeMax | ConvertTo-Json" , volumeID )
164
- out , err = utils .RunPowershellCmd (cmd )
176
+ cmd = `Get-Volume -UniqueId "$Env:volumeID" | Get-partition | Get-PartitionSupportedSize | Select SizeMax | ConvertTo-Json`
177
+ cmdEnv := fmt .Sprintf ("volumeID=%s" , volumeID )
178
+ out , err := utils .RunPowershellCmd (cmd , cmdEnv )
165
179
166
180
if err != nil || len (out ) == 0 {
167
181
return fmt .Errorf ("error getting sizemin,sizemax from mount. cmd: %s, output: %s, error: %v" , cmd , string (out ), err )
@@ -192,8 +206,10 @@ func (volumeAPI) ResizeVolume(volumeID string, size int64) error {
192
206
return nil
193
207
}
194
208
195
- cmd = fmt .Sprintf ("Get-Volume -UniqueId \" %s\" | Get-Partition | Resize-Partition -Size %d" , volumeID , finalSize )
196
- out , err = utils .RunPowershellCmd (cmd )
209
+ cmd = fmt .Sprintf (`Get-Volume -UniqueId "$Env:volumeID" | Get-Partition | Resize-Partition -Size %d` , finalSize )
210
+ cmdEnv := []string {}
211
+ cmdEnv = append (cmdEnv , fmt .Sprintf ("volumeID=%s" , volumeID ))
212
+ out , err = utils .RunPowershellCmd (cmd , cmdEnv ... )
197
213
if err != nil {
198
214
return fmt .Errorf ("error resizing volume. cmd: %s, output: %s size:%v, finalSize %v, error: %v" , cmd , string (out ), size , finalSize , err )
199
215
}
@@ -203,8 +219,9 @@ func (volumeAPI) ResizeVolume(volumeID string, size int64) error {
203
219
// GetVolumeStats - retrieves the volume stats for a given volume
204
220
func (volumeAPI ) GetVolumeStats (volumeID string ) (int64 , int64 , error ) {
205
221
// get the size and sizeRemaining for the volume
206
- cmd := fmt .Sprintf ("(Get-Volume -UniqueId \" %s\" | Select SizeRemaining,Size) | ConvertTo-Json" , volumeID )
207
- out , err := utils .RunPowershellCmd (cmd )
222
+ cmd := `(Get-Volume -UniqueId "$Env:volumeID" | Select SizeRemaining,Size) | ConvertTo-Json`
223
+ cmdEnv := fmt .Sprintf ("volumeID=%s" , volumeID )
224
+ out , err := utils .RunPowershellCmd (cmd , cmdEnv )
208
225
209
226
if err != nil {
210
227
return - 1 , - 1 , fmt .Errorf ("error getting capacity and used size of volume. cmd: %s, output: %s, error: %v" , cmd , string (out ), err )
@@ -227,8 +244,9 @@ func (volumeAPI) GetVolumeStats(volumeID string) (int64, int64, error) {
227
244
// GetDiskNumberFromVolumeID - gets the disk number where the volume is.
228
245
func (volumeAPI ) GetDiskNumberFromVolumeID (volumeID string ) (uint32 , error ) {
229
246
// get the size and sizeRemaining for the volume
230
- cmd := fmt .Sprintf ("(Get-Volume -UniqueId \" %s\" | Get-Partition).DiskNumber" , volumeID )
231
- out , err := utils .RunPowershellCmd (cmd )
247
+ cmd := `(Get-Volume -UniqueId "$Env:volumeID" | Get-Partition).DiskNumber`
248
+ cmdEnv := fmt .Sprintf ("volumeID=%s" , volumeID )
249
+ out , err := utils .RunPowershellCmd (cmd , cmdEnv )
232
250
233
251
if err != nil || len (out ) == 0 {
234
252
return 0 , fmt .Errorf ("error getting disk number. cmd: %s, output: %s, error: %v" , cmd , string (out ), err )
@@ -261,8 +279,9 @@ func (volumeAPI) GetVolumeIDFromTargetPath(mount string) (string, error) {
261
279
}
262
280
263
281
func getTarget (mount string ) (string , error ) {
264
- cmd := fmt .Sprintf ("(Get-Item -Path %s).Target" , mount )
265
- out , err := utils .RunPowershellCmd (cmd )
282
+ cmd := `(Get-Item -Path $Env:mountpath).Target`
283
+ cmdEnv := fmt .Sprintf ("mountpath=%s" , mount )
284
+ out , err := utils .RunPowershellCmd (cmd , cmdEnv )
266
285
if err != nil || len (out ) == 0 {
267
286
return "" , fmt .Errorf ("error getting volume from mount. cmd: %s, output: %s, error: %v" , cmd , string (out ), err )
268
287
}
@@ -352,8 +371,9 @@ func ensureVolumePrefix(volume string) string {
352
371
353
372
// dereferenceSymlink dereferences the symlink `path` and returns the stdout.
354
373
func dereferenceSymlink (path string ) (string , error ) {
355
- cmd := fmt .Sprintf (`(Get-Item -Path %s).Target` , path )
356
- out , err := utils .RunPowershellCmd (cmd )
374
+ cmd := `(Get-Item -Path $Env:linkpath).Target`
375
+ cmdEnv := fmt .Sprintf ("linkpath=%s" , path )
376
+ out , err := utils .RunPowershellCmd (cmd , cmdEnv )
357
377
if err != nil {
358
378
return "" , err
359
379
}
@@ -368,8 +388,9 @@ func getVolumeForDriveLetter(path string) (string, error) {
368
388
return "" , fmt .Errorf ("The path=%s is not a valid DriverLetter" , path )
369
389
}
370
390
371
- cmd := fmt .Sprintf (`(Get-Partition -DriveLetter %s | Get-Volume).UniqueId` , path )
372
- out , err := utils .RunPowershellCmd (cmd )
391
+ cmd := `(Get-Partition -DriveLetter $Env:drivepath | Get-Volume).UniqueId`
392
+ cmdEnv := fmt .Sprintf ("drivepath=%s" , path )
393
+ out , err := utils .RunPowershellCmd (cmd , cmdEnv )
373
394
if err != nil {
374
395
return "" , err
375
396
}
@@ -379,8 +400,9 @@ func getVolumeForDriveLetter(path string) (string, error) {
379
400
}
380
401
381
402
func writeCache (volumeID string ) error {
382
- cmd := fmt .Sprintf ("Get-Volume -UniqueId \" %s\" | Write-Volumecache" , volumeID )
383
- out , err := utils .RunPowershellCmd (cmd )
403
+ cmd := `Get-Volume -UniqueId "$Env:volumeID" | Write-Volumecache`
404
+ cmdEnv := fmt .Sprintf ("volumeID=%s" , volumeID )
405
+ out , err := utils .RunPowershellCmd (cmd , cmdEnv )
384
406
if err != nil {
385
407
return fmt .Errorf ("error writing volume cache. cmd: %s, output: %s, error: %v" , cmd , string (out ), err )
386
408
}
0 commit comments