Skip to content

Commit cd6f3ca

Browse files
authored
Merge pull request #19 from jesselang/feature/full-text-search
Full-text Search
2 parents f0d1b21 + 2610c8c commit cd6f3ca

File tree

6 files changed

+110
-32
lines changed

6 files changed

+110
-32
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ Please note we have a code of conduct, please follow it in all your interactions
77
## Pull Request Process
88

99
1. Ensure any install or build dependencies are removed before the end of the layer when doing a build.
10-
2. Update the README.md with detafils of changes to the interface, this includes new environment variables, exposed ports, useful file locations and container parameters.
10+
2. Update the README.md with details of changes to the interface, this includes new environment variables, exposed ports, useful file locations and container parameters.
1111
3. Increase the version numbers in any examples files and the README.md to the new version that this Pull Request would represent. The versioning scheme we use is [SemVer](https://semver.org).
1212
4. You may merge the Pull Request in once you have the sign-off of two other developers, or if you do not have permission to do that, you may request the second reviewer to merge it for you.

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ $ go get github.com/dnnrly/httpref/cmd/httpref
3434

3535
# Usage
3636

37+
## Filter by Title
38+
3739
```
3840
$ httpref 1
3941
1xx Informational response
4042
100 Continue
4143
101 Switching
4244
102 Processing
4345
103 Early hints
44-
46+
4547
4648
$ httpref 200
4749
200 - OK
@@ -60,6 +62,21 @@ The successful result of a PUT or a DELETE is often not a 200 OK but a 204 No Co
6062
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/200
6163
```
6264

65+
## Full-text Search
66+
67+
The `--search` option accepts a term to full-text search. This option is available for header, method, and status references.
68+
69+
```
70+
$ httpref --search clear
71+
Clear-Site-Data
72+
Clears browsing data (e.g. cookies, storage, cache) associated with the requesting
73+
website.
74+
205
75+
Reset Content
76+
431
77+
Request Header Fields Too Large
78+
79+
6380
## Important `make` targets
6481
6582
* `deps` - downloads all of the deps you need to build, test, and release

cmd/root.go

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,29 @@ import (
1111
)
1212

1313
var (
14-
titles = false
15-
width = 100
14+
titles = false
15+
width = 100
16+
searchTerm = ""
1617
)
1718

18-
// rootCmd represents the base command when ctitlesed without any subcommands
19+
// rootCmd represents the base command when called without any subcommands
1920
var rootCmd = &cobra.Command{
2021
Use: "httpref [filter]",
2122
Args: cobra.MaximumNArgs(1),
2223
Short: "Command line access to HTTP references",
2324
Long: paragraphical.Format(width, `This displays useful information related to HTTP.
2425
25-
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:
26+
It will prefer exact matches where there are multiple 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:
2627
httpref 'Accept*'
2728
2829
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.
2930
3031
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.`),
31-
RunE: root,
32+
Run: root,
3233
}
3334

3435
// Execute adds titles child commands to the root command and sets flags appropriately.
35-
// This is ctitlesed by main.main(). It only needs to happen once to the rootCmd.
36+
// This is called by main.main(). It only needs to happen once to the rootCmd.
3637
func Execute() {
3738
if err := rootCmd.Execute(); err != nil {
3839
fmt.Println(err)
@@ -43,6 +44,7 @@ func Execute() {
4344
func init() {
4445
rootCmd.PersistentFlags().BoolVarP(&titles, "titles", "t", titles, "List titles of the summaries available")
4546
rootCmd.PersistentFlags().IntVarP(&width, "width", "w", width, "Width to fit the output to")
47+
rootCmd.PersistentFlags().StringVarP(&searchTerm, "search", "", searchTerm, "Full-text search term")
4648

4749
rootCmd.AddCommand(subCmd("methods", "method", httpref.Methods))
4850
rootCmd.AddCommand(subCmd("statuses", "status", httpref.Statuses))
@@ -64,26 +66,35 @@ func subCmd(name, alias string, ref httpref.References) *cobra.Command {
6466
}
6567
}
6668

67-
func root(cmd *cobra.Command, args []string) error {
68-
results := append(httpref.Statuses.Titles(), httpref.Headers.Titles()...)
69-
results = append(results, httpref.Methods.Titles()...)
70-
results = append(results, httpref.WellKnownPorts.Titles()...)
71-
results = append(results, httpref.RegisteredPorts.Titles()...)
69+
func root(cmd *cobra.Command, args []string) {
70+
if titles {
71+
results := append(httpref.Statuses.Titles(), httpref.Headers.Titles()...)
72+
results = append(results, httpref.Methods.Titles()...)
73+
results = append(results, httpref.WellKnownPorts.Titles()...)
74+
results = append(results, httpref.RegisteredPorts.Titles()...)
7275

73-
if !titles {
74-
if len(args) == 0 {
75-
fmt.Fprintf(os.Stderr, "Must specify something to filter by\n")
76-
os.Exit(1)
77-
} else {
78-
results = append(httpref.Statuses, httpref.Headers...)
79-
results = append(results, httpref.Methods...)
80-
results = results.ByName(args[0])
81-
}
76+
printResults(results)
77+
78+
return
8279
}
8380

84-
printResults(results)
81+
if searchTerm == "" && len(args) == 0 {
82+
fmt.Fprintf(os.Stderr, "Must specify something to filter by\n")
83+
os.Exit(1)
84+
}
85+
86+
results := append(httpref.Headers, httpref.Methods...)
87+
results = append(results, httpref.Statuses...)
88+
89+
if searchTerm != "" {
90+
results = results.Search(searchTerm)
91+
printResults(results)
8592

86-
return nil
93+
return
94+
}
95+
96+
results = results.ByName(args[0])
97+
printResults(results)
8798
}
8899

89100
func printResults(results httpref.References) {
@@ -100,36 +111,42 @@ func printResults(results httpref.References) {
100111
}
101112
}
102113

103-
func referenceCmd(ref httpref.References) func(cmd *cobra.Command, args []string) {
114+
func referenceCmd(results httpref.References) func(cmd *cobra.Command, args []string) {
104115
return func(cmd *cobra.Command, args []string) {
105-
results := ref
116+
if searchTerm != "" {
117+
printResults(results.Search(searchTerm))
118+
return
119+
}
106120

107121
if len(args) == 0 {
108-
results = results.Titles()
109-
} else {
110-
results = results.ByName(args[0])
122+
printResults(results.Titles())
123+
return
111124
}
112125

113-
printResults(results)
126+
printResults(results.ByName(args[0]))
114127
}
115128
}
116129

117130
func portsReference() func(cmd *cobra.Command, args []string) {
118131
return func(cmd *cobra.Command, args []string) {
132+
if searchTerm != "" {
133+
fmt.Fprintf(os.Stderr, "error: full-text search not supported\n")
134+
os.Exit(1)
135+
}
136+
119137
ref := append(httpref.WellKnownPorts, httpref.RegisteredPorts...)
120138
var results httpref.References
121139

122140
if len(args) == 0 {
123141
results = ref.Titles()
124142
} else {
125143
results = ref.ByName(args[0])
126-
144+
127145
if len(results) == 0 {
128146
results = ref.InRange(args[0])
129147
}
130148
}
131149

132-
133150
printResults(results)
134151
}
135152
}

httpref.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,17 @@ func (r References) Titles() References {
8787

8888
return results
8989
}
90+
91+
func (r References) Search(term string) References {
92+
results := References{}
93+
94+
for _, v := range r {
95+
if strings.Contains(strings.ToLower(v.Name), term) ||
96+
strings.Contains(strings.ToLower(v.Summary), term) ||
97+
strings.Contains(strings.ToLower(v.Description), term) {
98+
results = append(results, v)
99+
}
100+
}
101+
102+
return results
103+
}

httpref_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,8 @@ func TestPortsConsistencyValidation(t *testing.T) {
111111
}
112112
}
113113
}
114+
115+
func TestReferences_Search(t *testing.T) {
116+
n := Statuses.Search("auth")
117+
assert.Equal(t, 8, len(n))
118+
}

test/basic.bats

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,31 @@ BIN=./httpref
5959
[ "$(echo $output | grep -c '/docs/Web/HTTP/Headers/Accept$')" -eq 1 ]
6060
}
6161

62+
@test "Full-text search option works on root" {
63+
run ${BIN} --search clear
64+
[ $status -eq 0 ]
65+
}
66+
67+
@test "Full-text search option works on headers" {
68+
run ${BIN} headers --search cache
69+
[ $status -eq 0 ]
70+
}
71+
72+
@test "Full-text search option works on methods" {
73+
run ${BIN} methods --search cache
74+
[ $status -eq 0 ]
75+
}
76+
77+
@test "Full-text search option works on statuses" {
78+
run ${BIN} statuses --search cache
79+
[ $status -eq 0 ]
80+
}
81+
82+
@test "Full-text search option does NOT work on ports" {
83+
run ${BIN} ports --search cache
84+
[ $status -eq 1 ]
85+
}
86+
6287
@test "Can change the width of the output" {
6388
run ${BIN} -w 70 100
6489
[ $status -eq 0 ]

0 commit comments

Comments
 (0)