Skip to content

Commit

Permalink
feat: Add option WithNoExec() to skip executing of plugins (#103)
Browse files Browse the repository at this point in the history
Co-authored-by: Kemal Hadimli <disq@users.noreply.github.com>
  • Loading branch information
disq and disq authored Sep 19, 2023
1 parent 6bbb806 commit 0a7a456
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 30 deletions.
6 changes: 6 additions & 0 deletions managedplugin/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ func WithNoSentry() Option {
}
}

func WithNoExec() Option {
return func(c *Client) {
c.noExec = true
}
}

func WithOtelEndpoint(endpoint string) Option {
return func(c *Client) {
c.otelEndpoint = endpoint
Expand Down
72 changes: 42 additions & 30 deletions managedplugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type Client struct {
Conn *grpc.ClientConn
config Config
noSentry bool
noExec bool
otelEndpoint string
otelEndpointInsecure bool
metrics *Metrics
Expand Down Expand Up @@ -128,57 +129,68 @@ func (c Clients) Terminate() error {
// If registrySpec is gRPC then clients creates a new connection
// If registrySpec is Docker then client downloads the docker image, runs it and creates a gRPC connection.
func NewClient(ctx context.Context, typ PluginType, config Config, opts ...Option) (*Client, error) {
c := Client{
c := &Client{
directory: defaultDownloadDir,
wg: &sync.WaitGroup{},
config: config,
metrics: &Metrics{},
registry: config.Registry,
}
for _, opt := range opts {
opt(&c)
opt(c)
}
switch config.Registry {
case RegistryGrpc:
err := c.connectUsingTCP(ctx, config.Path)
if err != nil {
if err := c.downloadPlugin(ctx, typ); err != nil {
return nil, err
}
if !c.noExec {
if err := c.execPlugin(ctx); err != nil {
return nil, err
}
}

return c, nil
}

func (c *Client) downloadPlugin(ctx context.Context, typ PluginType) error {
switch c.config.Registry {
case RegistryGrpc:
return nil // GRPC plugins are not downloaded
case RegistryLocal:
if err := validateLocalExecPath(config.Path); err != nil {
return nil, err
}
if err := c.startLocal(ctx, config.Path); err != nil {
return nil, err
}
return validateLocalExecPath(c.config.Path)
case RegistryGithub:
pathSplit := strings.Split(config.Path, "/")
pathSplit := strings.Split(c.config.Path, "/")
if len(pathSplit) != 2 {
return nil, fmt.Errorf("invalid github plugin path: %s. format should be owner/repo", config.Path)
return fmt.Errorf("invalid github plugin path: %s. format should be owner/repo", c.config.Path)
}
org, name := pathSplit[0], pathSplit[1]
c.LocalPath = filepath.Join(c.directory, "plugins", typ.String(), org, name, config.Version, "plugin")
c.LocalPath = filepath.Join(c.directory, "plugins", typ.String(), org, name, c.config.Version, "plugin")
c.LocalPath = WithBinarySuffix(c.LocalPath)
if err := DownloadPluginFromGithub(ctx, c.LocalPath, org, name, config.Version, typ); err != nil {
return nil, err
}
if err := c.startLocal(ctx, c.LocalPath); err != nil {
return nil, err
}
return DownloadPluginFromGithub(ctx, c.LocalPath, org, name, c.config.Version, typ)
case RegistryDocker:
if imageAvailable, err := isDockerImageAvailable(ctx, config.Path); err != nil {
return nil, err
if imageAvailable, err := isDockerImageAvailable(ctx, c.config.Path); err != nil {
return err
} else if !imageAvailable {
if err := pullDockerImage(ctx, config.Path); err != nil {
return nil, err
}
}
if err := c.startDockerPlugin(ctx, config.Path); err != nil {
return nil, err
return pullDockerImage(ctx, c.config.Path)
}
return nil
default:
return fmt.Errorf("unknown registry %s", c.config.Registry.String())
}
}

return &c, nil
func (c *Client) execPlugin(ctx context.Context) error {
switch c.config.Registry {
case RegistryGrpc:
return c.connectUsingTCP(ctx, c.config.Path)
case RegistryLocal:
return c.startLocal(ctx, c.config.Path)
case RegistryGithub:
return c.startLocal(ctx, c.LocalPath)
case RegistryDocker:
return c.startDockerPlugin(ctx, c.config.Path)
default:
return fmt.Errorf("unknown registry %s", c.config.Registry.String())
}
}

func (c *Client) ConnectionString() string {
Expand Down

0 comments on commit 0a7a456

Please sign in to comment.