summaryrefslogtreecommitdiff
path: root/plugin/special/stdin.go
diff options
context:
space:
mode:
Diffstat (limited to 'plugin/special/stdin.go')
-rw-r--r--plugin/special/stdin.go81
1 files changed, 54 insertions, 27 deletions
diff --git a/plugin/special/stdin.go b/plugin/special/stdin.go
index f2f9cf9c..459bdf9f 100644
--- a/plugin/special/stdin.go
+++ b/plugin/special/stdin.go
@@ -3,7 +3,7 @@ package special
import (
"bufio"
"encoding/json"
- "fmt"
+ "log"
"os"
"strings"
@@ -17,14 +17,55 @@ const (
func init() {
lib.RegisterInputConfigCreator(TypeStdin, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
- return newStdin(action, data)
+ return NewStdinFromBytes(action, data)
})
- lib.RegisterInputConverter(TypeStdin, &Stdin{
+ lib.RegisterInputConverter(TypeStdin, &stdin{
Description: DescStdin,
})
}
-func newStdin(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
+type stdin struct {
+ Type string
+ Action lib.Action
+ Description string
+ Name string
+ OnlyIPType lib.IPType
+}
+
+func NewStdin(action lib.Action, opts ...lib.InputOption) lib.InputConverter {
+ s := &stdin{
+ Type: TypeStdin,
+ Action: action,
+ Description: DescStdin,
+ }
+
+ for _, opt := range opts {
+ if opt != nil {
+ opt(s)
+ }
+ }
+
+ return s
+}
+
+func WithStdinName(name string) lib.InputOption {
+ return func(s lib.InputConverter) {
+ name = strings.TrimSpace(name)
+ if name == "" {
+ log.Fatalf("❌ [type %s | action %s] missing name", TypeStdin, s.(*stdin).Action)
+ }
+
+ s.(*stdin).Name = name
+ }
+}
+
+func WithStdinOnlyIPType(onlyIPType lib.IPType) lib.InputOption {
+ return func(s lib.InputConverter) {
+ s.(*stdin).OnlyIPType = onlyIPType
+ }
+}
+
+func NewStdinFromBytes(action lib.Action, data []byte) (lib.InputConverter, error) {
var tmp struct {
Name string `json:"name"`
OnlyIPType lib.IPType `json:"onlyIPType"`
@@ -36,40 +77,26 @@ func newStdin(action lib.Action, data json.RawMessage) (lib.InputConverter, erro
}
}
- if tmp.Name == "" {
- return nil, fmt.Errorf("❌ [type %s | action %s] missing name", TypeStdin, action)
- }
-
- return &Stdin{
- Type: TypeStdin,
- Action: action,
- Description: DescStdin,
- Name: tmp.Name,
- OnlyIPType: tmp.OnlyIPType,
- }, nil
-}
-
-type Stdin struct {
- Type string
- Action lib.Action
- Description string
- Name string
- OnlyIPType lib.IPType
+ return NewStdin(
+ action,
+ WithStdinName(tmp.Name),
+ WithStdinOnlyIPType(tmp.OnlyIPType),
+ ), nil
}
-func (s *Stdin) GetType() string {
+func (s *stdin) GetType() string {
return s.Type
}
-func (s *Stdin) GetAction() lib.Action {
+func (s *stdin) GetAction() lib.Action {
return s.Action
}
-func (s *Stdin) GetDescription() string {
+func (s *stdin) GetDescription() string {
return s.Description
}
-func (s *Stdin) Input(container lib.Container) (lib.Container, error) {
+func (s *stdin) Input(container lib.Container) (lib.Container, error) {
entry := lib.NewEntry(s.Name)
scanner := bufio.NewScanner(os.Stdin)