Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit afa10cc

Browse files
authored
feat: Add local path validation (#60)
Help users running CQ with locally built plugins get better error messages
1 parent 180909f commit afa10cc

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

managedplugin/plugin.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ func NewClient(ctx context.Context, typ PluginType, config Config, opts ...Optio
146146
return nil, fmt.Errorf("failed to dial grpc source plugin at %s: %w", config.Path, err)
147147
}
148148
case RegistryLocal:
149+
if err := validateLocalExecPath(config.Path); err != nil {
150+
return nil, err
151+
}
149152
if err := c.startLocal(ctx, config.Path); err != nil {
150153
return nil, err
151154
}
@@ -367,3 +370,22 @@ func (c *Client) Terminate() error {
367370

368371
return nil
369372
}
373+
374+
func isDirectory(path string) (bool, error) {
375+
fileInfo, err := os.Stat(path)
376+
if err != nil {
377+
return false, err
378+
}
379+
return fileInfo.IsDir(), err
380+
}
381+
382+
func validateLocalExecPath(filePath string) error {
383+
directory, err := isDirectory(filePath)
384+
if err != nil {
385+
return fmt.Errorf("error validating plugin path, %s: %w", filePath, err)
386+
}
387+
if directory {
388+
return fmt.Errorf("invalid plugin path: %s. Path cannot point to a directory", filePath)
389+
}
390+
return nil
391+
}

managedplugin/plugin_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package managedplugin
22

33
import (
44
"context"
5+
"os"
56
"path/filepath"
67
"testing"
78
)
@@ -47,3 +48,52 @@ func TestManagedPluginGitHub(t *testing.T) {
4748
t.Fatal(err)
4849
}
4950
}
51+
52+
func TestIsDirectory(t *testing.T) {
53+
tempDir := t.TempDir()
54+
directoryBool, err := isDirectory(tempDir)
55+
if err != nil {
56+
t.Fatal(err)
57+
}
58+
// directory should be `true`
59+
if !directoryBool {
60+
t.Fatal("expected directory")
61+
}
62+
tempFile, err := os.Create(tempDir + "testFile")
63+
if err != nil {
64+
t.Fatal(err)
65+
}
66+
defer tempFile.Close()
67+
isFileBool, err := isDirectory(tempDir + "testFile")
68+
if err != nil {
69+
t.Fatal(err)
70+
}
71+
if isFileBool {
72+
t.Fatal("expected file")
73+
}
74+
}
75+
76+
func TestValidatevLocalExecPath(t *testing.T) {
77+
tempDir := t.TempDir()
78+
// passing a directory should result in an error
79+
err := validateLocalExecPath(tempDir)
80+
if err == nil {
81+
t.Fatal(err)
82+
}
83+
84+
tempFile, err := os.Create(tempDir + "testFile")
85+
if err != nil {
86+
t.Fatal(err)
87+
}
88+
defer tempFile.Close()
89+
err = validateLocalExecPath(tempDir + "testFile")
90+
// passing a valid non directory path should result in no error
91+
if err != nil {
92+
t.Fatal(err)
93+
}
94+
// passing a file path that doesn't exist should result in an error
95+
err = validateLocalExecPath(tempDir + "randomfile")
96+
if err == nil {
97+
t.Fatal(err)
98+
}
99+
}

0 commit comments

Comments
 (0)