Skip to content

Commit

Permalink
Merge pull request #14 from dnnrly/feature/ports
Browse files Browse the repository at this point in the history
Feature/ports
  • Loading branch information
dnnrly authored Aug 7, 2020
2 parents cf4f496 + f9789d4 commit 060c4c0
Show file tree
Hide file tree
Showing 9 changed files with 15,626 additions and 604 deletions.
36 changes: 33 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
)

var (
titles = false
width = 100
titles = false
width = 100
)

// rootCmd represents the base command when ctitlesed without any subcommands
Expand All @@ -25,7 +25,9 @@ var rootCmd = &cobra.Command{
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:
httpref 'Accept*'
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.`),
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.
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.`),
RunE: root,
}

Expand All @@ -45,6 +47,12 @@ func init() {
rootCmd.AddCommand(subCmd("methods", "method", httpref.Methods))
rootCmd.AddCommand(subCmd("statuses", "status", httpref.Statuses))
rootCmd.AddCommand(subCmd("headers", "header", httpref.Headers))
rootCmd.AddCommand(&cobra.Command{
Use: fmt.Sprintf("%s [filter]", "ports"),
Aliases: []string{"port"},
Short: "References for common ports",
Run: portsReference(),
})
}

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

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

printResults(results)
}
}

func portsReference() func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
ref := append(httpref.WellKnownPorts, httpref.RegisteredPorts...)
var results httpref.References

if len(args) == 0 {
results = ref.Titles()
} else {
results = ref.ByName(args[0])

if len(results) == 0 {
results = ref.InRange(args[0])
}
}


printResults(results)
}
}
1 change: 1 addition & 0 deletions headers.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package httpref

// Headers is the list of all known standard HTTP headers
var Headers = References{
{
Name: "Headers",
Expand Down
625 changes: 24 additions & 601 deletions httpref.go

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions httpref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ func TestReferences_ByName(t *testing.T) {
}
}

func TestReferences_InRange(t *testing.T) {
type args struct {
code string
}
tests := []string{
"19150",
"16406",
"5988",
"5989",
}

for _, tt := range tests {
t.Run(tt, func(t *testing.T) {
if got := RegisteredPorts.InRange(tt); len(got) != 1 {
t.Errorf("References.InRange() = %v, want %v", len(got), 1)
}
})
}
}

func TestReference_SummarizeContainsCorrectParts(t *testing.T) {
r := Reference{
Name: "name",
Expand Down
1 change: 1 addition & 0 deletions methods.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package httpref

// Methods represents all of the defined HTTP methods
var Methods = References{
{
Name: "Methods",
Expand Down
Loading

0 comments on commit 060c4c0

Please sign in to comment.