mirror of
https://github.com/thegeeklab/wp-gitea-release.git
synced 2024-11-21 13:50:40 +00:00
chore: use file.ReadStringOrFile
from plugin lib (#18)
This commit is contained in:
parent
7d865f8f21
commit
7c5b42c481
@ -10,6 +10,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"github.com/thegeeklab/wp-plugin-go/file"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -53,13 +54,13 @@ func (p *Plugin) Validate() error {
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -19,29 +19,6 @@ import (
|
||||
|
||||
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) {
|
||||
b, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user