Skip to content

Commit 2e07b36

Browse files
Merge pull request #33 from open-olive/SIDE-1036/File-System
Side 1036/file system
2 parents 872e860 + 09d6074 commit 2e07b36

File tree

96 files changed

+28837
-1963
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+28837
-1963
lines changed

ldk/csharp/OliveHelpsLDK/OliveHelpsLDK/Filesystem/FilesystemClient.cs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,6 @@ public Task<IList<FileInfo>> QueryDirectory(string directoryPath,
3636
.ContinueWith(continuationFunction, cancellationToken);
3737
}
3838

39-
public Task<FileInfo> QueryFile(string filePath, CancellationToken cancellationToken = default)
40-
{
41-
var request = new FilesystemFileRequest
42-
{
43-
Session = CreateSession(),
44-
Path = filePath,
45-
};
46-
var continuationFunction =
47-
LoggedParser<Task<FilesystemFileResponse>, FileInfo>(task => ConvertProtoFileInfo(task.Result.File));
48-
return Client.FilesystemFileAsync(request, CreateOptions(cancellationToken))
49-
.ResponseAsync
50-
.ContinueWith(continuationFunction, cancellationToken);
51-
}
52-
5339
public IStreamingCall<FileEvent> StreamDirectory(string directoryPath,
5440
CancellationToken cancellationToken = default)
5541
{
@@ -63,16 +49,16 @@ public IStreamingCall<FileEvent> StreamDirectory(string directoryPath,
6349
LoggedParser<FilesystemDirStreamResponse, FileEvent>(ConvertFileEvent));
6450
}
6551

66-
public IStreamingCall<FileEvent> StreamFile(string filePath, CancellationToken cancellationToken = default)
52+
public IStreamingCall<FileEvent> StreamFileInfo(string filePath, CancellationToken cancellationToken = default)
6753
{
68-
var request = new FilesystemFileStreamRequest
54+
var request = new FilesystemFileInfoStreamRequest
6955
{
7056
Session = CreateSession(),
7157
Path = filePath
7258
};
73-
var call = Client.FilesystemFileStream(request, CreateOptions(cancellationToken));
74-
return new StreamingCall<FilesystemFileStreamResponse, FileEvent>(call,
75-
LoggedParser<FilesystemFileStreamResponse, FileEvent>(ConvertFileEvent));
59+
var call = Client.FilesystemFileInfoStream(request, CreateOptions(cancellationToken));
60+
return new StreamingCall<FilesystemFileInfoStreamResponse, FileEvent>(call,
61+
LoggedParser<FilesystemFileInfoStreamResponse, FileEvent>(ConvertFileEvent));
7662
}
7763

7864
private static IList<FileInfo> ConvertFileList(IEnumerable<Proto.FileInfo> files)
@@ -101,7 +87,7 @@ private static FileEvent ConvertFileEvent(FilesystemDirStreamResponse dirStreamR
10187
};
10288
}
10389

104-
private static FileEvent ConvertFileEvent(FilesystemFileStreamResponse fileStreamResponse)
90+
private static FileEvent ConvertFileEvent(FilesystemFileInfoStreamResponse fileStreamResponse)
10591
{
10692
return new FileEvent
10793
{

ldk/csharp/OliveHelpsLDK/OliveHelpsLDK/Filesystem/IFilesystemService.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ namespace OliveHelpsLDK.Filesystem
88
public interface IFilesystemService
99
{
1010
Task<IList<FileInfo>> QueryDirectory(string directoryPath, CancellationToken cancellationToken = default);
11-
Task<FileInfo> QueryFile(string filePath, CancellationToken cancellationToken = default);
1211
IStreamingCall<FileEvent> StreamDirectory(string directoryPath, CancellationToken cancellationToken = default);
13-
IStreamingCall<FileEvent> StreamFile(string filePath, CancellationToken cancellationToken = default);
12+
IStreamingCall<FileEvent> StreamFileInfo(string filePath, CancellationToken cancellationToken = default);
1413
}
1514

1615
public struct FileInfo

ldk/go/cursorClient.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,23 @@ func (c *CursorClient) ListenPosition(ctx context.Context, handler ListenPositio
3636
return err
3737
}
3838

39-
go func() {
40-
for {
41-
resp, err := cursorReadStreamClient.Recv()
42-
if err == io.EOF {
43-
break
44-
}
45-
if err != nil {
46-
handler(CursorPosition{}, err)
47-
}
48-
if resp.GetError() != "" {
49-
err = errors.New(resp.GetError())
50-
}
51-
handler(CursorPosition{
52-
X: int(resp.X),
53-
Y: int(resp.Y),
54-
Screen: 0,
55-
}, err)
39+
for {
40+
resp, err := cursorReadStreamClient.Recv()
41+
if err == io.EOF {
42+
break
5643
}
57-
}()
44+
if err != nil {
45+
handler(CursorPosition{}, err)
46+
}
47+
if resp.GetError() != "" {
48+
err = errors.New(resp.GetError())
49+
}
50+
handler(CursorPosition{
51+
X: int(resp.X),
52+
Y: int(resp.Y),
53+
Screen: 0,
54+
}, err)
55+
}
5856

5957
return nil
6058
}

ldk/go/examples/filesystem-directory/loop/loop.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (c *Loop) LoopStop() error {
8383
return nil
8484
}
8585

86-
func (c *Loop) emitExampleWhisper(f ldk.FileInfo) error {
86+
func (c *Loop) emitExampleWhisper(f os.FileInfo) error {
8787
type template struct {
8888
Name string
8989
Size string
@@ -93,20 +93,20 @@ func (c *Loop) emitExampleWhisper(f ldk.FileInfo) error {
9393
}
9494

9595
var t template
96-
if f.IsDir {
96+
if f.IsDir() {
9797
t = template{
98-
Name: f.Name,
99-
Mode: os.FileMode(f.Mode).String(),
100-
Updated: f.Updated.Format(time.Stamp),
101-
IsDir: fmt.Sprintf("%t", f.IsDir),
98+
Name: f.Name(),
99+
Mode: f.Mode().String(),
100+
Updated: f.ModTime().Format(time.Stamp),
101+
IsDir: fmt.Sprintf("%t", f.IsDir()),
102102
}
103103
} else {
104104
t = template{
105-
Name: f.Name,
106-
Size: humanize.Bytes(uint64(f.Size)),
107-
Mode: os.FileMode(f.Mode).String(),
108-
Updated: f.Updated.Format(time.Stamp),
109-
IsDir: fmt.Sprintf("%t", f.IsDir),
105+
Name: f.Name(),
106+
Size: humanize.Bytes(uint64(f.Size())),
107+
Mode: f.Mode().String(),
108+
Updated: f.ModTime().Format(time.Stamp),
109+
IsDir: fmt.Sprintf("%t", f.IsDir()),
110110
}
111111
}
112112

@@ -126,7 +126,7 @@ func (c *Loop) emitExampleWhisper(f ldk.FileInfo) error {
126126
}
127127
}()
128128

129-
c.logger.Info("Sent message", "markdown", markdownBytes.String())
129+
// c.logger.Info("Sent message", "markdown", markdownBytes.String())
130130

131131
return nil
132132
}

ldk/go/examples/filesystem-directory/loop/loop_test.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,24 @@ import (
1313
)
1414

1515
func TestController(t *testing.T) {
16+
markdownDoneChan := make(chan bool)
17+
18+
type template struct {
19+
Name string
20+
Size int
21+
Mode int
22+
Updated time.Time
23+
IsDir bool
24+
}
25+
1626
sidekick := &ldktest.Sidekick{
27+
1728
FilesystemService: &ldktest.FilesystemService{
18-
Dirf: func(ctx context.Context, dir string) ([]ldk.FileInfo, error) {
19-
fi := []ldk.FileInfo{
20-
{
21-
Name: "foo.md",
22-
Size: 1024,
23-
Mode: int(os.ModePerm),
24-
Updated: time.Date(2020, 10, 1, 2, 34, 0, 0, time.UTC),
25-
IsDir: false,
26-
},
27-
}
28-
return fi, nil
29+
Dirf: func(ctx context.Context, dir string) ([]os.FileInfo, error) {
30+
fi := ldk.NewFileInfo("foo.md", int(os.ModePerm), 1024, time.Date(2020, 10, 1, 2, 34, 0, 0, time.UTC), false)
31+
32+
fiArray := []os.FileInfo{&fi}
33+
return fiArray, nil
2934
},
3035
},
3136
WhisperService: &ldktest.WhisperService{
@@ -34,6 +39,7 @@ func TestController(t *testing.T) {
3439
if got := w.Markdown; !cmp.Equal(got, exp) {
3540
t.Errorf("unexpected markdown:\n%s\n", cmp.Diff(got, exp))
3641
}
42+
markdownDoneChan <- true
3743
return nil
3844
},
3945
},
@@ -46,6 +52,7 @@ func TestController(t *testing.T) {
4652
if err := c.LoopStart(sidekick); err != nil {
4753
t.Fatal(err)
4854
}
55+
<-markdownDoneChan
4956
if err := c.LoopStop(); err != nil {
5057
t.Fatal(err)
5158
}
Lines changed: 24 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
package loop
22

33
import (
4-
"bytes"
54
"context"
6-
"fmt"
75
"html/template"
8-
"os"
9-
"time"
6+
"io"
107

11-
"github.com/dustin/go-humanize"
128
ldk "github.com/open-olive/loop-development-kit/ldk/go"
139
"github.com/open-olive/loop-development-kit/ldk/go/examples/filesystem-file/bind"
1410
)
@@ -61,66 +57,37 @@ func (c *Loop) LoopStart(sidekick ldk.Sidekick) error {
6157

6258
c.sidekick = sidekick
6359

64-
fi, err := sidekick.Filesystem().File(c.ctx, "./go.mod")
65-
if err != nil {
66-
c.logger.Error("error reading file info", "error", err)
67-
return err
68-
}
69-
return c.emitExampleWhisper(fi)
70-
}
60+
func() {
7161

72-
// LoopStop is called by the host when the plugin is stopped
73-
func (c *Loop) LoopStop() error {
74-
c.logger.Info("controller LoopStop called")
75-
c.cancel()
76-
77-
return nil
78-
}
79-
80-
func (c *Loop) emitExampleWhisper(f ldk.FileInfo) error {
81-
type template struct {
82-
Name string
83-
Size string
84-
Mode string
85-
Updated string
86-
IsDir string
87-
}
88-
89-
var t template
90-
if f.IsDir {
91-
t = template{
92-
Name: f.Name,
93-
Mode: os.FileMode(f.Mode).String(),
94-
Updated: f.Updated.Format(time.Stamp),
95-
IsDir: fmt.Sprintf("%t", f.IsDir),
96-
}
97-
} else {
98-
t = template{
99-
Name: f.Name,
100-
Size: humanize.Bytes(uint64(f.Size)),
101-
Mode: os.FileMode(f.Mode).String(),
102-
Updated: f.Updated.Format(time.Stamp),
103-
IsDir: fmt.Sprintf("%t", f.IsDir),
62+
file, err := sidekick.Filesystem().Open(c.ctx, "./go.sum")
63+
if err != nil {
64+
c.logger.Error("error reading file info", "error", err)
65+
return
10466
}
105-
}
67+
defer file.Close()
10668

107-
var markdownBytes bytes.Buffer
108-
if err := c.whisperTemplate.Execute(&markdownBytes, t); err != nil {
109-
c.logger.Error("failed to create markdown", "error", err)
110-
return err
111-
}
69+
logfile, err := sidekick.Filesystem().Create(c.ctx, "./read.txt")
70+
if err != nil {
71+
c.logger.Error("error creating file info", "error", err)
72+
return
73+
}
74+
defer logfile.Close()
11275

113-
go func() {
114-
err := c.sidekick.Whisper().Markdown(c.ctx, &ldk.WhisperContentMarkdown{
115-
Label: "Example Controller Go",
116-
Markdown: markdownBytes.String(),
117-
})
76+
_, err = io.Copy(logfile, file)
11877
if err != nil {
119-
c.logger.Error("failed to emit whisper", "error", err)
78+
c.logger.Error("error copying file", "error", err)
79+
return
12080
}
81+
12182
}()
12283

123-
c.logger.Info("Sent message", "markdown", markdownBytes.String())
84+
return nil
85+
}
86+
87+
// LoopStop is called by the host when the plugin is stopped
88+
func (c *Loop) LoopStop() error {
89+
c.logger.Info("controller LoopStop called")
90+
c.cancel()
12491

12592
return nil
12693
}

ldk/go/examples/filesystem-file/loop/loop_test.go

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@ package loop_test
22

33
import (
44
"context"
5-
"os"
65
"testing"
7-
"time"
86

9-
"github.com/google/go-cmp/cmp"
107
ldk "github.com/open-olive/loop-development-kit/ldk/go"
118
"github.com/open-olive/loop-development-kit/ldk/go/examples/filesystem-file/loop"
129
ldktest "github.com/open-olive/loop-development-kit/ldk/go/ldk-test"
@@ -15,23 +12,15 @@ import (
1512
func TestController(t *testing.T) {
1613
sidekick := &ldktest.Sidekick{
1714
FilesystemService: &ldktest.FilesystemService{
18-
Filef: func(ctx context.Context, dir string) (ldk.FileInfo, error) {
19-
return ldk.FileInfo{
20-
Name: "foo.md",
21-
Size: 1024,
22-
Mode: int(os.ModePerm),
23-
Updated: time.Date(2020, 10, 1, 2, 34, 0, 0, time.UTC),
24-
IsDir: false,
25-
}, nil
15+
Openf: func(ctx context.Context, dir string) (ldk.File, error) {
16+
readBytes := []byte("Hello World")
17+
mockFile := ldktest.CreateMockFile(readBytes)
18+
return &mockFile, nil
2619
},
27-
},
28-
WhisperService: &ldktest.WhisperService{
29-
Markdownf: func(ctx context.Context, w *ldk.WhisperContentMarkdown) error {
30-
exp := "# New File Event\n```\nfoo.md\n1.0 kB\n-rwxrwxrwx\nOct 1 02:34:00\nfalse\n```\n\n"
31-
if got := w.Markdown; !cmp.Equal(got, exp) {
32-
t.Errorf("unexpected markdown:\n%s\n", cmp.Diff(got, exp))
33-
}
34-
return nil
20+
Createf: func(ctx context.Context, dir string) (ldk.File, error) {
21+
readBytes := []byte("Hello World")
22+
mockFile := ldktest.CreateMockFile(readBytes)
23+
return &mockFile, nil
3524
},
3625
},
3726
}

ldk/go/examples/filesystem-listen-directory/loop/loop.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,21 @@ func (c *Loop) emitExampleWhisper(fe ldk.FileEvent) error {
9393
}
9494

9595
var t template
96-
if fe.Info.IsDir {
96+
if fe.Info.IsDir() {
9797
t = template{
98-
Name: fe.Info.Name,
99-
Mode: os.FileMode(fe.Info.Mode).String(),
100-
Updated: fe.Info.Updated.Format(time.Stamp),
101-
IsDir: fmt.Sprintf("%t", fe.Info.IsDir),
98+
Name: fe.Info.Name(),
99+
Mode: fe.Info.Mode().String(),
100+
Updated: fe.Info.ModTime().Format(time.Stamp),
101+
IsDir: fmt.Sprintf("%t", fe.Info.IsDir()),
102102
Action: fe.Action.String(),
103103
}
104104
} else {
105105
t = template{
106-
Name: fe.Info.Name,
107-
Size: humanize.Bytes(uint64(fe.Info.Size)),
108-
Mode: os.FileMode(fe.Info.Mode).String(),
109-
Updated: fe.Info.Updated.Format(time.Stamp),
110-
IsDir: fmt.Sprintf("%t", fe.Info.IsDir),
106+
Name: fe.Info.Name(),
107+
Size: humanize.Bytes(uint64(fe.Info.Size())),
108+
Mode: os.FileMode(fe.Info.Mode()).String(),
109+
Updated: fe.Info.ModTime().Format(time.Stamp),
110+
IsDir: fmt.Sprintf("%t", fe.Info.IsDir()),
111111
Action: fe.Action.String(),
112112
}
113113
}

0 commit comments

Comments
 (0)