| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"os" |
| 7 |
) |
| 8 |
|
| 9 |
// stdioPath is the special path value that selects stdin (for inputs) or |
| 10 |
// stdout (for outputs) instead of a file on disk. |
| 11 |
const stdioPath = "-" |
| 12 |
|
| 13 |
// stdioHelpText is rendered just under the Usage line of each subcommand |
| 14 |
// help so the - convention is documented once instead of on every flag. |
| 15 |
const stdioHelpText = " Pass \"-\" to any path flag to read from stdin or write to stdout.\n" |
| 16 |
|
| 17 |
// stdinReader is the source used when an input flag is set to "-". |
| 18 |
// It is a package level var so tests can swap in a deterministic reader. |
| 19 |
// Tests that mutate stdinReader cannot run with t.Parallel(). |
| 20 |
var stdinReader io.Reader = os.Stdin |
| 21 |
|
| 22 |
// ioClaims tracks which flags have claimed stdin and stdout during a single |
| 23 |
// command invocation so we can refuse a second flag asking for the same |
| 24 |
// stream. |
| 25 |
type ioClaims struct { |
| 26 |
in string |
| 27 |
out string |
| 28 |
} |
| 29 |
|
| 30 |
func (c *ioClaims) claimIn(flagName string) error { |
| 31 |
if c.in != "" && c.in != flagName { |
| 32 |
return fmt.Errorf("-%s and -%s both set to %q, only one input may read from stdin", c.in, flagName, stdioPath) |
| 33 |
} |
| 34 |
c.in = flagName |
| 35 |
return nil |
| 36 |
} |
| 37 |
|
| 38 |
func (c *ioClaims) claimOut(flagName string) error { |
| 39 |
if c.out != "" && c.out != flagName { |
| 40 |
return fmt.Errorf("-%s and -%s both set to %q, only one output may write to stdout", c.out, flagName, stdioPath) |
| 41 |
} |
| 42 |
c.out = flagName |
| 43 |
return nil |
| 44 |
} |
| 45 |
|
| 46 |
// reserveInputs walks alternating (flagName, path) pairs and claims stdin |
| 47 |
// for any path equal to stdioPath. It must be called before any input is |
| 48 |
// read so a conflict can be reported immediately instead of blocking on |
| 49 |
// io.ReadAll while waiting for input that will never arrive. |
| 50 |
func reserveInputs(claims *ioClaims, pairs ...string) error { |
| 51 |
return reserveStdio(claims, "reserveInputs", (*ioClaims).claimIn, pairs) |
| 52 |
} |
| 53 |
|
| 54 |
// reserveOutputs walks alternating (flagName, path) pairs and claims stdout |
| 55 |
// for any path equal to stdioPath. It must be called before any output is |
| 56 |
// written so a conflict cannot leave one stream half written before the |
| 57 |
// second flag fails. |
| 58 |
func reserveOutputs(claims *ioClaims, pairs ...string) error { |
| 59 |
return reserveStdio(claims, "reserveOutputs", (*ioClaims).claimOut, pairs) |
| 60 |
} |
| 61 |
|
| 62 |
func reserveStdio(claims *ioClaims, who string, claim func(*ioClaims, string) error, pairs []string) error { |
| 63 |
if len(pairs)%2 != 0 { |
| 64 |
panic(who + " requires alternating name, path pairs") |
| 65 |
} |
| 66 |
for i := 0; i < len(pairs); i += 2 { |
| 67 |
name, path := pairs[i], pairs[i+1] |
| 68 |
if path != stdioPath { |
| 69 |
continue |
| 70 |
} |
| 71 |
if err := claim(claims, name); err != nil { |
| 72 |
return err |
| 73 |
} |
| 74 |
} |
| 75 |
return nil |
| 76 |
} |
| 77 |
|
| 78 |
// readInput returns the bytes referenced by path, reading from stdin when |
| 79 |
// path is stdioPath. |
| 80 |
func readInput(flagName, path string, claims *ioClaims) ([]byte, error) { |
| 81 |
if path == stdioPath { |
| 82 |
if err := claims.claimIn(flagName); err != nil { |
| 83 |
return nil, err |
| 84 |
} |
| 85 |
return io.ReadAll(stdinReader) |
| 86 |
} |
| 87 |
return os.ReadFile(path) |
| 88 |
} |
| 89 |
|
| 90 |
// openInput returns a reader for path. When path is stdioPath the returned |
| 91 |
// reader wraps stdin and Close is a no-op. |
| 92 |
func openInput(flagName, path string, claims *ioClaims) (io.ReadCloser, error) { |
| 93 |
if path == stdioPath { |
| 94 |
if err := claims.claimIn(flagName); err != nil { |
| 95 |
return nil, err |
| 96 |
} |
| 97 |
return io.NopCloser(stdinReader), nil |
| 98 |
} |
| 99 |
return os.Open(path) |
| 100 |
} |
| 101 |
|
| 102 |
// writeOutput writes data to path, or to stdout when path is stdioPath. perm |
| 103 |
// is only used for file output. The caller must have already claimed stdout |
| 104 |
// via reserveOutputs before invoking with stdioPath. |
| 105 |
func writeOutput(path string, data []byte, perm os.FileMode, stdout io.Writer) error { |
| 106 |
if path == stdioPath { |
| 107 |
_, err := stdout.Write(data) |
| 108 |
return err |
| 109 |
} |
| 110 |
return os.WriteFile(path, data, perm) |
| 111 |
} |
| 112 |
|
| 113 |
// isStdio reports whether path is the stdio sentinel and so should skip |
| 114 |
// existence checks like "refuse to overwrite". |
| 115 |
func isStdio(path string) bool { |
| 116 |
return path == stdioPath |
| 117 |
} |