mirror of
https://github.com/thegeeklab/wp-github-comment.git
synced 2024-11-22 00:00:42 +00:00
chore: use file.ReadStringOrFile from plugin lib (#88)
This commit is contained in:
parent
a6af05c8a4
commit
cf7cda3da8
@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
"github.com/google/go-github/v59/github"
|
"github.com/google/go-github/v59/github"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
"github.com/thegeeklab/wp-plugin-go/file"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -38,7 +39,7 @@ func (p *Plugin) Validate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if p.Settings.Message != "" {
|
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)
|
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)
|
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)
|
return fmt.Errorf("error while reading %s: %w", p.Settings.Key, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user