-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuio.go
38 lines (32 loc) · 838 Bytes
/
uio.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package uio
import (
"fmt"
"io"
"log/slog"
"net/url"
"os"
"github.com/WangYihang/uio/pkg/provider"
)
func Open(uri string) (io.ReadWriteCloser, error) {
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.LevelDebug,
}))
logger.Info("Opening resource", slog.String("uri", uri))
u, err := url.Parse(uri)
if err != nil {
logger.Error("Invalid URI", slog.String("error", err.Error()))
return nil, fmt.Errorf("invalid URI: %w", err)
}
logger.Info("Parsed URI",
slog.String("scheme", u.Scheme),
slog.String("host", u.Host),
slog.String("path", u.Path),
slog.String("query", u.RawQuery),
)
if f, ok := provider.SchemaMap[u.Scheme]; ok {
return f(u, logger)
} else {
logger.Error("Fallback to file", slog.String("file", "stdio"))
return provider.OpenFile(u, logger)
}
}