Skip to content

Commit

Permalink
add URL
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathaningram committed Feb 12, 2021
1 parent 4e5f119 commit 849160d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
36 changes: 36 additions & 0 deletions check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,42 @@ func TestIsDialAddr(t *testing.T) {
}
}

func TestURL(t *testing.T) {
tests := []struct {
in string
wantErr bool
}{
// Valid
{"http://localhost", false},
{"http://localhost:1234", false},
{"http://192.168.0.1:1234", false},
{"https://example.com", false},
{"https://example.com/home", false},
{"https://example.com/home?a=b", false},

// Invalid
{"", true},
{"ht tp://foo.com", true}, // invalid character in schema
{"http://a b.com/", true}, // no space in host name please
{"cache_object:foo", true},
}

env.ResetForTesting()
prefix := env.CmdVar.Name()
_ = env.URL("URL", "URL test")
name := strings.ToUpper(prefix) + "_URL"

for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
os.Setenv(name, tt.in)

if err := env.Parse(); (err != nil) != tt.wantErr {
t.Errorf("env.Parse() = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestIsPath(t *testing.T) {
env.ResetForTesting()

Expand Down
39 changes: 39 additions & 0 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package env // import "code.sajari.com/env"
import (
"errors"
"fmt"
"net/url"
"os"
"path"
"strconv"
Expand Down Expand Up @@ -128,6 +129,30 @@ func (v *boolValue) String() string {
return strconv.FormatBool(bool(*v))
}

type urlValue url.URL

func newURLValue(x url.URL, p *url.URL) *urlValue {
*p = x
return (*urlValue)(p)
}

func (v *urlValue) Set(x string) error {
if x == "" {
return errors.New("empty")
}
u, err := url.Parse(x)
if err != nil {
return err
}
*v = urlValue(*u)
return err
}

func (v *urlValue) String() string {
u := url.URL(*v)
return u.String()
}

// NewVarSet creates a new variable set with given name.
//
// If name is non-empty, then all variables will have a strings.ToUpper(name)+"_"
Expand Down Expand Up @@ -249,6 +274,14 @@ func (v *VarSet) DialAddr(name, usage string) *string {
return p
}

// URL defines a string variable with specified name, usage string validated as a URL.
// The return value is the address of a URL variable that stores the value of the variable.
func (v *VarSet) URL(name, usage string) *url.URL {
p := new(url.URL)
v.Var(newURLValue(url.URL{}, p), name, usage)
return p
}

// Path defines a string variable with specified name, usage string validated as a local path.
// The return value is the address of a string variable that stores the value of the variable.
func (v *VarSet) Path(name, usage string) *string {
Expand Down Expand Up @@ -356,6 +389,12 @@ func DialAddr(name, usage string) *string {
return CmdVar.DialAddr(name, usage)
}

// URL defines a string variable with specified name, usage string validated as a URL.
// The return value is the address of a URL variable that stores the value of the variable.
func URL(name, usage string) *url.URL {
return CmdVar.URL(name, usage)
}

// Path defines a string variable with specified name, usage string validated as a
// local path.
// The return value is the address of a string variable that stores the value of the variable.
Expand Down
2 changes: 2 additions & 0 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestAll(t *testing.T) {
env.Int("INT", "int test")
env.BindAddr("LISTEN", "bindaddr test")
env.DialAddr("ADDR", "dialaddr test")
env.URL("URL", "URL test")
env.String("STRING", "string test")
env.Duration("TIMEOUT", "timeout test")

Expand All @@ -28,6 +29,7 @@ func TestAll(t *testing.T) {
"TEST_INT": "1",
"TEST_LISTEN": ":1234",
"TEST_ADDR": "localhost:1234",
"TEST_URL": "http://localhost:1234/api",
"TEST_STRING": "name",
"TEST_TIMEOUT": "1m1s",
}
Expand Down

0 comments on commit 849160d

Please sign in to comment.