Skip to content

Commit

Permalink
add 'drive' cli
Browse files Browse the repository at this point in the history
  • Loading branch information
seborama committed Apr 24, 2024
1 parent 6749248 commit fe88ba2
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 10 deletions.
8 changes: 4 additions & 4 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -59,7 +59,7 @@ func (cli *CLI) copyFromPCloudToLocal(ctx context.Context, from, to string) erro
resp, err := cli.httpClient.Do(req)
if resp != nil {
defer func() {
_, err = io.Copy(ioutil.Discard, resp.Body)
_, err = io.Copy(io.Discard, resp.Body)
if err != nil {
fmt.Println("discarding remainder of response body:", err.Error())
}
Expand All @@ -74,7 +74,7 @@ func (cli *CLI) copyFromPCloudToLocal(ctx context.Context, from, to string) erro
}

// TODO: optimise this to read in chunks
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, "reading body of the response to download data from pCloud")
}
Expand All @@ -83,7 +83,7 @@ func (cli *CLI) copyFromPCloudToLocal(ctx context.Context, from, to string) erro
return errors.New(string(body))
}

err = ioutil.WriteFile(to, body, 0600)
err = os.WriteFile(to, body, 0600)

return errors.WithStack(err)
}
59 changes: 59 additions & 0 deletions cmd/drive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"context"
"log"
"net/http"
"time"

ucli "github.com/urfave/cli/v2"

"github.com/seborama/pcloud/fuse"
"github.com/seborama/pcloud/sdk"
)

func drive(c *ucli.Context) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

sdkHTTPClient := &http.Client{
Transport: &http.Transport{
MaxIdleConnsPerHost: 2,
MaxConnsPerHost: 10,
ResponseHeaderTimeout: 20 * time.Second,
Proxy: http.ProxyFromEnvironment,
},
Timeout: 0,
}

pCloudClient := sdk.NewClient(sdkHTTPClient)

log.Println("Logging into pCloud")
err := pCloudClient.Login(
ctx,
c.String("pcloud-otp-code"),
sdk.WithGlobalOptionUsername(c.String("pcloud-username")),
sdk.WithGlobalOptionPassword(c.String("pcloud-password")),
)
if err != nil {
return err
}

log.Println("Creating drive")
drive, err := fuse.NewDrive(
c.String("mount-point"),
pCloudClient,
)
if err != nil {
panic(err)
}
defer func() { _ = drive.Unmount() }()

log.Println("Mouting FS at", c.String("mount-point"))
err = drive.Mount()
if err != nil {
panic(err)
}

return nil
}
13 changes: 13 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ func main() {
},
},
},
{
Name: "drive",
Aliases: []string{"d"},
Usage: "pCloud FUSE drive",
Action: drive,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "mount-point",
Usage: "Location of mount point",
Required: true,
},
},
},
},
}

Expand Down
9 changes: 8 additions & 1 deletion fuse/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ I am developing on a Linux ARM Raspberry Pi4. I haven't (yet) tried Linux x86_64

At this stage, this is explorative. The code base is entirely experimental, most features are not implemented or only partially.

The drive can be mounted via the tests and it can be "walked" through.
The drive can be mounted via the CLI:

```bash
# Remember to first export the CLI's PCLOUD_* environemt variables!
go run ./cmd drive --mount-point /tmp/pcloud_mnt
```

Read operations are very slow and `dd` is not supported (need to investigate that one).

No write operations are supported for now.

Expand Down
4 changes: 2 additions & 2 deletions fuse/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Drive struct {
conn *fuse.Conn // TODO: define an interface
}

func Mount(mountpoint string, pcClient *sdk.Client) (*Drive, error) {
func NewDrive(mountpoint string, pcClient *sdk.Client) (*Drive, error) {
conn, err := fuse.Mount(
mountpoint,
fuse.FSName("pcloud"),
Expand Down Expand Up @@ -62,7 +62,7 @@ func (d *Drive) Unmount() error {
return d.conn.Close()
}

func (d *Drive) Serve() error {
func (d *Drive) Mount() error {
return fs.Serve(d.conn, d.fs)
}

Expand Down
4 changes: 2 additions & 2 deletions fuse/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Test(t *testing.T) {

mountpoint := "/tmp/pcloud_mnt"

drive, err := pfuse.Mount(
drive, err := pfuse.NewDrive(
mountpoint,
pcClient,
)
Expand All @@ -28,7 +28,7 @@ func Test(t *testing.T) {
defer func() { _ = drive.Unmount() }()

log.Println("Mouting FS")
err = drive.Serve()
err = drive.Mount()
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/seborama/pcloud

go 1.20
go 1.21

require (
bazil.org/fuse v0.0.0-20230120002735-62a210ff1fd5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tv42/httpunix v0.0.0-20191220191345-2ba4b9c3382c h1:u6SKchux2yDvFQnDHS3lPnIRmfVJ5Sxy3ao2SIdysLQ=
github.com/tv42/httpunix v0.0.0-20191220191345-2ba4b9c3382c/go.mod h1:hzIxponao9Kjc7aWznkXaL4U4TWaDSs8zcsY4Ka08nM=
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913/go.mod h1:4aEEwZQutDLsQv2Deui4iYQ6DWTxR14g6m8Wv88+Xqk=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
Expand Down

0 comments on commit fe88ba2

Please sign in to comment.