envexp: turn Expander from interface to struct

Signed-off-by: Alejandro Mery <amery@jpi.io>
This commit is contained in:
2021-06-26 20:34:39 +01:00
parent 607fdb6ee4
commit 3e2356a3f6
2 changed files with 6 additions and 10 deletions
+5 -9
View File
@@ -10,34 +10,30 @@ import (
type VarGetter func(key string) string type VarGetter func(key string) string
// A Expander is an object that you can use to Expand strings // A Expander is an object that you can use to Expand strings
type Expander interface { type Expander struct {
Expand(s string) string
}
type expander struct {
get VarGetter get VarGetter
} }
// NewExpander allocates a new Expander using a // NewExpander allocates a new Expander using a
// given VarGetter, or os.Getenv if none is given // given VarGetter, or os.Getenv if none is given
func NewExpander(f VarGetter) Expander { func NewExpander(f VarGetter) *Expander {
if f == nil { if f == nil {
f = os.Getenv f = os.Getenv
} }
return &expander{ return &Expander{
get: f, get: f,
} }
} }
// Get resolves a variable name as the given Expander // Get resolves a variable name as the given Expander
// would do // would do
func (exp *expander) Get(key string) string { func (exp *Expander) Get(key string) string {
return exp.get(key) return exp.get(key)
} }
// Expand expands a string using the VarGetter // Expand expands a string using the VarGetter
// defined for the given Expander // defined for the given Expander
func (exp *expander) Expand(s string) string { func (exp *Expander) Expand(s string) string {
return s return s
} }
+1 -1
View File
@@ -4,7 +4,7 @@ import (
"os" "os"
) )
var envExpander = &expander{ var envExpander = &Expander{
get: os.Getenv, get: os.Getenv,
} }