parseArg splits the `path#alt text` form of an --attach argument. An existing path wins over the delimiter, since `#` is legal in filenames.
(arg string)
| 100 | // parseArg splits the `path#alt text` form of an --attach argument. An existing |
| 101 | // path wins over the delimiter, since `#` is legal in filenames. |
| 102 | func parseArg(arg string) (path, alt string) { |
| 103 | if _, err := os.Stat(arg); err == nil { |
| 104 | return arg, "" |
| 105 | } |
| 106 | // Scan from the last hash to the first, so the longest path that exists |
| 107 | // wins. That continues the rule above, where the whole argument is the |
| 108 | // longest match of all, and it leaves a hash inside the alt text usable. |
| 109 | for i := strings.LastIndex(arg, "#"); i > 0; i = strings.LastIndex(arg[:i], "#") { |
| 110 | if _, err := os.Stat(arg[:i]); err == nil { |
| 111 | return arg[:i], arg[i+1:] |
| 112 | } |
| 113 | } |
| 114 | // No candidate exists, so the argument names a file that is not there. The |
| 115 | // last hash keeps the error naming the path a reader would expect. |
| 116 | if idx := strings.LastIndex(arg, "#"); idx > 0 { |
| 117 | return arg[:idx], arg[idx+1:] |
| 118 | } |
| 119 | return arg, "" |
| 120 | } |