Skip to content

Commit

Permalink
feat(new): add console plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincenzo authored and Vincenzo committed Oct 13, 2024
1 parent a6b842b commit d3d13bf
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 8 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ else
endif

PLUGINS=mongodb \
console \
azblobstorage \
azcosmosdb \
luascript \
Expand Down
1 change: 1 addition & 0 deletions cmd/plugin/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ var rootCmd = &cobra.Command{
Use: "jrplugin",
Short: "jrplugin, a plugin for jr",
Long: "jrplugin is a plugin system for jr",
Run: run,
}
10 changes: 2 additions & 8 deletions cmd/plugin/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
_ "github.com/jrnd-io/jr-plugins/internal/plugin/azblobstorage"
_ "github.com/jrnd-io/jr-plugins/internal/plugin/azcosmosdb"
_ "github.com/jrnd-io/jr-plugins/internal/plugin/cassandra"
_ "github.com/jrnd-io/jr-plugins/internal/plugin/console"
_ "github.com/jrnd-io/jr-plugins/internal/plugin/elastic"
_ "github.com/jrnd-io/jr-plugins/internal/plugin/gcs"
_ "github.com/jrnd-io/jr-plugins/internal/plugin/http"
Expand All @@ -44,21 +45,14 @@ import (
)

var (
runCmd = &cobra.Command{
Use: "run",
Short: "Run plugin",
Long: "Run plugin",
Run: run,
}
cfgFile string
cfgBytes []byte
)

func init() {

cobra.OnInitialize(readConfig)
runCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
rootCmd.AddCommand(runCmd)
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file")
}

func readConfig() {
Expand Down
20 changes: 20 additions & 0 deletions internal/plugin/console/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright © 2024 JR team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package console
66 changes: 66 additions & 0 deletions internal/plugin/console/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//go:build plugin_console
// +build plugin_console

// Copyright © 2024 JR team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package console

import (
"context"
"fmt"

"github.com/jrnd-io/jr-plugins/internal/plugin"
"github.com/jrnd-io/jrv2/pkg/jrpc"
)

const (
Name = "console"
)

func init() {
plugin.RegisterPlugin(Name, &Plugin{})
}

type Plugin struct{}

func (p *Plugin) Init(_ context.Context, _ []byte) error {
// No initialization needed for console plugin
return nil
}

func (p *Plugin) Close(_ context.Context) error {
// No cleanup needed for console plugin
return nil
}

func (p *Plugin) Produce(k []byte, v []byte, headers map[string]string) (*jrpc.ProduceResponse, error) {
fmt.Printf("Key: %s\n", string(k))
fmt.Printf("Value: %s\n", string(v))
fmt.Println("Headers:")
for key, value := range headers {
fmt.Printf(" %s: %s\n", key, value)
}

return &jrpc.ProduceResponse{
Bytes: uint64(len(v)),
Message: "Printed to console",
}, nil
}

0 comments on commit d3d13bf

Please sign in to comment.