cmd: refine

This commit is contained in:
Meo597
2026-10-12 22:20:45 +08:00
parent 6e6a592996
commit 3c74bcdf2a
2 changed files with 9 additions and 11 deletions
+5 -7
View File
@@ -11,10 +11,10 @@ import (
)
var cmdDecode = &base.Command{
UsageLine: "{{.Exec}} base64 decode [-gzip] [value | - | stdin:] [output-file]",
UsageLine: "{{.Exec}} base64 decode [-gzip] <value | - | stdin:> [output-file]",
Short: "Decode Base64",
Long: fmt.Sprintf(`
Decode a Base64 value; omit value or pass "-" or "stdin:" to read stdin.
Decode a Base64 value, or pass "-" or "stdin:" to read stdin.
Write to output-file, or stdout if omitted.
Accepts %s, %s, or no prefix; gzip is enabled by %s or -gzip.
@@ -32,19 +32,17 @@ func init() {
}
func executeDecode(cmd *base.Command, args []string) {
if len(args) > 2 {
if len(args) == 0 || len(args) > 2 {
cmd.Usage()
}
var value string
if len(args) == 0 || args[0] == "-" || args[0] == "stdin:" {
value := args[0]
if value == "-" || value == "stdin:" {
input, err := io.ReadAll(os.Stdin)
if err != nil {
base.Fatalf("failed to read input: %s", err)
}
value = string(input)
} else {
value = args[0]
}
value = strings.TrimSpace(value)
+4 -4
View File
@@ -10,11 +10,11 @@ import (
)
var cmdEncode = &base.Command{
UsageLine: "{{.Exec}} base64 encode [-gzip] [input-file | - | stdin:]",
UsageLine: "{{.Exec}} base64 encode [-gzip] <input-file | - | stdin:>",
Short: "Encode as Base64",
Long: fmt.Sprintf(`
Encode input-file as Base64 with the %s prefix.
Omit input-file or pass "-" or "stdin:" to read stdin.
Pass "-" or "stdin:" to read stdin.
Use -gzip to compress and output %s.
Examples:
@@ -31,13 +31,13 @@ func init() {
}
func executeEncode(cmd *base.Command, args []string) {
if len(args) > 1 {
if len(args) != 1 {
cmd.Usage()
}
var data []byte
var err error
if len(args) == 0 || args[0] == "-" || args[0] == "stdin:" {
if args[0] == "-" || args[0] == "stdin:" {
data, err = io.ReadAll(os.Stdin)
} else {
data, err = os.ReadFile(args[0])