Skip to content

Commit 460d55f

Browse files
authored
Move HostnameValidator to ghinstance
1 parent 3ecb9de commit 460d55f

6 files changed

Lines changed: 65 additions & 67 deletions

File tree

internal/ghinstance/host.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ghinstance
22

33
import (
4+
"errors"
45
"fmt"
56
"strings"
67
)
@@ -42,6 +43,21 @@ func NormalizeHostname(h string) string {
4243
return hostname
4344
}
4445

46+
func HostnameValidator(v interface{}) error {
47+
hostname, valid := v.(string)
48+
if !valid {
49+
return errors.New("hostname is not a string")
50+
}
51+
52+
if len(strings.TrimSpace(hostname)) < 1 {
53+
return errors.New("a value is required")
54+
}
55+
if strings.ContainsRune(hostname, '/') || strings.ContainsRune(hostname, ':') {
56+
return errors.New("invalid hostname")
57+
}
58+
return nil
59+
}
60+
4561
func GraphQLEndpoint(hostname string) string {
4662
if IsEnterprise(hostname) {
4763
return fmt.Sprintf("https://%s/api/graphql", hostname)

internal/ghinstance/host_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package ghinstance
22

33
import (
44
"testing"
5+
6+
"github.com/stretchr/testify/assert"
57
)
68

79
func TestOverridableDefault(t *testing.T) {
@@ -97,6 +99,50 @@ func TestNormalizeHostname(t *testing.T) {
9799
}
98100
}
99101

102+
func TestHostnameValidator(t *testing.T) {
103+
tests := []struct {
104+
name string
105+
input interface{}
106+
wantsErr bool
107+
}{
108+
{
109+
name: "valid hostname",
110+
input: "internal.instance",
111+
wantsErr: false,
112+
},
113+
{
114+
name: "hostname with slashes",
115+
input: "//internal.instance",
116+
wantsErr: true,
117+
},
118+
{
119+
name: "empty hostname",
120+
input: " ",
121+
wantsErr: true,
122+
},
123+
{
124+
name: "hostname with colon",
125+
input: "internal.instance:2205",
126+
wantsErr: true,
127+
},
128+
{
129+
name: "non-string hostname",
130+
input: 62,
131+
wantsErr: true,
132+
},
133+
}
134+
135+
for _, tt := range tests {
136+
t.Run(tt.name, func(t *testing.T) {
137+
err := HostnameValidator(tt.input)
138+
if tt.wantsErr {
139+
assert.Error(t, err)
140+
return
141+
}
142+
assert.Equal(t, nil, err)
143+
})
144+
}
145+
}
100146
func TestGraphQLEndpoint(t *testing.T) {
101147
tests := []struct {
102148
host string

pkg/cmd/api/api.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/cli/cli/pkg/cmdutil"
2222
"github.com/cli/cli/pkg/iostreams"
2323
"github.com/cli/cli/pkg/jsoncolor"
24-
"github.com/cli/cli/utils"
2524
"github.com/spf13/cobra"
2625
)
2726

@@ -131,7 +130,7 @@ original query accepts an '$endCursor: String' variable and that it fetches the
131130
opts.RequestMethodPassed = c.Flags().Changed("method")
132131

133132
if c.Flags().Changed("hostname") {
134-
if err := utils.HostnameValidator(opts.Hostname); err != nil {
133+
if err := ghinstance.HostnameValidator(opts.Hostname); err != nil {
135134
return &cmdutil.FlagError{Err: fmt.Errorf("error parsing --hostname: %w", err)}
136135
}
137136
}

pkg/cmd/auth/login/login.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func NewCmdLogin(f *cmdutil.Factory, runF func(*LoginOptions) error) *cobra.Comm
8888
}
8989

9090
if cmd.Flags().Changed("hostname") {
91-
if err := utils.HostnameValidator(opts.Hostname); err != nil {
91+
if err := ghinstance.HostnameValidator(opts.Hostname); err != nil {
9292
return &cmdutil.FlagError{Err: fmt.Errorf("error parsing --hostname: %w", err)}
9393
}
9494
}
@@ -166,7 +166,7 @@ func loginRun(opts *LoginOptions) error {
166166
if isEnterprise {
167167
err := prompt.SurveyAskOne(&survey.Input{
168168
Message: "GHE hostname:",
169-
}, &hostname, survey.WithValidator(utils.HostnameValidator))
169+
}, &hostname, survey.WithValidator(ghinstance.HostnameValidator))
170170
if err != nil {
171171
return fmt.Errorf("could not prompt: %w", err)
172172
}

utils/utils.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package utils
22

33
import (
4-
"errors"
54
"fmt"
65
"io"
76
"net/url"
@@ -29,21 +28,6 @@ func OpenInBrowser(url string) error {
2928
return err
3029
}
3130

32-
func HostnameValidator(v interface{}) error {
33-
hostname, valid := v.(string)
34-
if !valid {
35-
return errors.New("hostname is not a string")
36-
}
37-
38-
if len(strings.TrimSpace(hostname)) < 1 {
39-
return errors.New("a value is required")
40-
}
41-
if strings.ContainsRune(hostname, '/') || strings.ContainsRune(hostname, ':') {
42-
return errors.New("invalid hostname")
43-
}
44-
return nil
45-
}
46-
4731
func Pluralize(num int, thing string) string {
4832
if num == 1 {
4933
return fmt.Sprintf("%d %s", num, thing)

utils/utils_test.go

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package utils
33
import (
44
"testing"
55
"time"
6-
7-
"github.com/stretchr/testify/assert"
86
)
97

108
func TestFuzzyAgo(t *testing.T) {
@@ -38,48 +36,3 @@ func TestFuzzyAgo(t *testing.T) {
3836
}
3937
}
4038
}
41-
42-
func TestHostnameValidator(t *testing.T) {
43-
tests := []struct {
44-
name string
45-
input interface{}
46-
wantsErr bool
47-
}{
48-
{
49-
name: "valid hostname",
50-
input: "internal.instance",
51-
wantsErr: false,
52-
},
53-
{
54-
name: "hostname with slashes",
55-
input: "//internal.instance",
56-
wantsErr: true,
57-
},
58-
{
59-
name: "empty hostname",
60-
input: " ",
61-
wantsErr: true,
62-
},
63-
{
64-
name: "hostname with colon",
65-
input: "internal.instance:2205",
66-
wantsErr: true,
67-
},
68-
{
69-
name: "non-string hostname",
70-
input: 62,
71-
wantsErr: true,
72-
},
73-
}
74-
75-
for _, tt := range tests {
76-
t.Run(tt.name, func(t *testing.T) {
77-
err := HostnameValidator(tt.input)
78-
if tt.wantsErr {
79-
assert.Error(t, err)
80-
return
81-
}
82-
assert.Equal(t, nil, err)
83-
})
84-
}
85-
}

0 commit comments

Comments
 (0)