-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A gateway to proper filesystem builtins needed for #53.
- Loading branch information
Showing
6 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |