Skip to content

Commit

Permalink
Pluginapi nits (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-Morozko authored Feb 10, 2024
1 parent 56df6c5 commit e27f8a7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 53 deletions.
35 changes: 15 additions & 20 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
version: 1

project_name: fabric

env:
- CGO_ENABLED=0

builds:
# CLI

- id: fabric
main: .
binary: fabric
env:
- CGO_ENABLED=0
# Default: '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser'
flags: "-trimpath"
ldflags:
- -s -w -X github.com/blackstork-io/fabric/cmd.version={{.Version}}
- -s -w
- -X github.com/blackstork-io/fabric/cmd.version={{.Version}}
goos:
- linux
- windows
Expand All @@ -23,8 +26,7 @@ builds:
- id: elasticsearch
main: ./internal/elasticsearch/cmd
binary: "plugins/blackstork/elasticsearch@{{ .Version }}"
env:
- CGO_ENABLED=0
flags: "-trimpath"
goos:
- linux
- windows
Expand All @@ -33,8 +35,7 @@ builds:
- id: github
main: ./internal/github/cmd
binary: "plugins/blackstork/github@{{ .Version }}"
env:
- CGO_ENABLED=0
flags: "-trimpath"
goos:
- linux
- windows
Expand All @@ -43,8 +44,7 @@ builds:
- id: graphql
main: ./internal/graphql/cmd
binary: "plugins/blackstork/graphql@{{ .Version }}"
env:
- CGO_ENABLED=0
flags: "-trimpath"
goos:
- linux
- windows
Expand All @@ -53,8 +53,7 @@ builds:
- id: openai
main: ./internal/openai/cmd
binary: "plugins/blackstork/openai@{{ .Version }}"
env:
- CGO_ENABLED=0
flags: "-trimpath"
goos:
- linux
- windows
Expand All @@ -63,8 +62,7 @@ builds:
- id: opencti
main: ./internal/opencti/cmd
binary: "plugins/blackstork/opencti@{{ .Version }}"
env:
- CGO_ENABLED=0
flags: "-trimpath"
goos:
- linux
- windows
Expand All @@ -73,8 +71,7 @@ builds:
- id: postgresql
main: ./internal/postgresql/cmd
binary: "plugins/blackstork/postgresql@{{ .Version }}"
env:
- CGO_ENABLED=0
flags: "-trimpath"
goos:
- linux
- windows
Expand All @@ -83,8 +80,7 @@ builds:
- id: sqlite
main: ./internal/sqlite/cmd
binary: "plugins/blackstork/sqlite@{{ .Version }}"
env:
- CGO_ENABLED=0
flags: "-trimpath"
goos:
- linux
- windows
Expand All @@ -93,8 +89,7 @@ builds:
- id: terraform
main: ./internal/terraform/cmd
binary: "plugins/blackstork/terraform@{{ .Version }}"
env:
- CGO_ENABLED=0
flags: "-trimpath"
goos:
- linux
- windows
Expand Down
3 changes: 1 addition & 2 deletions plugin/content_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ func (cg *ContentProvider) Execute(ctx context.Context, params *ProvideContentPa
if cg == nil {
return nil, hcl.Diagnostics{{
Severity: hcl.DiagError,
Summary: "Incomplete ContentProviderSchema",
Detail: "Content provider not loaded",
Summary: "Missing ContentProvider schema",
}}
}
if cg.ContentFunc == nil {
Expand Down
3 changes: 1 addition & 2 deletions plugin/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ func (ds *DataSource) Execute(ctx context.Context, params *RetrieveDataParams) (
if ds == nil {
return nil, hcl.Diagnostics{{
Severity: hcl.DiagError,
Summary: "No schema",
Detail: "No schema defined",
Summary: "Missing DataSource schema",
}}
}
if ds.DataFunc == nil {
Expand Down
17 changes: 0 additions & 17 deletions plugin/diagnostics.go

This file was deleted.

37 changes: 25 additions & 12 deletions plugin/pluginapi/v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"log/slog"
"os/exec"
"path"
"path/filepath"
"strings"

goplugin "github.com/hashicorp/go-plugin"
Expand All @@ -13,27 +13,40 @@ import (
"github.com/blackstork-io/fabric/plugin"
)

func NewClient(loc string) (p *plugin.Schema, closefn func() error, err error) {
base := path.Base(loc)
if base == "" {
return nil, nil, fmt.Errorf("invalid plugin location: %s", loc)
}
parts := strings.SplitN(base, "@", 2)
func parsePluginInfo(path string) (name, version string, err error) {
nameVer := filepath.Base(path)
ext := filepath.Ext(path)

parts := strings.SplitN(
nameVer[:len(nameVer)-len(ext)],
"@", 2,
)
if len(parts) != 2 {
return nil, nil, fmt.Errorf("invalid plugin name: %s", base)
err = fmt.Errorf("plugin at '%s' must have a file name '<plugin_name>@<plugin_version>[.exe]'", path)
return
}
name = parts[0]
version = parts[1]
return
}

func NewClient(loc string) (p *plugin.Schema, closefn func() error, err error) {
pluginName, _, err := parsePluginInfo(loc)
if err != nil {
return
}
client := goplugin.NewClient(&goplugin.ClientConfig{
HandshakeConfig: handshake,
Plugins: map[string]goplugin.Plugin{
parts[0]: &grpcPlugin{},
pluginName: &grpcPlugin{},
},
Cmd: exec.Command("sh", "-c", loc),
Cmd: exec.Command(loc),
AllowedProtocols: []goplugin.Protocol{
goplugin.ProtocolGRPC,
},
Logger: sloghclog.Adapt(
slog.Default(),
sloghclog.Name("plugin."+parts[0]),
sloghclog.Name("plugin."+pluginName),
// disable code location reporting, it's always going to be incorrect
// for remote plugin logs
sloghclog.AddSource(false),
Expand All @@ -43,7 +56,7 @@ func NewClient(loc string) (p *plugin.Schema, closefn func() error, err error) {
if err != nil {
return nil, nil, fmt.Errorf("failed to create plugin client: %v", err)
}
raw, err := rpcClient.Dispense(parts[0])
raw, err := rpcClient.Dispense(pluginName)
if err != nil {
rpcClient.Close()
return nil, nil, fmt.Errorf("failed to dispense plugin: %v", err)
Expand Down

0 comments on commit e27f8a7

Please sign in to comment.