cmd: omit file or pass "-" or "stdin:" to read stdin

This commit is contained in:
Meo597
2026-10-12 20:38:22 +08:00
parent 35de242f0a
commit 8f40112a3f
3 changed files with 14 additions and 14 deletions
+3 -3
View File
@@ -18,9 +18,9 @@ var CmdBase64 = &base.Command{
},
}
func readInput(name string) ([]byte, error) {
if name == "stdin:" {
func readInput(args []string) ([]byte, error) {
if len(args) == 0 || args[0] == "-" || args[0] == "stdin:" {
return io.ReadAll(os.Stdin)
}
return os.ReadFile(name)
return os.ReadFile(args[0])
}
+5 -5
View File
@@ -10,17 +10,17 @@ import (
)
var cmdDecode = &base.Command{
UsageLine: "{{.Exec}} base64 decode [-gzip] [file | stdin:]",
UsageLine: "{{.Exec}} base64 decode [-gzip] [file | - | stdin:]",
Short: "Decode Base64",
Long: fmt.Sprintf(`
Decode Base64 from a file or stdin:.
Decode Base64 from a file; omit file or pass "-" or "stdin:" to read stdin.
Accepts %s, %s, or no prefix.
Use -gzip to decompress gzip; %s is decompressed automatically.
Examples:
{{.Exec}} base64 decode encoded.txt
{{.Exec}} base64 decode -gzip stdin:
{{.Exec}} base64 decode -gzip -
`, xbase64.Prefix, xbase64.GzipPrefix, xbase64.GzipPrefix),
}
@@ -31,10 +31,10 @@ func init() {
}
func executeDecode(cmd *base.Command, args []string) {
if len(args) != 1 {
if len(args) > 1 {
cmd.Usage()
}
input, err := readInput(args[0])
input, err := readInput(args)
if err != nil {
base.Fatalf("failed to read input: %s", err)
}
+6 -6
View File
@@ -9,17 +9,17 @@ import (
)
var cmdEncode = &base.Command{
UsageLine: "{{.Exec}} base64 encode [-gzip] [file | stdin:]",
UsageLine: "{{.Exec}} base64 encode [-gzip] [file | - | stdin:]",
Short: "Encode as Base64",
Long: fmt.Sprintf(`
Encode a file or stdin: as Base64 with the %s prefix.
Use -gzip to compress with gzip and output %s.
Encode a file as Base64 with the %s prefix.
Omit file or pass "-" or "stdin:" to read stdin. Use -gzip to compress and output %s.
Examples:
{{.Exec}} base64 encode data.bin
{{.Exec}} base64 encode -gzip data.bin
{{.Exec}} base64 encode stdin:
{{.Exec}} base64 encode
`, xbase64.Prefix, xbase64.GzipPrefix),
}
@@ -30,10 +30,10 @@ func init() {
}
func executeEncode(cmd *base.Command, args []string) {
if len(args) != 1 {
if len(args) > 1 {
cmd.Usage()
}
data, err := readInput(args[0])
data, err := readInput(args)
if err != nil {
base.Fatalf("failed to read input: %s", err)
}