(r *Registry)
| 646 | } |
| 647 | |
| 648 | func existingFileMapper(r *Registry) MapperFunc { |
| 649 | return func(ctx *DecodeContext, target reflect.Value) error { |
| 650 | if target.Kind() == reflect.Slice { |
| 651 | return sliceDecoder(r)(ctx, target) |
| 652 | } |
| 653 | if target.Kind() != reflect.String { |
| 654 | return fmt.Errorf("\"existingfile\" type must be applied to a string not %s", target.Type()) |
| 655 | } |
| 656 | var path string |
| 657 | err := ctx.Scan.PopValueInto("file", &path) |
| 658 | if err != nil { |
| 659 | return err |
| 660 | } |
| 661 | |
| 662 | if !ctx.Value.Active || (ctx.Value.Set && ctx.Value.Target.Type() == target.Type()) { |
| 663 | // early return to avoid checking extra files that may not exist; |
| 664 | // this hack only works because the value provided on the cli is |
| 665 | // checked before the default value is checked (if default is set). |
| 666 | return nil |
| 667 | } |
| 668 | |
| 669 | if path != "-" { |
| 670 | path = ExpandPath(path) |
| 671 | stat, err := os.Stat(path) |
| 672 | if err != nil { |
| 673 | return err |
| 674 | } |
| 675 | if stat.IsDir() { |
| 676 | return fmt.Errorf("%q exists but is a directory", path) |
| 677 | } |
| 678 | } |
| 679 | target.SetString(path) |
| 680 | return nil |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | func existingDirMapper(r *Registry) MapperFunc { |
| 685 | return func(ctx *DecodeContext, target reflect.Value) error { |
no test coverage detected