Skip to content

Commit 405ad8d

Browse files
authored
Merge pull request #6 from dnnrly/feature/formatting
Feature/formatting
2 parents be98d0c + 02ee729 commit 405ad8d

File tree

6 files changed

+90
-7
lines changed

6 files changed

+90
-7
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
This is a handy little helper that puts HTTP reference at your fingertips, when you're on the CLI, when you need it.
44

5+
6+
[![godoc](https://godoc.org/github.com/dnnrly/httpref?status.svg)](http://godoc.org/github.com/dnnrly/httpref)
7+
[![report card](https://goreportcard.com/badge/github.com/dnnrly/httpref)](https://goreportcard.com/report/github.com/dnnrly/httpref)
8+
59
## But why?
610

711
Because I can **never** remember what the bloody http status codes mean, or the details of methods, or whatever. It's annoying!

cmd/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ func printResults(results httpref.References) {
6868
fmt.Fprintf(os.Stderr, "Filter not found any results\n")
6969
os.Exit(1)
7070
case 1:
71-
fmt.Printf("%s - %s\n\n%s\n", results[0].Name, results[0].Summary, results[0].Description)
71+
fmt.Printf("%s\n", results[0].Describe())
7272
default:
7373
for _, r := range results {
74-
fmt.Printf("\t%s\t%s\n", r.Name, r.Summary)
74+
fmt.Printf("%s\n", r.Summarize())
7575
}
7676
}
7777
}

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ module github.com/dnnrly/httpref
22

33
go 1.13
44

5-
require github.com/spf13/cobra v0.0.5
5+
require (
6+
github.com/dnnrly/paragraphical v0.1.1
7+
github.com/spf13/cobra v0.0.5
8+
github.com/stretchr/testify v1.2.2
9+
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc
44
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
55
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
66
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
7+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
78
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
9+
github.com/dnnrly/paragraphical v0.1.1 h1:2aU1GudYxREsGr38muAO7cFdTcDnJ1o5XePQhaurKvk=
10+
github.com/dnnrly/paragraphical v0.1.1/go.mod h1:8bqT0aKyYRBRNWolMb6pYRzi0oe48Y28zCru9ZE5Jdk=
811
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
912
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
1013
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
@@ -13,6 +16,7 @@ github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czP
1316
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
1417
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
1518
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
19+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1620
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1721
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
1822
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
@@ -23,7 +27,9 @@ github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb6
2327
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
2428
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
2529
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
30+
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
2631
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
32+
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
2733
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
2834
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
2935
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=

httpref.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
package httpref
22

3-
import "strings"
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
para "github.com/dnnrly/paragraphical"
8+
)
9+
10+
const (
11+
limit = 100
12+
)
413

514
type Reference struct {
615
Name string
@@ -11,6 +20,24 @@ type Reference struct {
1120

1221
type References []Reference
1322

23+
func (r Reference) Summarize() string {
24+
return para.Format(
25+
limit,
26+
fmt.Sprintf("%s\n %s", r.Name, r.Summary),
27+
)
28+
}
29+
30+
func (r Reference) Describe() string {
31+
text := fmt.Sprintf(
32+
"%s\n %s\n\n%s",
33+
r.Name,
34+
r.Summary,
35+
r.Description,
36+
)
37+
38+
return para.Format(limit, text)
39+
}
40+
1441
func (r References) ByName(code string) References {
1542
results := References{}
1643

httpref_test.go

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package httpref
22

33
import (
4+
"strings"
45
"testing"
5-
)
66

7-
func Test_Anything(t *testing.T) {
8-
}
7+
"github.com/stretchr/testify/assert"
8+
)
99

1010
func TestReferences_ByName(t *testing.T) {
1111
Statuses = append(Statuses, Reference{Name: "501-extended"})
@@ -30,3 +30,45 @@ func TestReferences_ByName(t *testing.T) {
3030
})
3131
}
3232
}
33+
34+
func TestReference_SummarizeContainsCorrectParts(t *testing.T) {
35+
r := Reference{
36+
Name: "name",
37+
Summary: "summary",
38+
Description: "description",
39+
}
40+
41+
s := r.Summarize()
42+
assert.Contains(t, s, "name")
43+
assert.Contains(t, s, "summary")
44+
}
45+
46+
func TestReference_SummarizeLimitsLineLength(t *testing.T) {
47+
r := Reference{
48+
Name: "title name",
49+
IsTitle: true,
50+
Summary: "this is an extremely long line sfasfasdfsd werasasg asfgsdfgdsf sdfgdfs sdfg dsfg dsfg dsfg sdfg sfg a rwr sdfg sdfdffb sdfg dsg sfg dfsg sd",
51+
Description: "title description",
52+
}
53+
54+
s := r.Summarize()
55+
for i, line := range strings.Split(s, "\n") {
56+
assert.True(t, len(line) < 100, "line %d is length %d - '%s'", i, len(line), line)
57+
}
58+
}
59+
60+
func TestReference_DescribeLimitsLength(t *testing.T) {
61+
r := Headers.ByName("Headers")[0]
62+
description := r.Describe()
63+
64+
assert.Contains(t, description, "HTTP")
65+
assert.Contains(t, description, "apply")
66+
for i, line := range strings.Split(description, "\n") {
67+
assert.True(t, len(line) < 100, "line %d is length %d - '%s'", i, len(line), line)
68+
}
69+
}
70+
71+
func TestReferences_Titles(t *testing.T) {
72+
n := Statuses.Titles()
73+
assert.Equal(t, 5, len(n))
74+
}

0 commit comments

Comments
 (0)