From 4df2c5a06d93f2f986add41635e1881b442556e8 Mon Sep 17 00:00:00 2001 From: krau <71133316+krau@users.noreply.github.com> Date: Tue, 17 Jun 2025 22:19:06 +0800 Subject: [PATCH] refactor: replace key package with ctxkey for context keys --- core/batchtftask/execute.go | 4 ++-- core/tftask/execute.go | 4 ++-- pkg/enums/ctxkey/context_key.go | 5 +++++ pkg/enums/{key => ctxkey}/context_key_enum.go | 12 ++++++------ pkg/enums/key/context_key.go | 5 ----- storage/alist/alist.go | 4 ++-- storage/minio/client.go | 4 ++-- storage/webdav/client.go | 4 ++-- 8 files changed, 21 insertions(+), 21 deletions(-) create mode 100644 pkg/enums/ctxkey/context_key.go rename pkg/enums/{key => ctxkey}/context_key_enum.go (88%) delete mode 100644 pkg/enums/key/context_key.go diff --git a/core/batchtftask/execute.go b/core/batchtftask/execute.go index 3affcb9..6d60fa0 100644 --- a/core/batchtftask/execute.go +++ b/core/batchtftask/execute.go @@ -13,7 +13,7 @@ import ( "github.com/krau/SaveAny-Bot/common/utils/fsutil" "github.com/krau/SaveAny-Bot/common/utils/ioutil" "github.com/krau/SaveAny-Bot/config" - "github.com/krau/SaveAny-Bot/pkg/enums/key" + "github.com/krau/SaveAny-Bot/pkg/enums/ctxkey" "golang.org/x/sync/errgroup" ) @@ -104,7 +104,7 @@ func (t *Task) processElement(ctx context.Context, elem TaskElement) error { if err != nil { return fmt.Errorf("failed to get file stat: %w", err) } - vctx := context.WithValue(ctx, key.ContextKeyContentLength, fileStat.Size()) + vctx := context.WithValue(ctx, ctxkey.ContentLength, fileStat.Size()) err = retry.Retry(func() error { var file *os.File file, err = os.Open(elem.localPath) diff --git a/core/tftask/execute.go b/core/tftask/execute.go index 29a74c4..4002e61 100644 --- a/core/tftask/execute.go +++ b/core/tftask/execute.go @@ -11,7 +11,7 @@ import ( "github.com/krau/SaveAny-Bot/common/tdler" "github.com/krau/SaveAny-Bot/common/utils/fsutil" "github.com/krau/SaveAny-Bot/config" - "github.com/krau/SaveAny-Bot/pkg/enums/key" + "github.com/krau/SaveAny-Bot/pkg/enums/ctxkey" ) func (t *TGFileTask) Execute(ctx context.Context) error { @@ -52,7 +52,7 @@ func (t *TGFileTask) Execute(ctx context.Context) error { if err != nil { return fmt.Errorf("failed to get file stat: %w", err) } - vctx := context.WithValue(ctx, key.ContextKeyContentLength, fileStat.Size()) + vctx := context.WithValue(ctx, ctxkey.ContentLength, fileStat.Size()) for i := range config.Cfg.Retry + 1 { if err = vctx.Err(); err != nil { return fmt.Errorf("context canceled while saving file: %w", err) diff --git a/pkg/enums/ctxkey/context_key.go b/pkg/enums/ctxkey/context_key.go new file mode 100644 index 0000000..a56ead8 --- /dev/null +++ b/pkg/enums/ctxkey/context_key.go @@ -0,0 +1,5 @@ +package ctxkey + +//go:generate go-enum --values --names --flag --nocase --noprefix +// ENUM(content-length) +type ContextKey string diff --git a/pkg/enums/key/context_key_enum.go b/pkg/enums/ctxkey/context_key_enum.go similarity index 88% rename from pkg/enums/key/context_key_enum.go rename to pkg/enums/ctxkey/context_key_enum.go index 942dc66..7a77844 100644 --- a/pkg/enums/key/context_key_enum.go +++ b/pkg/enums/ctxkey/context_key_enum.go @@ -4,7 +4,7 @@ // Build Date: 2025-03-18T23:42:14Z // Built By: goreleaser -package key +package ctxkey import ( "fmt" @@ -12,14 +12,14 @@ import ( ) const ( - // ContextKeyContentLength is a ContextKey of type content-length. - ContextKeyContentLength ContextKey = "content-length" + // ContentLength is a ContextKey of type content-length. + ContentLength ContextKey = "content-length" ) var ErrInvalidContextKey = fmt.Errorf("not a valid ContextKey, try [%s]", strings.Join(_ContextKeyNames, ", ")) var _ContextKeyNames = []string{ - string(ContextKeyContentLength), + string(ContentLength), } // ContextKeyNames returns a list of possible string values of ContextKey. @@ -32,7 +32,7 @@ func ContextKeyNames() []string { // ContextKeyValues returns a list of the values for ContextKey func ContextKeyValues() []ContextKey { return []ContextKey{ - ContextKeyContentLength, + ContentLength, } } @@ -49,7 +49,7 @@ func (x ContextKey) IsValid() bool { } var _ContextKeyValue = map[string]ContextKey{ - "content-length": ContextKeyContentLength, + "content-length": ContentLength, } // ParseContextKey attempts to convert a string to a ContextKey. diff --git a/pkg/enums/key/context_key.go b/pkg/enums/key/context_key.go deleted file mode 100644 index 4c41074..0000000 --- a/pkg/enums/key/context_key.go +++ /dev/null @@ -1,5 +0,0 @@ -package key - -//go:generate go-enum --values --names --flag --nocase -// ENUM(content-length) -type ContextKey string diff --git a/storage/alist/alist.go b/storage/alist/alist.go index d04f7ac..d0438d1 100644 --- a/storage/alist/alist.go +++ b/storage/alist/alist.go @@ -14,7 +14,7 @@ import ( "github.com/charmbracelet/log" config "github.com/krau/SaveAny-Bot/config/storage" - "github.com/krau/SaveAny-Bot/pkg/enums/key" + "github.com/krau/SaveAny-Bot/pkg/enums/ctxkey" storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage" ) @@ -118,7 +118,7 @@ func (a *Alist) Save(ctx context.Context, reader io.Reader, storagePath string) req.Header.Set("Authorization", a.token) req.Header.Set("File-Path", url.PathEscape(candidate)) req.Header.Set("Content-Type", "application/octet-stream") - if length := ctx.Value(key.ContextKeyContentLength); length != nil { + if length := ctx.Value(ctxkey.ContentLength); length != nil { length, ok := length.(int64) if ok { req.ContentLength = length diff --git a/storage/minio/client.go b/storage/minio/client.go index 9b21a87..4dda7aa 100644 --- a/storage/minio/client.go +++ b/storage/minio/client.go @@ -9,7 +9,7 @@ import ( "github.com/charmbracelet/log" config "github.com/krau/SaveAny-Bot/config/storage" - "github.com/krau/SaveAny-Bot/pkg/enums/key" + "github.com/krau/SaveAny-Bot/pkg/enums/ctxkey" storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage" "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" @@ -74,7 +74,7 @@ func (m *Minio) Save(ctx context.Context, r io.Reader, storagePath string) error candidate = fmt.Sprintf("%s_%d%s", base, i, ext) } size := int64(-1) - if length := ctx.Value(key.ContextKeyContentLength); length != nil { + if length := ctx.Value(ctxkey.ContentLength); length != nil { length, ok := length.(int64) if ok && length > 0 { size = length diff --git a/storage/webdav/client.go b/storage/webdav/client.go index 2b3f520..e16abc1 100644 --- a/storage/webdav/client.go +++ b/storage/webdav/client.go @@ -9,7 +9,7 @@ import ( "path" "strings" - "github.com/krau/SaveAny-Bot/pkg/enums/key" + "github.com/krau/SaveAny-Bot/pkg/enums/ctxkey" ) type Client struct { @@ -54,7 +54,7 @@ func (c *Client) doRequest(ctx context.Context, method WebdavMethod, url string, req.Header.Set("Depth", "1") } if method == WebdavMethodPut && ctx != nil { - if length := ctx.Value(key.ContextKeyContentLength); length != nil { + if length := ctx.Value(ctxkey.ContentLength); length != nil { if l, ok := length.(int64); ok { req.ContentLength = l }