Skip to content

Commit 20dc007

Browse files
committed
improve doc
1 parent 74e5d62 commit 20dc007

File tree

9 files changed

+96
-36
lines changed

9 files changed

+96
-36
lines changed

examples/godoc/godoc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// An example that locally serves files from github.com/golang/go/doc
1+
// An example locally serves files from github.com/golang/go/doc.
22
package main
33

44
import (

examples/templates/templates.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// An example that locally serves files from github.com/golang/go/doc
1+
// An example that shows how gitfs helps using template files with Go code smoothly.
22
package main
33

44
import (

fsutil/fs.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Package fsutil provides utility functions for http filesystems.
1+
// Package fsutil provides useful utility functions for http.FileSystem.
22
package fsutil
33

44
import (
@@ -9,15 +9,17 @@ import (
99
"github.com/kr/fs"
1010
)
1111

12-
// Walk returns a https://godoc.org/github.com/kr/fs#Walker over an
13-
// https://golang.org/pkg/net/http/#FileSystem.
12+
// Walk returns a fs.Walker over http.FileSystem, which enables
13+
// walking over all files in the filesystem.
14+
//
15+
// See https://godoc.org/github.com/kr/fs#Walker for more details.
1416
func Walk(hfs http.FileSystem, root string) *fs.Walker {
1517
return fs.WalkFS(root, fileSystem{hfs})
1618
}
1719

18-
// FileSystem implements https://godoc.org/github.com/kr/fs#FileSystem over
19-
// https://golang.org/pkg/net/http/#FileSystem. It can be used to walking
20-
// over files in the filesystem.
20+
// FileSystem implements fs.FileSystem over http.FileSystem.
21+
//
22+
// See https://godoc.org/github.com/kr/fs#FileSystem for more details.
2123
type fileSystem struct {
2224
http.FileSystem
2325
}

fsutil/tmpl.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/pkg/errors"
1212
)
1313

14-
// TmplParse Parse templates from the given filesystem according to the
14+
// TmplParse parses templates from the given filesystem according to the
1515
// given paths. If tmpl is not nil, the templates will be added to it.
1616
// paths must contain at least one path. All paths must exist in the
1717
// given filesystem.
@@ -30,7 +30,7 @@ func TmplParseGlob(fs http.FileSystem, tmpl *txttmpl.Template, pattern string) (
3030
return t.Template, err
3131
}
3232

33-
// TmplParseHTML Parse HTML templates from the given filesystem according
33+
// TmplParseHTML parses HTML templates from the given filesystem according
3434
// to the given paths. If tmpl is not nil, the templates will be added to
3535
// it. paths must contain at least one path. All paths must exist in the
3636
// given filesystem.

gitfs.go

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,68 @@
1-
// Package gitfs enables using static files in Go code without packing them into binary.
1+
// Package gitfs loads static files in Go code without binary-packing.
22
//
3-
// This package enable loading static files from a remote git repository,
4-
// in Go code. This files can be used for static serving, template loading,
3+
// This package enable loading any file from remote git repository in Go code.
4+
// This files can be used for static serving, template loading, content loading,
55
// or anything else.
6+
//
67
// The following features are supported:
78
//
89
// * Loading of specific version is supported.
10+
//
911
// * For debug purposes, the files can be loaded from local path. The transition
10-
// from remote project to local files is smooth.
12+
// from remote project to local files is smooth.
13+
//
1114
// * Project is loaded instantly and files are loaded lazily but only once.
1215
//
13-
// Examples
16+
// Usage
1417
//
15-
// To setup a filesystem from a github repository `github.com/x/y`
16-
// at a given version v1.2.3 and path "static" inside the project:
18+
// First, we need to create a filesystem using the `New` function.
19+
// This function accepts the project path with pattern:
20+
// `github.com/<owner>/<repo>(/<path>)?(@<ref>)?`.
21+
// If no `path` is specified, the root of the project will be used.
22+
// `ref` can be any git branch using `heads/<branch name>` or any
23+
// git tag using `tags/<tag>`. If the tag is of Semver format, the `tags/`
24+
// prefix is not required.
25+
// If no `ref` is specified, the default branch will be used.
26+
//
27+
// Here we will load repository `github.com/x/y` at version v1.2.3
28+
// and internal path "static":
1729
//
1830
// fs, err := gitfs.New(ctx, "github.com/x/y/[email protected]")
1931
//
20-
// Then, reading a file can be done by opening it:
32+
// Reading a file from the repository can be done using the `Open` method.
33+
// This function accepts a path, relative to the root of the defined
34+
// filesystem.
2135
//
2236
// f, err := fs.Open("index.html")
2337
//
24-
// The variable fs, which is of type `http.FileSystem`, can be used to serve
25-
// static content:
38+
// The variable `fs` implements `http.FileSystem`, and can be used for anything
39+
// that accepts it. For example, it can be used for serving static content
40+
// using the standard library:
2641
//
2742
// http.Handle("/", http.FileServer(fs))
2843
//
29-
// When used with private github repo, you would want to provide
30-
// an HTTP client with the appropriate credentials. For example,
31-
// if you have a Github Token in environnement variable `GITHUB_TOKEN`:
44+
// When used with private github repository, it should be accessed with
45+
// appropriate credentials. The credentials can be passed by providing an
46+
// HTTP client. For example, to use a Github Token from environnement
47+
// variable `GITHUB_TOKEN`:
3248
//
3349
// token := os.Getenv("GITHUB_TOKEN")
3450
// client := oauth2.NewClient(
3551
// context.Background(),
3652
// oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}))
3753
// fs, err := gitfs.New(ctx, "github.com/x/y", gitfs.OptClient(client))
3854
//
39-
// There are some useful functions in ./fsutil package. For example, it
40-
// enables working easily with templates (They can work with any
41-
// `http.FileSystem`):
55+
// For debugging purposes, it is easier and faster to use local static
56+
// content and not remote content that was pushed to the remote repository.
57+
// This is enabled by the `OptLocal` option. To use this option only in
58+
// local development and not in production system, it can be used as follow:
59+
//
60+
// local := os.Getenv("LOCAL_DEBUG")
61+
// fs, err := gitfs.New(ctx, "github.com/x/y", gitfs.OptLocal(local))
4262
//
43-
// fs, err := gitfs.New(ctx, "github.com/x/y/templates")
44-
// // Handle err...
45-
// tmpl, err := fsutil.TmplParseGlob(fs, nil, "*.gotmpl")
46-
// // Handle err...
47-
// tmpl.ExecuteTemplate(...)
63+
// Then, running the program with `LOCAL_DEBUG=.` will use local files while
64+
// running without it will use the remote files. (the value of the environment
65+
// variable should point to any directory within the github project).
4866
package gitfs
4967

5068
import (

gitfs_test.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,68 @@ import (
1111
"github.com/stretchr/testify/require"
1212
)
1313

14-
func ExampleTest() {
14+
// With gitfs you can open a remote git repository, and load any file,
15+
// including non-go files.
16+
// In this example, the README.md file of a remote repository is loaded.
17+
func Example_open() {
1518
ctx := context.Background()
16-
fs, err := New(ctx, "github.com/kelseyhightower/helloworld@tags/3.0.0")
19+
20+
// The load path is of the form: github.com/<owner>/<repo>(/<path>)?(@<ref>)?.
21+
// `ref` can reference any git tag or branch. If github releases are in Semver format,
22+
// the `tags/` prefix is not needed in the `ref` part.
23+
fs, err := New(ctx, "github.com/kelseyhightower/[email protected]")
1724
if err != nil {
1825
log.Fatalf("Failed initialize filesystem: %s", err)
1926
}
27+
28+
// Open any file in the github repository, using the `Open` function. Both files
29+
// and directory can be opened. The content is not loaded until it is actually being
30+
// read. The content is loaded only once.
2031
f, err := fs.Open("README.md")
2132
if err != nil {
2233
log.Fatalf("Failed opening file: %s", err)
2334
}
35+
36+
// Copy the content to stdout.
2437
io.Copy(os.Stdout, f)
38+
2539
// Output: # helloworld
2640
}
2741

28-
func ExampleTest_templates() {
42+
// The ./fsutil package is a collection of useful functions that can work with
43+
// any `http.FileSystem` implementation.
44+
// For example, here we will use a function that loads go templates from the
45+
// filesystem.
46+
func Example_fsutil() {
2947
ctx := context.Background()
48+
49+
// Open a git remote repository `posener/gitfs` in path `examples/templates`.
3050
fs, err := New(ctx, "github.com/posener/gitfs/examples/templates")
3151
if err != nil {
3252
log.Fatalf("Failed initialize filesystem: %s", err)
3353
}
3454

55+
// Use util function that loads all templates according to a glob pattern.
3556
tmpls, err := fsutil.TmplParseGlob(fs, nil, "*.gotmpl")
3657
if err != nil {
3758
log.Fatalf("Failed parsing templates: %s", err)
3859
}
60+
61+
// Execute the template and write to stdout.
3962
tmpls.ExecuteTemplate(os.Stdout, "tmpl1.gotmpl", "Foo")
63+
4064
// Output: Hello, Foo
4165
}
4266

67+
// Tests not supported repository pattern.
4368
func TestNew_notSupported(t *testing.T) {
4469
t.Parallel()
4570
ctx := context.Background()
4671
_, err := New(ctx, "git.com/nosuchusername/nosuchproject")
4772
require.Error(t, err)
4873
}
4974

75+
// Tests loading of local repository.
5076
func TestNew_local(t *testing.T) {
5177
t.Parallel()
5278
ctx := context.Background()

goreadme.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"recursive": true,
3+
"badges": {
4+
"goreadme": true,
5+
"travis_ci": true,
6+
"code_cov": true,
7+
"golang_ci": true,
8+
"go_doc": true
9+
}
10+
}

internal/githubfs/githubtree.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
var (
1919
reGithubProject = regexp.MustCompile(`^github\.com/([^@/]+)/([^@/]+)(/([^@]*))?(@([^#]+))?$`)
20-
reSemver = regexp.MustCompile(`^v\d+(\.\d+){0,2}$`)
20+
reSemver = regexp.MustCompile(`^v?\d+(\.\d+){0,2}$`)
2121
)
2222

2323
type project struct {

internal/githubfs/githubtree_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ func TestGithubProjectProperties(t *testing.T) {
5353
{path: "github.com/x/y@v1", wantOwner: "x", wantRepo: "y", wantRef: "tags/v1"},
5454
{path: "github.com/x/[email protected]", wantOwner: "x", wantRepo: "y", wantRef: "tags/v1.2"},
5555
{path: "github.com/x/[email protected]", wantOwner: "x", wantRepo: "y", wantRef: "tags/v1.2.3"},
56+
{path: "github.com/x/y@1", wantOwner: "x", wantRepo: "y", wantRef: "tags/1"},
57+
{path: "github.com/x/[email protected]", wantOwner: "x", wantRepo: "y", wantRef: "tags/1.2"},
58+
{path: "github.com/x/[email protected]", wantOwner: "x", wantRepo: "y", wantRef: "tags/1.2.3"},
5659
{path: "github.com/x/y/static/path", wantOwner: "x", wantRepo: "y", wantPath: "static/path/"},
5760
{path: "github.com/x/y/[email protected]", wantOwner: "x", wantRepo: "y", wantRef: "tags/v1.2.3", wantPath: "static/"},
5861
}
@@ -85,7 +88,8 @@ func TestGithubProjectProperties_error(t *testing.T) {
8588
// Invalid semvers
8689
"github.com/x/y@v1.",
8790
"github.com/x/[email protected]",
88-
"github.com/x/[email protected]",
91+
"github.com/x/y@1.",
92+
"github.com/x/[email protected]",
8993
}
9094

9195
for _, path := range paths {

0 commit comments

Comments
 (0)