Skip to content

Commit

Permalink
Initial commit of vfs
Browse files Browse the repository at this point in the history
  • Loading branch information
jkc committed Aug 7, 2017
0 parents commit a1fd606
Show file tree
Hide file tree
Showing 33 changed files with 9,876 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
vendor
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) 2011-2012 C2FO

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
117 changes: 117 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# vfs - Virtual File System
> Go library to generalize commands and behavior when interacting with various file systems.
The vfs library includes interfaces which allow you to interact with files and locations on various file systems in a generic way. Currently supported file systems:
* Local fs (Windows, OS X, Linux)
* Amazon S3
* GCS

These interfaces are composed of standard Go library interfaces, allowing for simple file manipulation within, and between
the supported file systems.

At C2FO we have created a factory system that is integrated with our app configuration that allows for simply initializing the various locations we tend to do file work in. You can build your own similar system directly on top of the various file system implementations and the provided generic interfaces, or you can use the simple interface included in the vfs package.
The usage examples below will detail this simple interface. We will eventually be providing a version of our factory as an example of how this library can be used in a more complex project.

A couple notes on configuration for this interface (vfs.NewFile and vfs.NewLocation):
* Before calling either function you must initialize any file systems you expect to be using.
* Local: The local file system requires no configuration. Simply call vfs.InitializeLocalFileSystem so the internals are prepared to expect "file:///" URIs.
* S3: The vfs.InitializeS3FileSystem() method requires authentication parameters for the user, see godoc for this function.
* GCS: In addition to calling vfs.InitializeGSFileSystem, you are expected to have authenticated with GCS using the Google Cloud Shell for the user running the app. We will be looking into more flexible forms of authentication (similar to the S3 library) in the future, but this was an ideal use case for us to start with, and therefore, all that is currently provided.

## Installation

OS X, Linux, and Windows:

```sh
glide install github.com/c2fo/vfs
```

## Usage example

```go
import "github.com/c2fo/vfs"

// The following functions tell vfs we expect to handle a particular file system in subsequent calls to
// vfs.NewFile() and vfs.NewLocation
// Local files, ie: "file:///"
vfs.InitializeLocalFileSystem()

// Google Cloud Storage, ie: "gs://"
vfs.InitializeGSFileSystem()

// Amazon S3, ie: "s3://"
vfs.InitializeS3FileSystem(accessKeyId, secreteAccessKey, token)

// alternative to above for S3, if you've already initialized a client of interface s3iface.S3API
vfs.SetS3Client(client)
```

You can then use those file systems to initialize locations which you'll be referencing frequently, or initialize files directly

```go
osFile, err := vfs.NewFile("file:///path/to/file.txt")
s3File, err := vfs.NewFile("s3://bucket/prefix/file.txt")

osLocation, err := vfs.NewLocation("file:///tmp")
s3Location, err := vfs.NewLocation("s3://bucket")

osTmpFile, err := osLocation.NewFile("anotherFile.txt") // file at /tmp/anotherFile.txt
```

With a number of files and locations between s3 and the local file system you can perform a number of actions without any consideration for the system's api or implementation details.

```go
osFileExists, err := osFile.Exists() // true, nil
s3FileExists, err := s3File.Exists() // false, nil
err = osFile.CopyToFile(s3File) // nil
s3FileExists, err = s3File.Exists() // true, nil

movedOsFile, err := osFile.MoveToLocation(osLocation)
osFileExists, err = osFile.Exists() // false, nil (move actions delete the original file)
movedOsFileExists, err := movedOsFile.Exists() // true, nil

s3FileUri := s3File.URI() // s3://bucket/prefix/file.txt
s3FileName := s3File.Name() // file.txt
s3FilePath := s3File.Path() // /prefix/file.txt

// vfs.File and vfs.Location implement fmt.Stringer, returning x.URI()
fmt.Sprintf("Working on file: %s", s3File) // "Working on file: s3://bucket/prefix/file.txt"
```

## Development setup

Fork the project and clone it locally, then in the cloned directory...

```sh
glide install
go test $(glide novendor)
```

## Release History

* 0.1.0
* The first release
* Support for local file system, s3, and gcs
* Initial README.md

## Meta

Brought to you by the Enterprise Pipeline team at C2FO:

John Judd - [email protected]

Jason Coble - [@jasonkcoble](https://twitter.com/jasonkcoble) - [email protected]

Chris Roush – [email protected]

Distributed under the MIT license. See ``LICENSE`` for more information.

[https://github.com/c2fo/](https://github.com/c2fo/)

## Contributing

1. Fork it (<https://github.com/c2fo/vfs/fork>)
2. Create your feature branch (`git checkout -b feature/fooBar`)
3. Commit your changes (`git commit -am 'Add some fooBar'`)
4. Push to the branch (`git push origin feature/fooBar`)
5. Create a new Pull Request
53 changes: 53 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Package which provides a technique for handling multiple errors in the event of deferred method calls
// such as file.Close()
package errors

import (
"errors"
"fmt"
)

//MultiErr provides a set of functions to handle the scenario where, because of errors in defers, we have a way to handle
//the potenetial of multiple errors.
//for instance, if you do a open a file, defer it's close, then fail to Seek. The seek fauilure has one error but then
//the Close fails as well. This ensure neither are ignored.
type MultiErr struct {
errs []error
}

// Constructor for generating a zero-value MultiErr reference.
func NewMutliErr() *MultiErr {
return &MultiErr{}
}

// Returns the error message string.
func (me *MultiErr) Error() string {
var errorString string
for _, err := range me.errs {
errorString = fmt.Sprintf("%s%s\n", errorString, err.Error())
}
return errorString
}

// Appends the provided errors to the errs slice for future message reporting.
func (me *MultiErr) Append(errs ...error) error {
me.errs = append(me.errs, errs...)
return errors.New("return value for multiErr must be set in the first deferred function")
}

// If there are no errors in the MultErr instance, then return nil, otherwise return the full MultiErr instance.
func (me *MultiErr) OrNil() error {
if len(me.errs) > 0 {
return me
}
return nil
}

type singleErrReturn func() error


func (me *MultiErr) DeferFunc(f singleErrReturn) {
if err := f(); err != nil {
_ = me.Append(err)
}
}
29 changes: 29 additions & 0 deletions errors/errors_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package errors

import "github.com/c2fo/vfs"

func ExampleMultiErr_DeferFunc() {
// NOTE: We use a named error in the function since our first defer will set it based on any appended errors
_ = func(f vfs.File) (rerr error) {
//THESE LINES REQUIRED
errs := NewMutliErr()
defer func() { rerr = errs.OrNil() }()

_, err := f.Read(nil)
if err != nil {
//for REGULAR ERROR RETURNS we just return the Appended errors
return errs.Append(err)
}

// for defers, use DeferFunc and pass it the func name
defer errs.DeferFunc(f.Close)

_, err = f.Seek(0, 0)
if err != nil {
//for REGULAR ERROR RETURNS we just return the Appended errors
return errs.Append(err)
}

return nil
}
}
138 changes: 138 additions & 0 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package: github.com/c2fo/vfs
import:
- package: cloud.google.com/go
version: 085c05ca074a8de9107005f9baa6308eae7eaf41
subpackages:
- storage
- package: github.com/aws/aws-sdk-go
version: ~1.10.18
subpackages:
- aws/awserr
- aws/request
- service/s3
- service/s3/s3iface
- service/s3/s3manager
- package: github.com/stretchr/testify
version: ~1.1.4
subpackages:
- mock
- package: golang.org/x/net
subpackages:
- context
- package: google.golang.org/api
version: 48e49d1645e228d1c50c3d54fb476b2224477303
subpackages:
- iterator
Loading

0 comments on commit a1fd606

Please sign in to comment.