Skip to content

Commit

Permalink
Basic GitHub filesystem support
Browse files Browse the repository at this point in the history
A gateway to proper filesystem builtins needed for #53.
  • Loading branch information
edigaryev committed Sep 16, 2020
1 parent a37366d commit 211491b
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/go-test/deep v1.0.7
github.com/gogo/protobuf v1.3.1 // indirect
github.com/golang/protobuf v1.4.2
github.com/google/go-github/v32 v32.1.0
github.com/google/uuid v1.1.1
github.com/gorilla/mux v1.7.4 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.2.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-github/v32 v32.1.0 h1:GWkQOdXqviCPx7Q7Fj+KyPoGm4SwHRh8rheoPhd27II=
github.com/google/go-github/v32 v32.1.0/go.mod h1:rIEpZD9CTDQwDK9GDrtMTycQNA4JU3qBsCizh3q2WCI=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
Expand Down
4 changes: 3 additions & 1 deletion pkg/larker/fs/fs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fs

import "context"
import (
"context"
)

type FileSystem interface {
Get(ctx context.Context, path string) ([]byte, error)
Expand Down
51 changes: 51 additions & 0 deletions pkg/larker/fs/github/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package github

import (
"context"
"encoding/base64"
"errors"
"fmt"
"github.com/google/go-github/v32/github"
"syscall"
)

var ErrAPI = errors.New("failed to communicate with the GitHub API")

type GitHub struct {
client *github.Client
owner string
repo string
reference string
}

func New(owner, repo, reference, token string) *GitHub {
return &GitHub{
client: github.NewClient(nil),
owner: owner,
repo: repo,
reference: reference,
}
}

func (gh *GitHub) Get(ctx context.Context, path string) ([]byte, error) {
fileContent, _, _, err := gh.client.Repositories.GetContents(ctx, gh.owner, gh.repo, path,
&github.RepositoryContentGetOptions{
Ref: gh.reference,
},
)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrAPI, err)
}

// Simulate os.Read() behavior in case the supplied path points to a directory
if fileContent == nil {
return nil, syscall.EISDIR
}

fileBytes, err := base64.StdEncoding.DecodeString(*fileContent.Content)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrAPI, err)
}

return fileBytes, nil
}
32 changes: 32 additions & 0 deletions pkg/larker/fs/github/github_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package github_test

import (
"context"
"errors"
"github.com/cirruslabs/cirrus-cli/pkg/larker/fs"
"github.com/cirruslabs/cirrus-cli/pkg/larker/fs/github"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"syscall"
"testing"
)

func selfFS() fs.FileSystem {
return github.New("cirruslabs", "cirrus-cli", "master", "")
}

func TestGetFile(t *testing.T) {
fileBytes, err := selfFS().Get(context.Background(), "go.mod")
if err != nil {
t.Fatal(err)
}

assert.Contains(t, string(fileBytes), "module github.com/cirruslabs/cirrus-cli")
}

func TestGetDirectory(t *testing.T) {
_, err := selfFS().Get(context.Background(), ".")

require.Error(t, err)
assert.True(t, errors.Is(err, syscall.EISDIR))
}
39 changes: 39 additions & 0 deletions pkg/larker/fs/local/local_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package local_test

import (
"context"
"errors"
"github.com/cirruslabs/cirrus-cli/internal/testutil"
"github.com/cirruslabs/cirrus-cli/pkg/larker/fs/local"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io/ioutil"
"path/filepath"
"syscall"
"testing"
)

func TestGetFile(t *testing.T) {
// Prepare temporary directory
dir := testutil.TempDir(t)
if err := ioutil.WriteFile(filepath.Join(dir, "some-file.txt"), []byte("some-contents"), 0600); err != nil {
t.Fatal(err)
}

fileBytes, err := local.New(dir).Get(context.Background(), "some-file.txt")
if err != nil {
t.Fatal(err)
}

assert.Contains(t, string(fileBytes), "some-contents")
}

func TestGetDirectory(t *testing.T) {
// Prepare temporary directory
dir := testutil.TempDir(t)

_, err := local.New(dir).Get(context.Background(), ".")

require.Error(t, err)
assert.True(t, errors.Is(err, syscall.EISDIR))
}

0 comments on commit 211491b

Please sign in to comment.