Matthew Rich
bcf4e768ff
WIP: add support container image build using local filesytem contexts or contextes generated from resource definitions WIP: added support for the create command in the exec resource Fix a type matching error in `types` package use of generics
158 lines
2.7 KiB
Go
158 lines
2.7 KiB
Go
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
package transport
|
|
|
|
import (
|
|
_ "errors"
|
|
_ "fmt"
|
|
"net/url"
|
|
_ "net/http"
|
|
_ "strings"
|
|
_ "path/filepath"
|
|
"io"
|
|
_ "os"
|
|
"context"
|
|
)
|
|
|
|
type Handler interface {
|
|
URI() *url.URL
|
|
ContentType() string
|
|
Gzip() bool
|
|
Signature() string
|
|
Reader() io.ReadCloser
|
|
Writer() io.WriteCloser
|
|
}
|
|
|
|
type Reader struct {
|
|
uri *url.URL
|
|
handle Handler
|
|
stream io.ReadCloser
|
|
exists func() bool
|
|
}
|
|
|
|
func NewReader(u *url.URL) (reader *Reader, e error) {
|
|
ctx := context.Background()
|
|
reader = &Reader{ uri: u }
|
|
switch u.Scheme {
|
|
case "http", "https":
|
|
reader.handle, e = NewHTTP(u, ctx)
|
|
case "file":
|
|
fallthrough
|
|
default:
|
|
reader.handle, e = NewFile(u)
|
|
reader.exists = func() bool { return FileExists(u) }
|
|
}
|
|
reader.SetStream(reader.handle.Reader())
|
|
return
|
|
}
|
|
|
|
func NewReaderURI(uri string) (reader *Reader, e error) {
|
|
var u *url.URL
|
|
if u, e = url.Parse(uri); e == nil {
|
|
return NewReader(u)
|
|
}
|
|
return
|
|
}
|
|
|
|
type Writer struct {
|
|
uri *url.URL
|
|
handle Handler
|
|
stream io.WriteCloser
|
|
exists func() bool
|
|
}
|
|
|
|
func NewWriter(u *url.URL) (writer *Writer, e error) {
|
|
ctx := context.Background()
|
|
writer = &Writer{ uri: u }
|
|
switch u.Scheme {
|
|
case "http", "https":
|
|
writer.handle, e = NewHTTP(u, ctx)
|
|
case "file":
|
|
fallthrough
|
|
default:
|
|
writer.handle, e = NewFile(u)
|
|
writer.exists = func() bool { return FileExists(u) }
|
|
}
|
|
writer.SetStream(writer.handle.Writer())
|
|
return writer, e
|
|
}
|
|
|
|
func NewWriterURI(uri string) (writer *Writer, e error) {
|
|
var u *url.URL
|
|
if u, e = url.Parse(uri); e == nil {
|
|
return NewWriter(u)
|
|
}
|
|
return
|
|
}
|
|
|
|
func ExistsURI(uri string) bool {
|
|
if u, e := url.Parse(uri); e == nil {
|
|
return Exists(u)
|
|
}
|
|
return false
|
|
}
|
|
|
|
func Exists(u *url.URL) bool {
|
|
switch u.Scheme {
|
|
case "http", "https":
|
|
return HTTPExists(u)
|
|
case "file":
|
|
fallthrough
|
|
default:
|
|
return FileExists(u)
|
|
}
|
|
}
|
|
|
|
func (r *Reader) Exists() bool { return r.exists() }
|
|
|
|
func (r *Reader) Read(b []byte) (int, error) {
|
|
return r.stream.Read(b)
|
|
}
|
|
|
|
func (r *Reader) Close() error {
|
|
return r.stream.Close()
|
|
}
|
|
|
|
func (r *Reader) ContentType() string {
|
|
return r.handle.ContentType()
|
|
}
|
|
|
|
func (r *Reader) Gzip() bool {
|
|
return r.handle.Gzip()
|
|
}
|
|
|
|
func (r *Reader) Signature() string {
|
|
return r.handle.Signature()
|
|
}
|
|
|
|
func (r *Reader) SetStream(s io.ReadCloser) {
|
|
r.stream = s
|
|
}
|
|
|
|
|
|
func (w *Writer) Exists() bool { return w.exists() }
|
|
|
|
func (w *Writer) Write(b []byte) (int, error) {
|
|
return w.stream.Write(b)
|
|
}
|
|
|
|
func (w *Writer) Close() error {
|
|
return w.stream.Close()
|
|
}
|
|
|
|
func (w *Writer) ContentType() string {
|
|
return w.handle.ContentType()
|
|
}
|
|
|
|
func (w *Writer) Gzip() bool {
|
|
return w.handle.Gzip()
|
|
}
|
|
|
|
func (w *Writer) Signature() string {
|
|
return w.handle.Signature()
|
|
}
|
|
|
|
func (w *Writer) SetStream(s io.WriteCloser) {
|
|
w.stream = s
|
|
}
|