From e27f8a71aae04e08bb04296445f9b7fdec9ace48 Mon Sep 17 00:00:00 2001 From: Andrew Morozko Date: Sat, 10 Feb 2024 22:31:27 +0400 Subject: [PATCH] Pluginapi nits (#72) --- .goreleaser.yaml | 35 ++++++++++++++------------------- plugin/content_provider.go | 3 +-- plugin/data_source.go | 3 +-- plugin/diagnostics.go | 17 ---------------- plugin/pluginapi/v1/client.go | 37 +++++++++++++++++++++++------------ 5 files changed, 42 insertions(+), 53 deletions(-) delete mode 100644 plugin/diagnostics.go diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 3bb12f22..bcd19b62 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/plugin/content_provider.go b/plugin/content_provider.go index 7c3e427a..930fd7fc 100644 --- a/plugin/content_provider.go +++ b/plugin/content_provider.go @@ -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 { diff --git a/plugin/data_source.go b/plugin/data_source.go index eb54d654..34bc69c8 100644 --- a/plugin/data_source.go +++ b/plugin/data_source.go @@ -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 { diff --git a/plugin/diagnostics.go b/plugin/diagnostics.go deleted file mode 100644 index b5bd517a..00000000 --- a/plugin/diagnostics.go +++ /dev/null @@ -1,17 +0,0 @@ -package plugin - -type DiagnosticSeverity int - -const ( - DiagInvalid DiagnosticSeverity = iota - DiagError - DiagWarning -) - -type Diagnostic struct { - Severity DiagnosticSeverity - Summary string - Detail string -} - -type Diagnostics []*Diagnostic diff --git a/plugin/pluginapi/v1/client.go b/plugin/pluginapi/v1/client.go index e8930b27..b4db4635 100644 --- a/plugin/pluginapi/v1/client.go +++ b/plugin/pluginapi/v1/client.go @@ -4,7 +4,7 @@ import ( "fmt" "log/slog" "os/exec" - "path" + "path/filepath" "strings" goplugin "github.com/hashicorp/go-plugin" @@ -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 '@[.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), @@ -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)