Browse Source

envexp: drop package in favour of asciigoat.org/parsers/shexp

Signed-off-by: Alejandro Mery <amery@jpi.io>
v0.2.x v0.1.0
Alejandro Mery 3 years ago
parent
commit
7828f8d92f
  1. 39
      envexp/expand.go
  2. 39
      envexp/global.go
  3. 64
      envexp/reader.go

39
envexp/expand.go

@ -1,39 +0,0 @@
// Package envexp provides environment variable expansion using sh syntax
package envexp
import (
"os"
)
// A VarGetter is a function that for a given paramenter
// returns a string representing its value
type VarGetter func(key string) string
// A Expander is an object that you can use to Expand strings
type Expander struct {
get VarGetter
}
// NewExpander allocates a new Expander using a
// given VarGetter, or os.Getenv if none is given
func NewExpander(f VarGetter) *Expander {
if f == nil {
f = os.Getenv
}
return &Expander{
get: f,
}
}
// Get resolves a variable name as the given Expander
// would do
func (exp *Expander) Get(key string) string {
return exp.get(key)
}
// Expand expands a string using the VarGetter
// defined for the given Expander
func (exp *Expander) Expand(s string) string {
return s
}

39
envexp/global.go

@ -1,39 +0,0 @@
package envexp
import (
"io"
"os"
)
var envExpander = &Expander{
get: os.Getenv,
}
// Expand uses os.GetEnv() to expand a given string
func Expand(s string) string {
return envExpander.Expand(s)
}
// NewReaderSize creates a new Reader wrapper with a given buffer size
// using os.GetEnv() for expanding
func NewReaderSize(in io.Reader, size int) *Reader {
return envExpander.NewReaderSize(in, size)
}
// NewReader creates a new Reader wrapper
// using os.GetEnv() for expanding
func NewReader(in io.Reader) *Reader {
return envExpander.NewReader(in)
}
// NewReaderFileSize creates a new Reader for a file
// with a given buffer size using os.GetEnv() for expanding
func NewReaderFileSize(filename string, size int) (*Reader, error) {
return envExpander.NewReaderFileSize(filename, size)
}
// NewReaderFile creates a new Reader for a file
// using os.GetEnv() for expanding
func NewReaderFile(filename string) (*Reader, error) {
return envExpander.NewReaderFile(filename)
}

64
envexp/reader.go

@ -1,64 +0,0 @@
package envexp
import (
"bytes"
"io"
"os"
)
const (
MinimumBufferSize = 32
DefaultBufferSize = 4096
)
type Reader struct {
exp *Expander
in io.Reader
buf *bytes.Buffer
out *bytes.Buffer
}
func (rd *Reader) Read(b []byte) (int, error) {
return rd.in.Read(b)
}
func (rd *Reader) Close() error {
if f, ok := rd.in.(io.Closer); ok {
return f.Close()
}
return nil
}
//
// Constructors
//
func (exp *Expander) NewReaderSize(in io.Reader, size int) *Reader {
if size < MinimumBufferSize {
size = DefaultBufferSize
}
return &Reader{
exp: exp,
in: in,
buf: bytes.NewBuffer(make([]byte, 0, size)),
out: bytes.NewBuffer(make([]byte, 0, size)),
}
}
func (exp *Expander) NewReader(in io.Reader) *Reader {
return exp.NewReaderSize(in, DefaultBufferSize)
}
func (exp *Expander) NewReaderFileSize(filename string, size int) (*Reader, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
rd := exp.NewReaderSize(f, size)
return rd, nil
}
func (exp *Expander) NewReaderFile(filename string) (*Reader, error) {
return exp.NewReaderFileSize(filename, DefaultBufferSize)
}
Loading…
Cancel
Save