{"id":15441888,"url":"https://github.com/mudler/go-pluggable","last_synced_at":"2025-04-19T18:40:47.249Z","repository":{"id":57555722,"uuid":"312633115","full_name":"mudler/go-pluggable","owner":"mudler","description":":bento: go-pluggable is a light Bus-event driven plugin library for Golang","archived":false,"fork":false,"pushed_at":"2023-01-26T22:06:32.000Z","size":72,"stargazers_count":8,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-05T16:32:05.991Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mudler.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-11-13T16:51:32.000Z","updated_at":"2024-02-19T08:08:03.000Z","dependencies_parsed_at":"2023-02-15T01:46:03.086Z","dependency_job_id":null,"html_url":"https://github.com/mudler/go-pluggable","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudler%2Fgo-pluggable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudler%2Fgo-pluggable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudler%2Fgo-pluggable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudler%2Fgo-pluggable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mudler","download_url":"https://codeload.github.com/mudler/go-pluggable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231260785,"owners_count":18349461,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-01T19:23:58.678Z","updated_at":"2024-12-26T19:10:56.159Z","avatar_url":"https://github.com/mudler.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# :bento: go-pluggable\n[![PkgGoDev](https://pkg.go.dev/badge/github.com/mudler/go-pluggable)](https://pkg.go.dev/github.com/mudler/go-pluggable) [![Go Report Card](https://goreportcard.com/badge/github.com/mudler/go-pluggable)](https://goreportcard.com/report/github.com/mudler/go-pluggable) [![Test](https://github.com/mudler/go-pluggable/workflows/Test/badge.svg)](https://github.com/mudler/go-pluggable/actions?query=workflow%3ATest)\n\n:bento: *go-pluggable* is a light Bus-event driven plugin library for Golang.\n\n`go-pluggable` implements the event/sub pattern to extend your Golang project with external binary plugins that can be written in any language.\n\n```golang\nimport \"github.com/mudler/go-pluggable\"\n\n\nfunc main() {\n\n    var myEv pluggableEventType = \"something.to.hook.on\"\n    temp := \"/usr/custom/bin\"\n\n    m = pluggable.NewManager(\n        []pluggable.EventType{\n            myEv,\n        },\n    )\n        \n    // Load plugins\n    m.Autoload(\"test\", temp) // Scan for binary plugins with the \"test\" prefix. E.g. 'test-foo'\n    m.Plugin = append(m.Plugin, pluggable.Plugin{ Name: \"foo\" , Executable: \"path\" }) // manually add a Plugin\n    m.Load(\"my-binary\", \"my-binary-2\"...) // Load individually, scanning $PATH\n\n    // Register to events and initialize the manager\n    m.Register()\n\n    // Optionally process plugin results response\n    // The plugins has to return as output a json in stdout in the format { 'state': \"somestate\", data: \"some data\", error: \"some error\" }\n    // e.g. with jq:  \n    // jq --arg key0   'state' \\\n    // --arg value0 '' \\\n    // --arg key1   'data' \\\n    // --arg value1 \"\" \\\n    // --arg key2   'error' \\\n    // --arg value2 '' \\\n    // '. | .[$key0]=$value0 | .[$key1]=$value1 | .[$key2]=$value2' \\\n    // \u003c\u003c\u003c'{}'\n    m.Response(myEv, func(p *pluggable.Plugin, r *pluggable.EventResponse) { ... }) \n\n    // Emit events, they are encoded and passed as JSON payloads to the plugins.\n    // In our case, test-foo will receive the map as JSON\n    m.Publish(myEv,  map[string]string{\"foo\": \"bar\"})\n}\n\n```\n\n# Plugin processed data\n\nThe interface passed to `Publish` gets marshalled in JSON in a event struct of the following form:\n\n```go\ntype Event struct {\n\tName EventType `json:\"name\"`\n\tData string    `json:\"data\"`\n\tFile string    `json:\"file\"`\n}\n```\n\n\nAn example bash plugin could be, for example:\n\n```bash\n#!/bin/bash\n\nevent=\"$1\"\npayload=\"$2\"\nif [ \"$event\" == \"something.to.hook.on\" ]; then\n  custom_data=$(echo \"$payload\" | jq -r .data | jq -r .foo )\n  ...\nfi\n```\n\nWhich can be called by \n```golang\nm.Publish(myEv,  map[string]string{\"foo\": \"bar\"})\n```\n\nTo note, when the payload exceeds the [threshold size](https://github.com/mudler/go-pluggable/blob/master/plugin.go#L35) the payload published with `Publish` is written into a temporary file and the file location is sent to the plugin with the Event `file` field, so for example, a plugin should expect data in a file if the publisher expects to send big chunk of data:\n\n```bash\n#!/bin/bash\ndata_file=\"$(echo $2 | jq -r .file)\"\nif [ -n \"${data_file}\" ]; then\n    payload=\"$(cat $data_file)\"\n\n...\nfi\n```\n\n## Writing plugin in golang\n\nIt is present a `FactoryPlugin` which allows to create plugins in golang, consider:\n\n```golang\nimport \"github.com/mudler/go-pluggable\"\n\nfunc main() {\n    var myEv pluggable.EventType = \"event\"\n\n    factory := pluggable.NewPluginFactory()\n    factory.Add(myEv, func(e *plugable.Event) pluggable.EventResponse { return EventResponse{ ... })\n\n    factory.Run(os.Args[1], os.Stdin, os.Stdout)\n}\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmudler%2Fgo-pluggable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmudler%2Fgo-pluggable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmudler%2Fgo-pluggable/lists"}