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

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

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

View File

@ -10,6 +10,7 @@ import (
"github.com/google/go-github/v59/github"
"github.com/rs/zerolog/log"
"github.com/thegeeklab/wp-plugin-go/file"
"golang.org/x/oauth2"
)
@ -38,7 +39,7 @@ func (p *Plugin) Validate() error {
}
if p.Settings.Message != "" {
if p.Settings.Message, p.Settings.IsFile, err = readStringOrFile(p.Settings.Message); err != nil {
if p.Settings.Message, p.Settings.IsFile, err = file.ReadStringOrFile(p.Settings.Message); err != nil {
return fmt.Errorf("error while reading %s: %w", p.Settings.Message, err)
}
}
@ -58,7 +59,7 @@ func (p *Plugin) Validate() error {
p.Settings.Key = fmt.Sprintf("%x", hash)
}
if p.Settings.Key, _, err = readStringOrFile(p.Settings.Key); err != nil {
if p.Settings.Key, _, err = file.ReadStringOrFile(p.Settings.Key); err != nil {
return fmt.Errorf("error while reading %s: %w", p.Settings.Key, err)
}

View File

@ -1,27 +0,0 @@
package plugin
import (
"os"
)
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
}