9
9
"path"
10
10
"path/filepath"
11
11
"reflect"
12
+ "sort"
12
13
"strings"
13
14
14
15
osh "github.com/Kubuxu/go-os-helper"
@@ -236,7 +237,13 @@ func parseArgs(req *cmds.Request, root *cmds.Command, stdin *os.File) error {
236
237
}
237
238
238
239
stringArgs := make ([]string , 0 , numInputs )
239
- fileArgs := make (map [string ]files.Node )
240
+ fileArgs := make ([]files.DirEntry , 0 )
241
+ // Each file argument's import directory name is recorded under its base name
242
+ // to reject two files with the same name but different import directories
243
+ // (same directory just means the _exact_ same file, so we can skip it):
244
+ // file base name -> file directory name
245
+ fileImportDirName := make (map [string ]string )
246
+ var fileStdin files.Node
240
247
241
248
// the index of the current argument definition
242
249
iArgDef := 0
@@ -266,7 +273,7 @@ func parseArgs(req *cmds.Request, root *cmds.Command, stdin *os.File) error {
266
273
stringArgs , inputs = append (stringArgs , inputs [0 ]), inputs [1 :]
267
274
} else if stdin != nil && argDef .SupportsStdin && ! fillingVariadic {
268
275
if r , err := maybeWrapStdin (stdin , msgStdinInfo ); err == nil {
269
- fileArgs [ "stdin" ] , err = files .NewReaderPathFile (stdin .Name (), r , nil )
276
+ fileStdin , err = files .NewReaderPathFile (stdin .Name (), r , nil )
270
277
if err != nil {
271
278
return err
272
279
}
@@ -291,17 +298,7 @@ func parseArgs(req *cmds.Request, root *cmds.Command, stdin *os.File) error {
291
298
return err
292
299
}
293
300
} else if u := isURL (fpath ); u != nil {
294
- base := urlBase (u )
295
- fpath = base
296
- if _ , ok := fileArgs [fpath ]; ok {
297
- // Ensure a unique fpath by suffixing ' (n)'.
298
- for i := 1 ; ; i ++ {
299
- fpath = fmt .Sprintf ("%s (%d)" , base , i )
300
- if _ , ok := fileArgs [fpath ]; ! ok {
301
- break
302
- }
303
- }
304
- }
301
+ fpath = urlBase (u )
305
302
file = files .NewWebFile (u )
306
303
} else {
307
304
fpath = filepath .Clean (fpath )
@@ -333,18 +330,29 @@ func parseArgs(req *cmds.Request, root *cmds.Command, stdin *os.File) error {
333
330
}
334
331
335
332
fpath = filepath .Base (fpath )
333
+ importDir := filepath .Dir (fpath )
334
+ prevDir , ok := fileImportDirName [fpath ]
335
+ if ! ok {
336
+ fileImportDirName [fpath ] = importDir
337
+ } else {
338
+ if prevDir != importDir {
339
+ return fmt .Errorf ("file name %s repeated under different import directories: %s and %s" ,
340
+ fpath , importDir , prevDir )
341
+ }
342
+ continue // Skip repeated files.
343
+ }
336
344
file = nf
337
345
}
338
346
339
- fileArgs [ fpath ] = file
347
+ fileArgs = append ( fileArgs , files . FileEntry ( fpath , file ))
340
348
} else if stdin != nil && argDef .SupportsStdin &&
341
349
argDef .Required && ! fillingVariadic {
342
350
r , err := maybeWrapStdin (stdin , msgStdinInfo )
343
351
if err != nil {
344
352
return err
345
353
}
346
354
347
- fileArgs [ stdinName ( req )] , err = files .NewReaderPathFile (stdin .Name (), r , nil )
355
+ fileStdin , err = files .NewReaderPathFile (stdin .Name (), r , nil )
348
356
if err != nil {
349
357
return err
350
358
}
@@ -370,8 +378,14 @@ func parseArgs(req *cmds.Request, root *cmds.Command, stdin *os.File) error {
370
378
}
371
379
372
380
req .Arguments = stringArgs
381
+ if fileStdin != nil {
382
+ fileArgs = append (fileArgs , files .FileEntry (stdinName (req ), fileStdin ))
383
+ }
373
384
if len (fileArgs ) > 0 {
374
- req .Files = files .NewMapDirectory (fileArgs )
385
+ sort .Slice (fileArgs , func (i , j int ) bool {
386
+ return fileArgs [i ].Name () < fileArgs [j ].Name ()
387
+ })
388
+ req .Files = files .NewSliceDirectory (fileArgs )
375
389
}
376
390
377
391
return nil
0 commit comments