Skip to content

Commit 060c4c0

Browse files
authored
Merge pull request #14 from dnnrly/feature/ports
Feature/ports
2 parents cf4f496 + f9789d4 commit 060c4c0

File tree

9 files changed

+15626
-604
lines changed

9 files changed

+15626
-604
lines changed

cmd/root.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
)
1212

1313
var (
14-
titles = false
15-
width = 100
14+
titles = false
15+
width = 100
1616
)
1717

1818
// rootCmd represents the base command when ctitlesed without any subcommands
@@ -25,7 +25,9 @@ var rootCmd = &cobra.Command{
2525
It will prefer exact matches where there are mutliple entries matching the filter (e.g. Accept and Accept-Language). If you want to match everything with the same prefix then you can use * as a wildcard suffix, for example:
2626
httpref 'Accept*'
2727
28-
Most of the content comes from the Mozilla developer documentation (https://developer.mozilla.org/en-US/docs/Web/HTTP) and is copyright Mozilla and individual contributors. See https://developer.mozilla.org/en-US/docs/MDN/About#Copyrights_and_licenses for details.`),
28+
Most of the content comes from the Mozilla developer documentation (https://developer.mozilla.org/en-US/docs/Web/HTTP) and is copyright Mozilla and individual contributors. See https://developer.mozilla.org/en-US/docs/MDN/About#Copyrights_and_licenses for details.
29+
30+
Ports can only be looked up using the 'ports' sub command. You can also look up ports inside a range. Information on ports comes from https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers under the Creative Commons Attribution-ShareAlike License.`),
2931
RunE: root,
3032
}
3133

@@ -45,6 +47,12 @@ func init() {
4547
rootCmd.AddCommand(subCmd("methods", "method", httpref.Methods))
4648
rootCmd.AddCommand(subCmd("statuses", "status", httpref.Statuses))
4749
rootCmd.AddCommand(subCmd("headers", "header", httpref.Headers))
50+
rootCmd.AddCommand(&cobra.Command{
51+
Use: fmt.Sprintf("%s [filter]", "ports"),
52+
Aliases: []string{"port"},
53+
Short: "References for common ports",
54+
Run: portsReference(),
55+
})
4856
}
4957

5058
func subCmd(name, alias string, ref httpref.References) *cobra.Command {
@@ -59,6 +67,8 @@ func subCmd(name, alias string, ref httpref.References) *cobra.Command {
5967
func root(cmd *cobra.Command, args []string) error {
6068
results := append(httpref.Statuses.Titles(), httpref.Headers.Titles()...)
6169
results = append(results, httpref.Methods.Titles()...)
70+
results = append(results, httpref.WellKnownPorts.Titles()...)
71+
results = append(results, httpref.RegisteredPorts.Titles()...)
6272

6373
if !titles {
6474
if len(args) == 0 {
@@ -100,6 +110,26 @@ func referenceCmd(ref httpref.References) func(cmd *cobra.Command, args []string
100110
results = results.ByName(args[0])
101111
}
102112

113+
printResults(results)
114+
}
115+
}
116+
117+
func portsReference() func(cmd *cobra.Command, args []string) {
118+
return func(cmd *cobra.Command, args []string) {
119+
ref := append(httpref.WellKnownPorts, httpref.RegisteredPorts...)
120+
var results httpref.References
121+
122+
if len(args) == 0 {
123+
results = ref.Titles()
124+
} else {
125+
results = ref.ByName(args[0])
126+
127+
if len(results) == 0 {
128+
results = ref.InRange(args[0])
129+
}
130+
}
131+
132+
103133
printResults(results)
104134
}
105135
}

headers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package httpref
22

3+
// Headers is the list of all known standard HTTP headers
34
var Headers = References{
45
{
56
Name: "Headers",

httpref.go

Lines changed: 24 additions & 601 deletions
Large diffs are not rendered by default.

httpref_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,26 @@ func TestReferences_ByName(t *testing.T) {
3131
}
3232
}
3333

34+
func TestReferences_InRange(t *testing.T) {
35+
type args struct {
36+
code string
37+
}
38+
tests := []string{
39+
"19150",
40+
"16406",
41+
"5988",
42+
"5989",
43+
}
44+
45+
for _, tt := range tests {
46+
t.Run(tt, func(t *testing.T) {
47+
if got := RegisteredPorts.InRange(tt); len(got) != 1 {
48+
t.Errorf("References.InRange() = %v, want %v", len(got), 1)
49+
}
50+
})
51+
}
52+
}
53+
3454
func TestReference_SummarizeContainsCorrectParts(t *testing.T) {
3555
r := Reference{
3656
Name: "name",

methods.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package httpref
22

3+
// Methods represents all of the defined HTTP methods
34
var Methods = References{
45
{
56
Name: "Methods",

0 commit comments

Comments
 (0)