Skip to content

Commit

Permalink
Merge pull request #6 from dnnrly/feature/formatting
Browse files Browse the repository at this point in the history
Feature/formatting
  • Loading branch information
dnnrly authored Jan 16, 2020
2 parents be98d0c + 02ee729 commit 405ad8d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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


[![godoc](https://godoc.org/github.com/dnnrly/httpref?status.svg)](http://godoc.org/github.com/dnnrly/httpref)
[![report card](https://goreportcard.com/badge/github.com/dnnrly/httpref)](https://goreportcard.com/report/github.com/dnnrly/httpref)

## But why?

Because I can **never** remember what the bloody http status codes mean, or the details of methods, or whatever. It's annoying!
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ func printResults(results httpref.References) {
fmt.Fprintf(os.Stderr, "Filter not found any results\n")
os.Exit(1)
case 1:
fmt.Printf("%s - %s\n\n%s\n", results[0].Name, results[0].Summary, results[0].Description)
fmt.Printf("%s\n", results[0].Describe())
default:
for _, r := range results {
fmt.Printf("\t%s\t%s\n", r.Name, r.Summary)
fmt.Printf("%s\n", r.Summarize())
}
}
}
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ module github.com/dnnrly/httpref

go 1.13

require github.com/spf13/cobra v0.0.5
require (
github.com/dnnrly/paragraphical v0.1.1
github.com/spf13/cobra v0.0.5
github.com/stretchr/testify v1.2.2
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dnnrly/paragraphical v0.1.1 h1:2aU1GudYxREsGr38muAO7cFdTcDnJ1o5XePQhaurKvk=
github.com/dnnrly/paragraphical v0.1.1/go.mod h1:8bqT0aKyYRBRNWolMb6pYRzi0oe48Y28zCru9ZE5Jdk=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
Expand All @@ -13,6 +16,7 @@ github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czP
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
Expand All @@ -23,7 +27,9 @@ github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb6
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down
29 changes: 28 additions & 1 deletion httpref.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package httpref

import "strings"
import (
"fmt"
"strings"

para "github.com/dnnrly/paragraphical"
)

const (
limit = 100
)

type Reference struct {
Name string
Expand All @@ -11,6 +20,24 @@ type Reference struct {

type References []Reference

func (r Reference) Summarize() string {
return para.Format(
limit,
fmt.Sprintf("%s\n %s", r.Name, r.Summary),
)
}

func (r Reference) Describe() string {
text := fmt.Sprintf(
"%s\n %s\n\n%s",
r.Name,
r.Summary,
r.Description,
)

return para.Format(limit, text)
}

func (r References) ByName(code string) References {
results := References{}

Expand Down
48 changes: 45 additions & 3 deletions httpref_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package httpref

import (
"strings"
"testing"
)

func Test_Anything(t *testing.T) {
}
"github.com/stretchr/testify/assert"
)

func TestReferences_ByName(t *testing.T) {
Statuses = append(Statuses, Reference{Name: "501-extended"})
Expand All @@ -30,3 +30,45 @@ func TestReferences_ByName(t *testing.T) {
})
}
}

func TestReference_SummarizeContainsCorrectParts(t *testing.T) {
r := Reference{
Name: "name",
Summary: "summary",
Description: "description",
}

s := r.Summarize()
assert.Contains(t, s, "name")
assert.Contains(t, s, "summary")
}

func TestReference_SummarizeLimitsLineLength(t *testing.T) {
r := Reference{
Name: "title name",
IsTitle: true,
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",
Description: "title description",
}

s := r.Summarize()
for i, line := range strings.Split(s, "\n") {
assert.True(t, len(line) < 100, "line %d is length %d - '%s'", i, len(line), line)
}
}

func TestReference_DescribeLimitsLength(t *testing.T) {
r := Headers.ByName("Headers")[0]
description := r.Describe()

assert.Contains(t, description, "HTTP")
assert.Contains(t, description, "apply")
for i, line := range strings.Split(description, "\n") {
assert.True(t, len(line) < 100, "line %d is length %d - '%s'", i, len(line), line)
}
}

func TestReferences_Titles(t *testing.T) {
n := Statuses.Titles()
assert.Equal(t, 5, len(n))
}

0 comments on commit 405ad8d

Please sign in to comment.