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