diff --git a/main/commands/all/base64/base64.go b/main/commands/all/base64/base64.go index 7bdc41977..84438fa5d 100644 --- a/main/commands/all/base64/base64.go +++ b/main/commands/all/base64/base64.go @@ -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]) } diff --git a/main/commands/all/base64/decode.go b/main/commands/all/base64/decode.go index 7d3298818..1b7b74927 100644 --- a/main/commands/all/base64/decode.go +++ b/main/commands/all/base64/decode.go @@ -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) } diff --git a/main/commands/all/base64/encode.go b/main/commands/all/base64/encode.go index 7e3c16dde..1378bc06f 100644 --- a/main/commands/all/base64/encode.go +++ b/main/commands/all/base64/encode.go @@ -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) }