func readFile(source *Source) ([]byte, error) { if source.FS == nil { source.FS = vfs.OS() } // make sure we can access the file _, err := source.FS.Stat(source.URL.Path) if err != nil { log.Fatalf("Can't stat %s: %#v", source.URL.Path, err) return nil, err } f, err := source.FS.OpenFile(source.URL.Path, os.O_RDWR, 0) if err != nil { log.Fatalf("Can't open %s: %#v", source.URL.Path, err) return nil, err } b, err := ioutil.ReadAll(f) if err != nil { log.Fatalf("Can't read %s: %#v", source.URL.Path, err) return nil, err } return b, nil }
// Every vfs.Filesystem could be easily wrapped func ExampleReadOnlyOS() { // Create a readonly vfs accessing the filesystem of the underlying OS roFS := vfs.ReadOnly(vfs.OS()) // Mkdir is disabled on ReadOnly vfs, will return vfs.ErrReadOnly // See vfs.ReadOnly for all disabled operations err := roFS.Mkdir("/tmp/vfs", 0777) if err != nil { fatal("Error creating directory: %s\n", err) } // OpenFile is controlled to support read-only functionality. os.O_CREATE or os.O_APPEND will fail. // Flags like os.O_RDWR are supported but the returned file is protected e.g. from Write(..). f, err := roFS.OpenFile("/tmp/vfs/example.txt", os.O_RDWR, 0) if err != nil { fatal("Could not create file: %s\n", err) } defer f.Close() // Will fail and return vfs.ErrReadOnly _, err = f.Write([]byte("VFS working on your filesystem")) if err != nil { fatal("Could not write file on read only filesystem: %s", err) } }
func main() { flag.Parse() file := flag.Arg(0) if *version == true { fmt.Printf("%s version %s\n", os.Args[0], versionInfo()) return } if file == "" { fs := vfs.OS() env := venv.OS() file = GetInputPath(fs, env) } if file == "" { fmt.Printf("Usage: %s [options] path\n", os.Args[0]) os.Exit(1) } if !*list && *host == "" && !*inventory { fmt.Fprint(os.Stderr, "Either --host or --list must be specified") os.Exit(1) } path, err := filepath.Abs(file) if err != nil { fmt.Fprintf(os.Stderr, "Invalid file: %s\n", err) os.Exit(1) } stateFile, err := os.Open(path) defer stateFile.Close() if err != nil { fmt.Fprintf(os.Stderr, "Error opening tfstate file: %s\n", err) os.Exit(1) } var s state err = s.read(stateFile) if err != nil { fmt.Fprintf(os.Stderr, "Error reading tfstate file: %s\n", err) os.Exit(1) } if *list { os.Exit(cmdList(os.Stdout, os.Stderr, &s)) } else if *inventory { os.Exit(cmdInventory(os.Stdout, os.Stderr, &s)) } else if *host != "" { os.Exit(cmdHost(os.Stdout, os.Stderr, &s, *host)) } }
func ExampleMyWrapper() { // Disable Mkdirs on the OS Filesystem var fs vfs.Filesystem = NoNewDirs(vfs.OS()) err := fs.Mkdir("/tmp", 0777) if err != nil { fatal("Mkdir disabled!") } }
func ExampleOsFS_myWrapper() { // Disable Mkdirs on the OS Filesystem var fs vfs.Filesystem = NoNewDirs(vfs.OS()) err := fs.Mkdir("/tmp", 0777) if err != nil { fmt.Printf("Mkdir disabled!\n") } }
func ExampleMountFS() { // Create a vfs supporting mounts // The root fs is accessing the filesystem of the underlying OS fs := mountfs.Create(vfs.OS()) // Mount a memfs inside fs.Mount(memfs.Create(), "/memfs") // This will create /testdir inside the memfs fs.Mkdir("/memfs/testdir", 0777) // This will create /tmp/testdir inside your OS fs fs.Mkdir("/tmp/testdir", 0777) }
func ExampleBasicOS() { // Create a vfs accessing the filesystem of the underlying OS osFS := vfs.OS() err := osFS.Mkdir("/tmp/vfs", 0777) if err != nil { fatal("Error creating directory: %s\n", err) } f, err := osFS.Create("/tmp/vfs/example.txt") if err != nil { fatal("Could not create file: %s\n", err) } defer f.Close() if _, err := f.Write([]byte("VFS working on your filesystem")); err != nil { fatal("Error writing to file: %s\n", err) } }
func ExampleBasicOS() { // Create a vfs accessing the filesystem of the underlying OS osFS := vfs.OS() err := osFS.Mkdir("/tmp/vfs", 0777) if err != nil { fatal("Error creating directory: %s\n", err) } // Convenience method f, err := vfs.Create(osFS, "/tmp/vfs/example.txt") // f, err := osFS.OpenFile("/tmp/vfs/example.txt", os.O_CREATE|os.O_RDWR, 0666) if err != nil { fatal("Could not create file: %s\n", err) } defer f.Close() if _, err := f.Write([]byte("VFS working on your filesystem")); err != nil { fatal("Error writing to file: %s\n", err) } }
func Example() { // Create a vfs accessing the filesystem of the underlying OS var osfs vfs.Filesystem = vfs.OS() osfs.Mkdir("/tmp", 0777) // Make the filesystem read-only: osfs = vfs.ReadOnly(osfs) // Simply wrap filesystems to change its behaviour // os.O_CREATE will fail and return vfs.ErrReadOnly // os.O_RDWR is supported but Write(..) on the file is disabled f, _ := osfs.OpenFile("/tmp/example.txt", os.O_RDWR, 0) // Return vfs.ErrReadOnly _, err := f.Write([]byte("Write on readonly fs?")) if err != nil { fmt.Errorf("Filesystem is read only!\n") } // Create a fully writable filesystem in memory mfs := memfs.Create() mfs.Mkdir("/root", 0777) // Create a vfs supporting mounts // The root fs is accessing the filesystem of the underlying OS fs := mountfs.Create(osfs) // Mount a memfs inside /memfs // /memfs may not exist fs.Mount(mfs, "/memfs") // This will create /testdir inside the memfs fs.Mkdir("/memfs/testdir", 0777) // This would create /tmp/testdir inside your OS fs // But the rootfs `osfs` is read-only fs.Mkdir("/tmp/testdir", 0777) }
func GetTarget(path string) (Target, error) { return &fsRoot{vfs.OS(), path}, nil }
func NewTemplate(path string) (Template, error) { return &fsRoot{vfs.OS(), path}, nil }
"github.com/blang/vfs/mountfs" "github.com/r0cketman/goSpring-XD/rest" "log" "net/http" ) /////////////////////////////////////////////////// //// create a virtual file system wrapper //// create an in memory file system //// this is where we store the physical file system //// this is outside of the main function for variable scoping /////////////////////////////////////////////////// // create the vfs object that access the underlying file system. // Make the filesystem read-only: var osfs vfs.Filesystem = vfs.OS() func main() { /////////////////////////////////////////////////// //// Here is where we create our API //// We build End Points //// Set routes //// Create functions that serve end points // ToDo add end point to retrieve files, to do add hypermedia for discoverable // Ned to fix references to API framework, so forking doesn't cause any sillyness // Fernando Zavala 2/9/2016 /////////////////////////////////////////////////// osfs.Mkdir("root", 0777) // now, let's create the in memory fs object