Files
2026-07-28 13:21:39 +08:00

78 lines
2.3 KiB
Go

//go:build fakeplugin
package main
import (
"context"
"fmt"
"log/slog"
"os"
githubplugin "github.com/hashicorp/go-plugin"
"github.com/certimate-go/certimate/pkg/plugin"
)
type fakeDeployer struct {
behavior string
}
func (f *fakeDeployer) GetMetadata(ctx context.Context) (*plugin.Metadata, error) {
accessType := os.Getenv("FAKEPLUGIN_ACCESS_TYPE")
if accessType == "" {
accessType = os.Getenv("FAKEPLUGIN_PROVIDER_TYPE")
}
return &plugin.Metadata{
ProviderType: os.Getenv("FAKEPLUGIN_PROVIDER_TYPE"),
AccessProviderType: accessType,
ProtocolVersion: plugin.ProtocolVersion,
DeployCategory: "other",
DeployDisplayNameKey: "plugin.fake.name",
AccessDisplayNameKey: "plugin.fake.name",
}, nil
}
func (f *fakeDeployer) GetConfigSchema(ctx context.Context) (*plugin.ConfigSchema, error) {
// NOTE: Real plugins use //go:embed to bundle schema JSON files at compile time.
// This fakeplugin uses inline JSON for testing simplicity.
// See plugins/webhook-deployer/embed.go for the canonical pattern.
return &plugin.ConfigSchema{
AccessSchemaJSON: []byte(`{"schemaVersion":"form/v1","provider":"fake","category":"access"}`),
DeploySchemaJSON: []byte(`{"schemaVersion":"form/v1","provider":"fake","category":"deploy"}`),
I18n: map[string]map[string]string{
"en": {"plugin.fake.name": "Fake Plugin"},
},
}, nil
}
func (f *fakeDeployer) Deploy(ctx context.Context, req *plugin.DeployRequest, logger *slog.Logger) (*plugin.DeployResult, error) {
fmt.Fprintf(os.Stderr, "fakeplugin deploy behavior=%s access=%s\n", f.behavior, req.AccessConfigJSON)
if logger != nil {
logger.Info("fakeplugin deploy starting", slog.String("behavior", f.behavior))
}
switch f.behavior {
case "crash":
fmt.Fprintln(os.Stderr, "fakeplugin crashing now")
os.Exit(2)
case "configerror":
return nil, fmt.Errorf("bad config: missing field")
}
return &plugin.DeployResult{ExtendedDataJSON: `{"deployed":true}`}, nil
}
func main() {
behavior := os.Getenv("FAKEPLUGIN_BEHAVIOR")
if behavior == "" {
behavior = "ok"
}
impl := &fakeDeployer{behavior: behavior}
githubplugin.Serve(&githubplugin.ServeConfig{
HandshakeConfig: plugin.HandshakeConfig,
Plugins: map[string]githubplugin.Plugin{
plugin.PluginName: &plugin.DeployerGRPCPlugin{Impl: impl},
},
GRPCServer: githubplugin.DefaultGRPCServer,
})
}