Adding a hostname flag option for use with gh api

This commit is contained in:
wilso199 2020-10-01 14:49:00 -04:00 committed by GitHub
parent 4b395c5dd2
commit 3ecb9de1a7
5 changed files with 105 additions and 14 deletions

View file

@ -1,6 +1,7 @@
package utils
import (
"errors"
"fmt"
"io"
"net/url"
@ -28,6 +29,21 @@ func OpenInBrowser(url string) error {
return err
}
func HostnameValidator(v interface{}) error {
hostname, valid := v.(string)
if !valid {
return errors.New("hostname is not a string")
}
if len(strings.TrimSpace(hostname)) < 1 {
return errors.New("a value is required")
}
if strings.ContainsRune(hostname, '/') || strings.ContainsRune(hostname, ':') {
return errors.New("invalid hostname")
}
return nil
}
func Pluralize(num int, thing string) string {
if num == 1 {
return fmt.Sprintf("%d %s", num, thing)

View file

@ -3,6 +3,8 @@ package utils
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestFuzzyAgo(t *testing.T) {
@ -36,3 +38,48 @@ func TestFuzzyAgo(t *testing.T) {
}
}
}
func TestHostnameValidator(t *testing.T) {
tests := []struct {
name string
input interface{}
wantsErr bool
}{
{
name: "valid hostname",
input: "internal.instance",
wantsErr: false,
},
{
name: "hostname with slashes",
input: "//internal.instance",
wantsErr: true,
},
{
name: "empty hostname",
input: " ",
wantsErr: true,
},
{
name: "hostname with colon",
input: "internal.instance:2205",
wantsErr: true,
},
{
name: "non-string hostname",
input: 62,
wantsErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := HostnameValidator(tt.input)
if tt.wantsErr {
assert.Error(t, err)
return
}
assert.Equal(t, nil, err)
})
}
}