diff --git a/rclonefs/example/README.md b/rclonefs/example/README.md index 91d18b7..da26852 100644 --- a/rclonefs/example/README.md +++ b/rclonefs/example/README.md @@ -19,13 +19,13 @@ you put during the first step, is pcloud1. name> pcloud ``` -3. Put the file person.json or any other to the root +3. Put the file employee.yml or any other to the root directory of your PCloud storage. 4. Make sure that your configuration works. For example, run this command: ``` -rclone cat pcloud1:person.json +rclone cat pcloud1:employee.yml ``` pcloud1 is the name of your remote storage: @@ -34,21 +34,33 @@ pcloud1 is the name of your remote storage: - you will see the content of the file in your console. If your cloud storage contains buckets (Amazon S3, -minio, Backblaze B2) and the file mock.json is put into +minio, Backblaze B2) and the file employee.yml is put into the bucket1, the access to the file is: ```go -TODO +rclone cat minio1:bucket1/employee.yml ``` -5. Put the name of your remote storage with the full +minio1 is the name of your cloud storage. + +The data is stored in the file rclone.conf. On Linux, +its default path is: + +``` +${HOME}/.config/rclone/rclone.conf +``` + +5. Create "/cfg/json" directory in your remote storage. +It will be your working directory in the example. + +6. Put the name of your remote storage with the full path '/cfg/json' to the file 'cloud.txt' ``` pcloud_mv1:/cfg/json ``` -6. Run the example: +7. Run the example: ``` go run example.go diff --git a/rclonefs/example/employee.yml b/rclonefs/example/employee.yml new file mode 100644 index 0000000..dcf444d --- /dev/null +++ b/rclonefs/example/employee.yml @@ -0,0 +1,19 @@ +--- +# An employee record +name: Martin D'vloper +job: Developer +skill: Elite +employed: True +foods: + - Apple + - Orange + - Strawberry + - Mango +languages: + perl: Elite + python: Elite + pascal: Lame +education: | + 4 GCSEs + 3 A-Levels + BSc in the Internet of Things diff --git a/rclonefs/example/example.go b/rclonefs/example/example.go index a7d193e..b9a75c6 100644 --- a/rclonefs/example/example.go +++ b/rclonefs/example/example.go @@ -1,6 +1,9 @@ +// Example and test package main import ( +// "bytes" + "encoding/json" "fmt" "os" "log" @@ -23,17 +26,192 @@ func main() { log.Fatalf("Error in CreateRCFS: %v\n", err) } + // Test examples start. + // File path beginning from / is the absolute path. + // Otherwise it is the relative path. + fmt.Printf("Checking...\n") + // Create() test - file1, err := RFS.Create("/cfg/json/file1.json") // absolute path + file1, err := RFS.Create("/cfg/json/object.json") // absolute path if err != nil { log.Fatal("Create() test FAILED\n") } file1.Close() - ok, err := afero.Exists(RFS, "file1.json") // relative path + ok, err := afero.Exists(RFS, "object.json") // relative path if err != nil { - log.Fatal("Error in Exists(): %v\n") + log.Fatalf("Error in Exists(): %v\n", err) } if ok { fmt.Printf("Create() test passed.\n") } + + // Mkdir() test + err = RFS.Mkdir("../yml", 0750) // absolute path: /cfg/yml + if err != nil { + log.Fatal("Mkdir() test FAILED\n") + } + ok, err = afero.DirExists(RFS, "../yml") + if err != nil { + log.Fatalf("Error in DirExists(): %v\n", err) + } + if ok { + fmt.Printf("Mkdir() test passed.\n") + } + + // MkdirAll() test + err = RFS.MkdirAll("../../data/passwords/sites", 0750) // absolute path: /data/passwords/sites + if err != nil { + log.Fatal("MkdirAll() test FAILED\n") + } + ok, err = afero.DirExists(RFS, "/data/passwords/sites") + if err != nil { + log.Fatalf("Error in DirExists(): %v\n", err) + } + if ok { + fmt.Printf("MkdirAll() test passed.\n") + } + + // afero.WriteFile() test - the function is based on OpenFile() + // copying object.json to remote storage + data, err := os.ReadFile("object.json") + if err != nil { + log.Fatalf("Error in ReadFile(): %v\n", err) + } + afero.WriteFile(RFS, "object.json", data, 0644) + + // Open() test + file1, err = RFS.Open("object.json") + if err != nil { + log.Fatal("Open() test FAILED\n") + } + + // Stat() test + if fi, err := file1.Stat(); err == nil { + if size := fi.Size(); size == 129 { + fmt.Printf("Stat() test passed.\n") + } + } + file1.Close() + + // afero.ReadFile() test - the function is based on Open() + data, err = afero.ReadFile(RFS, "object.json") + if err != nil { + log.Fatalf("Error in afero.ReadFile(): %v\n", err) + } + + mp := make(map[string]interface{}) + err = json.Unmarshal(data, &mp) + if err != nil { + log.Fatalf("Error in json.Unmarshal(): %v\n", err) + } + + var test2 int = int(mp["test2"].(float64)) + if test2 != 7 { + log.Fatal("afero.ReadFile() / Open() test FAILED") + } + + fmt.Printf("Open(): all tests passed.\n") + fmt.Printf("OpenFile() test passed.\n") + + // GetTempDir example + // Relative paths like "../cfg" do not work. + afero.GetTempDir(RFS, "cfg") + + // SafeWriteReader example + srcfile, err := os.OpenFile("call.json", os.O_RDWR, 0644) + if err != nil { + log.Fatalf("Error in os.OpenFile(): %v\n", err) + } + + err = afero.SafeWriteReader(RFS, "../../data/passwords/sites/github/call.json", srcfile) + if err != nil { + log.Fatalf("SafeWriteReader() test FAILED") + } + srcfile.Close() + + ok, err = afero.Exists(RFS, "/data/passwords/sites/github/call.json") + if err != nil { + log.Fatalf("Error in afero.Exists(): %v\n", err) + } + if ok { + fmt.Printf("SafeWriteReader() / MkdirAll() test passed.\n") + } + + // WriteReader example + srcfile, err = os.OpenFile("employee.yml", os.O_RDWR, 0644) + if err != nil { + log.Fatalf("Error in os.OpenFile(): %v\n", err) + } + + err = afero.WriteReader(RFS, "/data/passwords/employee.yml", srcfile) + if err != nil { + log.Fatalf("WriteReader() test FAILED") + } + srcfile.Close() + + ok, err = afero.Exists(RFS, "/data/passwords/employee.yml") + if err != nil { + log.Fatalf("Error in afero.Exists(): %v\n", err) + } + if ok { + fmt.Printf("WriteReader() / OpenFile() test passed.\n") + } + + // TempDir() example + tdir, err := afero.TempDir(RFS, "../../cfg", "tmp") + if err != nil { + log.Fatalf("Error in afero.TempDir(): %v\n", err) + } + + ok, err = afero.DirExists(RFS, tdir) + if err != nil { + log.Fatalf("Error in DirExists(): %v\n", err) + } + if ok { + fmt.Printf("TempDir() test passed.\n") + } + + // Remove() test + err = RFS.Remove("/data/passwords/employee.yml") + if err != nil { + log.Fatal("Remove() test FAILED\n") + } + + ok, err = afero.Exists(RFS, "/data/passwords/employee.yml") + if err != nil { + log.Fatal("Error in afero.Exists(): %v\n", err) + } + if !ok { + fmt.Printf("Remove() test passed.\n") + } + + // RemoveAll() test + err = RFS.RemoveAll("../../data/passwords") // relative path + if err != nil { + log.Fatal("RemoveAll() test FAILED\n") + } + + ok, err = afero.DirExists(RFS, "/data/passwords") // absolute path + if err != nil { + log.Fatal("Error in afero.DirExists(): %v\n", err) + } + if !ok { + fmt.Printf("RemoveAll() test passed.\n") + } + + // Rename() test + err = RFS.Rename("object.json", "object2.json") // relative paths + if err != nil { + log.Fatal("Rename() test FAILED\n") + } + + ok, err = afero.Exists(RFS, "/cfg/json/object2.json") // absolute path + if err != nil { + log.Fatal("Error in afero.Exists(): %v\n") + } + if ok { + fmt.Printf("Rename() test passed.\n") + } + + fmt.Printf("ALL TESTS PASSED\n") } diff --git a/rclonefs/example/person.json b/rclonefs/example/person.json deleted file mode 100644 index 9956beb..0000000 --- a/rclonefs/example/person.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "person1": { - "name": "Alice", - "welcome": "Hello Alice!" - }, - "person2": { - "name": "Bob", - "welcome": "Hello Bob!" - } -}