diff options
Diffstat (limited to 'plugin/special/stdin.go')
| -rw-r--r-- | plugin/special/stdin.go | 76 |
1 files changed, 54 insertions, 22 deletions
diff --git a/plugin/special/stdin.go b/plugin/special/stdin.go index f2f9cf9c..f592560e 100644 --- a/plugin/special/stdin.go +++ b/plugin/special/stdin.go @@ -4,6 +4,7 @@ import ( "bufio" "encoding/json" "fmt" + "log" "os" "strings" @@ -17,14 +18,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"` @@ -40,36 +82,26 @@ func newStdin(action lib.Action, data json.RawMessage) (lib.InputConverter, erro 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) |
