Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jhchabran/gistfs
A Go io/fs filesystem implementation for reading files in Github gists.
https://github.com/jhchabran/gistfs
golang
Last synced: 19 days ago
JSON representation
A Go io/fs filesystem implementation for reading files in Github gists.
- Host: GitHub
- URL: https://github.com/jhchabran/gistfs
- Owner: jhchabran
- License: mit
- Created: 2021-01-02T16:22:37.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2021-09-17T16:45:51.000Z (about 3 years ago)
- Last Synced: 2024-10-04T10:58:11.519Z (about 2 months ago)
- Topics: golang
- Language: Go
- Homepage:
- Size: 21.5 KB
- Stars: 127
- Watchers: 3
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GistFS
GistFS is an `io/fs` implementation that enables to read files stored in a given Gist.
## Requirements
This module depends on `io/fs` which is only available since [go 1.16](https://tip.golang.org/doc/go1.16).
## Usage
GistFS is threadsafe.
```go
package mainimport (
"context"
"fmt"
"net/http""github.com/jhchabran/gistfs"
)func main() {
// create a FS based on https://gist.github.com/jhchabran/ded2f6727d98e6b0095e62a7813aa7cf
gfs := gistfs.New("ded2f6727d98e6b0095e62a7813aa7cf")// load the remote content once for all,
// ie, no more API calls toward Github will be made.
err := gfs.Load(context.Background())
if err != nil {
panic(err)
}// --- base API
// open the "test1.txt" file
f, err := gfs.Open("test1.txt")
if err != nil {
panic(err)
}// read its content
b := make([]byte, 1024)
_, err = f.Read(b)if err != nil {
panic(err)
}fmt.Println(string(b))
// --- ReadFile API
// directly read the "test1.txt" file
b, err = gfs.ReadFile("test1.txt")
if err != nil {
panic(err)
}fmt.Println(string(b))
// --- ReadDir API
// there is only one directory in a gistfile, the root dir "."
files, err := gfs.ReadDir(".")
if err != nil {
panic(err)
}for _, entry := range files {
fmt.Println(entry.Name())
}// --- Serve the files from the gists over http
http.ListenAndServe(":8080", http.FileServer(http.FS(gfs)))
}
```## See also
- [io/fs godoc](https://pkg.go.dev/io/fs)