Skip to content

Commit 4640104

Browse files
committed
refactor(cmd): update encode logic to handle errors and add new formats
Update the `applyFormatEncoder` function to return an error when an unsupported format is provided. Add support for "hex" and "gob" formats. Also, handle the error in the `Execute` method to prevent silent failures.
1 parent f950cc8 commit 4640104

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

cmd/encode.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cmd
22

33
import (
4+
"errors"
5+
46
"github.com/spf13/cobra"
57
"github.com/vldcreation/helpme-package/pkg/encode"
68
"github.com/vldcreation/helpme/util"
@@ -45,7 +47,9 @@ func (c *encodeCmd) Command() *cobra.Command {
4547

4648
func (c *encodeCmd) Execute(_ *cobra.Command, args []string) {
4749
encoder := switchEncoder(c.source, c.encoder)
48-
applyFormatEncoder(encoder, c.source, c.format)
50+
if err := applyFormatEncoder(encoder, c.source, c.format); err != nil {
51+
panic(err.Error())
52+
}
4953
encoder.ApplyOpt(encode.WithCopyToClipboard(c.copyToClipboard), encode.WithMimeType(c.withMimeType))
5054

5155
out, err := encoder.Encode()
@@ -68,13 +72,18 @@ func switchEncoder(source string, encoder string) encode.Encoder {
6872
}
6973
}
7074

71-
func applyFormatEncoder(e encode.Encoder, source string, format string) {
75+
func applyFormatEncoder(e encode.Encoder, source string, format string) error {
7276
switch format {
7377
case "base64":
7478
e.ApplyOpt(encode.WithFormatEncoder(encode.NewBase64Encoder(source)))
7579
case "base32":
7680
e.ApplyOpt(encode.WithFormatEncoder(encode.NewBase32Encoder(source)))
81+
case "hex":
82+
e.ApplyOpt(encode.WithFormatEncoder(&encode.HexEncoder{}))
83+
case "gob":
84+
e.ApplyOpt(encode.WithFormatEncoder(encode.NewGobEncoder()))
7785
default:
78-
e.ApplyOpt(encode.WithFormatEncoder(encode.NewBase64Encoder(source)))
86+
return errors.New("format encoder not available")
7987
}
88+
return nil
8089
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.24.1
44

55
require (
66
github.com/spf13/cobra v1.8.1
7-
github.com/vldcreation/helpme-package/pkg v0.0.0-20250318055914-504928e8d42c
7+
github.com/vldcreation/helpme-package/pkg v0.0.0-20250319034329-1996718746bf
88
golang.org/x/net v0.35.0
99
)
1010

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ github.com/vldcreation/helpme-package/pkg v0.0.0-20250318055416-f6df0d664710 h1:
3232
github.com/vldcreation/helpme-package/pkg v0.0.0-20250318055416-f6df0d664710/go.mod h1:Vu8Z4QG5jligqd8GJ3iOKnBDHZd3yyQ7YydBmAD1JGk=
3333
github.com/vldcreation/helpme-package/pkg v0.0.0-20250318055914-504928e8d42c h1:U5g3aj81HhwRPbsPi9JaVtLjMbKBiZ7ioHQGoQSa6Dw=
3434
github.com/vldcreation/helpme-package/pkg v0.0.0-20250318055914-504928e8d42c/go.mod h1:Vu8Z4QG5jligqd8GJ3iOKnBDHZd3yyQ7YydBmAD1JGk=
35+
github.com/vldcreation/helpme-package/pkg v0.0.0-20250319033435-58fa32f9147b h1:JquykNyIXDaZDhSGp1L4d4w2pwqQvFPVgaiE/jIXdKs=
36+
github.com/vldcreation/helpme-package/pkg v0.0.0-20250319033435-58fa32f9147b/go.mod h1:Vu8Z4QG5jligqd8GJ3iOKnBDHZd3yyQ7YydBmAD1JGk=
37+
github.com/vldcreation/helpme-package/pkg v0.0.0-20250319033709-b3a0698e514b h1:WDXZ+obULQSImxiqbBk/nRl7boBfD2p3+zr2tNYk6As=
38+
github.com/vldcreation/helpme-package/pkg v0.0.0-20250319033709-b3a0698e514b/go.mod h1:Vu8Z4QG5jligqd8GJ3iOKnBDHZd3yyQ7YydBmAD1JGk=
39+
github.com/vldcreation/helpme-package/pkg v0.0.0-20250319034329-1996718746bf h1:qTGn1Lmv/NMQUHAyAd5E+pYdvcqZ1VSX/SBDvdv41zQ=
40+
github.com/vldcreation/helpme-package/pkg v0.0.0-20250319034329-1996718746bf/go.mod h1:Vu8Z4QG5jligqd8GJ3iOKnBDHZd3yyQ7YydBmAD1JGk=
3541
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
3642
golang.design/x/clipboard v0.7.0 h1:4Je8M/ys9AJumVnl8m+rZnIvstSnYj1fvzqYrU3TXvo=
3743
golang.design/x/clipboard v0.7.0/go.mod h1:PQIvqYO9GP29yINEfsEn5zSQKAz3UgXmZKzDA6dnq2E=

0 commit comments

Comments
 (0)