0
0
mirror of https://github.com/thegeeklab/wp-gitea-release.git synced 2024-11-24 23:10:39 +00:00

chore: use file.ReadStringOrFile from plugin lib (#18)

This commit is contained in:
Robert Kaussow 2024-02-09 22:23:55 +01:00 committed by GitHub
parent 7d865f8f21
commit 7c5b42c481
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 3 additions and 25 deletions

View File

@ -10,6 +10,7 @@ import (
"strings" "strings"
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
"github.com/thegeeklab/wp-plugin-go/file"
) )
var ( var (
@ -53,13 +54,13 @@ func (p *Plugin) Validate() error {
} }
if p.Settings.Note != "" { if p.Settings.Note != "" {
if p.Settings.Note, _, err = readStringOrFile(p.Settings.Note); err != nil { if p.Settings.Note, _, err = file.ReadStringOrFile(p.Settings.Note); err != nil {
return fmt.Errorf("error while reading %s: %w", p.Settings.Note, err) return fmt.Errorf("error while reading %s: %w", p.Settings.Note, err)
} }
} }
if p.Settings.Title != "" { if p.Settings.Title != "" {
if p.Settings.Title, _, err = readStringOrFile(p.Settings.Title); err != nil { if p.Settings.Title, _, err = file.ReadStringOrFile(p.Settings.Title); err != nil {
return fmt.Errorf("error while reading %s: %w", p.Settings.Title, err) return fmt.Errorf("error while reading %s: %w", p.Settings.Title, err)
} }
} }

View File

@ -19,29 +19,6 @@ import (
var ErrHashMethodNotSupported = errors.New("hash method not supported") var ErrHashMethodNotSupported = errors.New("hash method not supported")
//nolint:unparam
func readStringOrFile(input string) (string, bool, error) {
//nolint:gomnd
if len(input) > 255 {
return input, false, nil
}
// Check if input is a file path
if _, err := os.Stat(input); err != nil && os.IsNotExist(err) {
// No file found => use input as result
return input, false, nil
} else if err != nil {
return "", false, err
}
result, err := os.ReadFile(input)
if err != nil {
return "", true, err
}
return string(result), true, nil
}
func checksum(r io.Reader, method string) (string, error) { func checksum(r io.Reader, method string) (string, error) {
b, err := io.ReadAll(r) b, err := io.ReadAll(r)
if err != nil { if err != nil {