-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
957 additions
and
365 deletions.
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
File renamed without changes.
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,20 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch Package", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "auto", | ||
"program": "${workspaceFolder}", | ||
"cwd": "${workspaceFolder}", | ||
"args": [ | ||
"--into", | ||
"inventory" | ||
] | ||
} | ||
] | ||
} |
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 was deleted.
Oops, something went wrong.
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,38 @@ | ||
package file | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"log/slog" | ||
"os" | ||
|
||
"github.com/lesomnus/bring/bringer" | ||
"github.com/lesomnus/bring/log" | ||
"github.com/lesomnus/bring/thing" | ||
) | ||
|
||
type br struct{} | ||
|
||
func FileBringer(opts ...bringer.Option) bringer.Bringer { | ||
return &br{} | ||
} | ||
|
||
func (b *br) Bring(ctx context.Context, t thing.Thing, opts ...bringer.Option) (io.ReadCloser, error) { | ||
l := log.From(ctx).With(slog.String("bringer", "file")) | ||
|
||
p := fmt.Sprintf("%s%s", t.Url.Host, t.Url.Path) | ||
l.Info("open", slog.String("path", p)) | ||
|
||
f, err := os.Open(p) | ||
if err != nil { | ||
return nil, fmt.Errorf("open: %w", err) | ||
} | ||
|
||
return f, nil | ||
} | ||
|
||
func init() { | ||
bringer.Register("", FileBringer) | ||
bringer.Register("file", FileBringer) | ||
} |
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,89 @@ | ||
package file_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/lesomnus/bring/bringer/file" | ||
"github.com/lesomnus/bring/thing" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFileBringer(t *testing.T) { | ||
b := file.FileBringer() | ||
|
||
d := t.TempDir() | ||
p := filepath.Join(d, "foo") | ||
data := []byte("bar") | ||
err := os.WriteFile(p, data, 0o644) | ||
require.NoError(t, err) | ||
|
||
t.Run("absolute path without schema", func(t *testing.T) { | ||
require := require.New(t) | ||
|
||
u, err := url.Parse(p) | ||
if err != nil { | ||
require.NoError(err) | ||
} | ||
|
||
f, err := b.Bring(context.Background(), thing.Thing{Url: *u}) | ||
if err == nil { | ||
defer f.Close() | ||
} | ||
require.NoError(err) | ||
|
||
v, err := io.ReadAll(f) | ||
require.NoError(err) | ||
require.Equal(v, data) | ||
}) | ||
t.Run("absolute path with schema", func(t *testing.T) { | ||
require := require.New(t) | ||
|
||
u, err := url.Parse(fmt.Sprintf("file://%s", p)) | ||
if err != nil { | ||
require.NoError(err) | ||
} | ||
|
||
f, err := b.Bring(context.Background(), thing.Thing{Url: *u}) | ||
if err == nil { | ||
defer f.Close() | ||
} | ||
require.NoError(err) | ||
|
||
v, err := io.ReadAll(f) | ||
require.NoError(err) | ||
require.Equal(v, data) | ||
}) | ||
t.Run("relative path", func(t *testing.T) { | ||
require := require.New(t) | ||
|
||
wd, err := os.Getwd() | ||
if err != nil { | ||
panic(err) | ||
} | ||
if err := os.Chdir(d); err != nil { | ||
panic(err) | ||
} | ||
defer os.Chdir(wd) | ||
|
||
u, err := url.Parse("./foo") | ||
if err != nil { | ||
require.NoError(err) | ||
} | ||
|
||
f, err := b.Bring(context.Background(), thing.Thing{Url: *u}) | ||
if err == nil { | ||
defer f.Close() | ||
} | ||
require.NoError(err) | ||
|
||
v, err := io.ReadAll(f) | ||
require.NoError(err) | ||
require.Equal(v, data) | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
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,91 @@ | ||
package http | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"log/slog" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/lesomnus/bring/bringer" | ||
"github.com/lesomnus/bring/log" | ||
"github.com/lesomnus/bring/thing" | ||
) | ||
|
||
type br struct { | ||
client http.Client | ||
} | ||
|
||
func (b *br) apply(opts []bringer.Option) { | ||
for _, opt := range opts { | ||
switch o := opt.(type) { | ||
case *transportOption: | ||
b.client.Transport = o.Value | ||
} | ||
} | ||
} | ||
|
||
func (b *br) with(opts []bringer.Option) *br { | ||
if len(opts) == 0 { | ||
return b | ||
} | ||
|
||
b_ := *b | ||
b_.apply(opts) | ||
return &b_ | ||
} | ||
|
||
func HttpBringer(opts ...bringer.Option) bringer.Bringer { | ||
b := &br{} | ||
b.apply(opts) | ||
return b | ||
} | ||
|
||
func (b *br) Bring(ctx context.Context, t thing.Thing, opts ...bringer.Option) (io.ReadCloser, error) { | ||
l := log.From(ctx).With(slog.String("bringer", "http")) | ||
b = b.with(opts) | ||
|
||
// TODO: check ETag, Cache-Control, of Last-Modified header. | ||
// res, err := http.Head(t.Url.String()) | ||
// if err != nil { | ||
// return nil, fmt.Errorf("request head: %w", err) | ||
// } | ||
|
||
// TODO: do not buffer the response in the memory according to the config. | ||
// f, err := os.CreateTemp("", "bring-") | ||
// if err != nil { | ||
// return nil, fmt.Errorf("create temp file: %w", err) | ||
// } | ||
// defer f.Close() | ||
|
||
l.Info("request GET") | ||
res, err := b.client.Get(t.Url.String()) | ||
if err != nil { | ||
e := &url.Error{} | ||
if errors.As(err, &e) { | ||
return nil, err | ||
} | ||
return nil, fmt.Errorf("request GET: %w", err) | ||
} | ||
l.Info("response", slog.Int("status", res.StatusCode)) | ||
if l.Enabled(ctx, slog.LevelDebug) { | ||
l.Debug("response", slog.String("header", fmt.Sprintf("%v", res.Header))) | ||
} | ||
if res.StatusCode != http.StatusOK { | ||
if l.Enabled(ctx, slog.LevelDebug) { | ||
if data, err := io.ReadAll(res.Body); err == nil { | ||
l.Debug("response", slog.String("body", string(data))) | ||
} | ||
} | ||
return nil, fmt.Errorf("status not 200: %d", res.StatusCode) | ||
} | ||
|
||
return res.Body, nil | ||
} | ||
|
||
func init() { | ||
bringer.Register("http", HttpBringer) | ||
bringer.Register("https", HttpBringer) | ||
} |
Oops, something went wrong.