-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
598 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package plugin | ||
package plugininterface | ||
|
||
import "github.com/Masterminds/semver/v3" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package json | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
type testFS struct { | ||
FS fs.FS | ||
path string | ||
} | ||
|
||
func makeTestFS(tb testing.TB) testFS { | ||
tb.Helper() | ||
|
||
path, err := filepath.EvalSymlinks(tb.TempDir()) | ||
if err != nil { | ||
tb.Fatalf("failed to create testFS: %s", err) | ||
} | ||
|
||
path = filepath.ToSlash(path) | ||
|
||
tb.Logf("creating testFS at %s", path) | ||
return testFS{ | ||
FS: os.DirFS(path), | ||
path: path, | ||
} | ||
} | ||
|
||
func (t testFS) Open(name string) (fs.File, error) { | ||
return t.FS.Open(filepath.ToSlash(name)) | ||
} | ||
|
||
func (t testFS) Path() string { | ||
return t.path | ||
} | ||
|
||
func (t testFS) WriteFile(name string, data []byte, perm os.FileMode) error { | ||
name = filepath.ToSlash(name) | ||
if filepath.IsAbs(name) { | ||
if strings.HasPrefix(name, t.path) { | ||
return os.WriteFile(name, data, perm) | ||
} | ||
return fmt.Errorf("path is outside test fs root folder") | ||
} | ||
return os.WriteFile(filepath.ToSlash(filepath.Join(t.path, name)), data, perm) | ||
} | ||
|
||
func (t testFS) MkdirAll(path string, perm os.FileMode) error { | ||
path = filepath.ToSlash(path) | ||
if filepath.IsAbs(path) { | ||
if strings.HasPrefix(path, t.path) { | ||
return os.MkdirAll(path, perm) | ||
} | ||
return fmt.Errorf("path is outside test fs root folder") | ||
} | ||
return os.MkdirAll(filepath.ToSlash(filepath.Join(t.path, path)), perm) | ||
} | ||
|
||
func testJSON(m any) json.RawMessage { | ||
contents, err := json.Marshal(m) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return contents | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package json | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/Masterminds/semver/v3" | ||
"github.com/blackstork-io/fabric/plugininterface/v1" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hcldec" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
var Version = semver.MustParse("0.1.0") | ||
|
||
type Plugin struct{} | ||
|
||
func (Plugin) GetPlugins() []plugininterface.Plugin { | ||
return []plugininterface.Plugin{ | ||
{ | ||
Namespace: "blackstork", | ||
Kind: "data", | ||
Name: "json", | ||
Version: plugininterface.Version(*Version), | ||
ConfigSpec: nil, | ||
InvocationSpec: &hcldec.ObjectSpec{ | ||
"glob": &hcldec.AttrSpec{ | ||
Name: "glob", | ||
Type: cty.String, | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (Plugin) Call(args plugininterface.Args) plugininterface.Result { | ||
glob := args.Args.GetAttr("glob").AsString() | ||
wd, err := os.Getwd() | ||
if err != nil { | ||
return plugininterface.Result{ | ||
Diags: hcl.Diagnostics{{ | ||
Severity: hcl.DiagError, | ||
Summary: "Failed to get current working directory", | ||
Detail: err.Error(), | ||
}}, | ||
} | ||
} | ||
filesystem := os.DirFS(wd) | ||
docs, err := readFS(filesystem, glob) | ||
if err != nil { | ||
return plugininterface.Result{ | ||
Diags: hcl.Diagnostics{{ | ||
Severity: hcl.DiagError, | ||
Summary: "Failed to read json files", | ||
Detail: err.Error(), | ||
}}, | ||
} | ||
} | ||
data := make([]any, len(docs)) | ||
for i, doc := range docs { | ||
data[i] = doc.Map() | ||
} | ||
return plugininterface.Result{ | ||
Result: data, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package json | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/blackstork-io/fabric/plugininterface/v1" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
func TestPlugin_GetPlugins(t *testing.T) { | ||
plugin := Plugin{} | ||
plugins := plugin.GetPlugins() | ||
require.Len(t, plugins, 1, "expected 1 plugin") | ||
got := plugins[0] | ||
assert.Equal(t, "json", got.Name) | ||
assert.Equal(t, "data", got.Kind) | ||
assert.Equal(t, "blackstork", got.Namespace) | ||
assert.Equal(t, Version.String(), got.Version.Cast().String()) | ||
assert.Nil(t, got.ConfigSpec) | ||
assert.NotNil(t, got.InvocationSpec) | ||
} | ||
|
||
func TestPlugin_Call(t *testing.T) { | ||
tt := []struct { | ||
name string | ||
glob string | ||
expected plugininterface.Result | ||
}{ | ||
{ | ||
name: "empty_list", | ||
glob: "unknown_dir/*.json", | ||
expected: plugininterface.Result{ | ||
Result: []any{}, | ||
}, | ||
}, | ||
{ | ||
name: "one_file", | ||
glob: "testdata/a.json", | ||
expected: plugininterface.Result{ | ||
Result: []any{ | ||
map[string]any{ | ||
"filename": "testdata/a.json", | ||
"contents": map[string]any{ | ||
"property_for": "a.json", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "dir", | ||
glob: "testdata/dir/*.json", | ||
expected: plugininterface.Result{ | ||
Result: []any{ | ||
map[string]any{ | ||
"filename": "testdata/dir/b.json", | ||
"contents": []any{ | ||
map[string]any{ | ||
"id": float64(1), | ||
"property_for": "dir/b.json", | ||
}, | ||
map[string]any{ | ||
"id": float64(2), | ||
"property_for": "dir/b.json", | ||
}, | ||
}, | ||
}, | ||
map[string]any{ | ||
"filename": "testdata/dir/c.json", | ||
"contents": []any{ | ||
map[string]any{ | ||
"id": float64(3), | ||
"property_for": "dir/c.json", | ||
}, | ||
map[string]any{ | ||
"id": float64(4), | ||
"property_for": "dir/c.json", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range tt { | ||
t.Run(tc.name, func(t *testing.T) { | ||
plugin := Plugin{} | ||
args := plugininterface.Args{ | ||
Kind: "json", | ||
Name: "json", | ||
Args: cty.ObjectVal(map[string]cty.Value{ | ||
"glob": cty.StringVal(tc.glob), | ||
}), | ||
} | ||
got := plugin.Call(args) | ||
assert.Equal(t, tc.expected, got) | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package json | ||
|
||
import ( | ||
"encoding/json" | ||
"io/fs" | ||
) | ||
|
||
// JSONDocument represents a JSON document that was read from the filesystem | ||
type JSONDocument struct { | ||
Filename string `json:"filename"` | ||
Contents json.RawMessage `json:"contents"` | ||
} | ||
|
||
func (doc JSONDocument) Map() map[string]any { | ||
var result any | ||
_ = json.Unmarshal(doc.Contents, &result) | ||
return map[string]any{ | ||
"filename": doc.Filename, | ||
"contents": result, | ||
} | ||
} | ||
|
||
// readFS reads all JSON documents from the filesystem that match the given glob pattern | ||
// The pattern is relative to the root of the filesystem | ||
func readFS(filesystem fs.FS, pattern string) ([]JSONDocument, error) { | ||
matchers, err := fs.Glob(filesystem, pattern) | ||
if err != nil { | ||
return nil, err | ||
} | ||
result := []JSONDocument{} | ||
for _, matcher := range matchers { | ||
file, err := filesystem.Open(matcher) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var contents json.RawMessage | ||
err = json.NewDecoder(file).Decode(&contents) | ||
if err != nil { | ||
return nil, err | ||
} | ||
result = append(result, JSONDocument{ | ||
Filename: matcher, | ||
Contents: contents, | ||
}) | ||
} | ||
return result, nil | ||
} |
Oops, something went wrong.