Merge pull request #8 from sundowndev/feat/string

Use default Go string conversion
pull/13/head v1.2.0
Raphaël 2020-06-15 11:06:01 +02:00 committed by GitHub
commit 0a517b584c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 81 additions and 55 deletions

View File

@ -43,11 +43,11 @@ package main
import "github.com/sundowndev/dorkgen"
func main() {
dork := &dorkgen.GoogleSearch{}
// dork := &dorkgen.DuckDuckGo{}
// dork := &dorkgen.Bing{}
dork := dorkgen.NewGoogleSearch()
// dork := dorkgen.NewDuckDuckGo()
// dork := dorkgen.NewBing()
dork.Site("example.com").Intext("text").ToString()
dork.Site("example.com").Intext("text").String()
// returns: site:example.com intext:"text"
}
```
@ -56,10 +56,10 @@ func main() {
```go
func main() {
dork.Site("facebook.com").Or().Site("twitter.com").ToString()
dork.Site("facebook.com").Or().Site("twitter.com").String()
// returns: site:facebook.com OR site:twitter.com
dork.Intext("facebook").And().Intext("twitter").ToString()
dork.Intext("facebook").And().Intext("twitter").String()
// returns: intext:"facebook" AND intext:"twitter"
}
```
@ -69,9 +69,9 @@ func main() {
```go
func main() {
dork.
Exclude((&dorkgen.GoogleSearch{}).
Exclude((dorkgen.NewGoogleSearch()).
Site("example.com").
ToString()).
String()).
Site("example.*").
Or().
Intext("text")
@ -84,13 +84,12 @@ func main() {
```go
func main() {
dork.
Group((&dorkgen.GoogleSearch{}).
Group((dorkgen.NewGoogleSearch()).
Site("facebook.com").
Or().
Site("twitter.com").
ToString()).
Site("twitter.com")).
Intext("wtf").
ToString()
String()
// returns: (site:facebook.com OR site:twitter.com) "wtf"
}
```
@ -101,11 +100,11 @@ func main() {
func main() {
dork.
Site("facebook.*").
Exclude((&dorkgen.GoogleSearch{}).
Exclude((dorkgen.NewGoogleSearch()).
Site("facebook.com").
ToString())
String())
dork.ToString()
dork.String()
// returns: site:facebook.* -site:facebook.com
dork.ToURL()
// returns: https://www.google.com/search?q=site%3Afacebook.%2A+-site%3Afacebook.com

24
doc.go
View File

@ -8,12 +8,28 @@ package main
import "github.com/sundowndev/dorkgen"
func main() {
dork := &dorkgen.GoogleSearch{}
// dork := &dorkgen.DuckDuckGo{}
// dork := &dorkgen.Bing{}
dork := dorkgen.NewGoogleSearch()
// dork := dorkgen.NewDuckDuckGo()
// dork := dorkgen.NewBing()
dork.Site("example.com").Intext("text").ToString()
dork.Site("example.com").Intext("text").String()
// returns: site:example.com "text"
}
// You can also isolate tags between parentheses
func main() {
dork := dorkgen.NewGoogleSearch()
dork.Group(
dorkgen.NewGoogleSearch().
Site("facebook.com")).
Or().
Group(
dorkgen.NewGoogleSearch().
Site("twitter.com")).
Intext("text")
String()
// returns: (site:facebook.com) OR (site:twitter.com) "text"
}
*/
package dorkgen

6
go.sum
View File

@ -3,11 +3,9 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -23,8 +23,12 @@ type GoogleSearch struct {
EngineFactory
}
// ToString converts all tags to a single request
func (e *GoogleSearch) ToString() string {
func NewGoogleSearch() *GoogleSearch {
return &GoogleSearch{}
}
// String converts all tags to a single request
func (e *GoogleSearch) String() string {
return strings.Join(e.tags, " ")
}
@ -96,9 +100,9 @@ func (e *GoogleSearch) Exclude(value string) *GoogleSearch {
return e
}
// Group isolate tags between parentheses.
func (e *GoogleSearch) Group(value string) *GoogleSearch {
e.tags = append(e.tags, "("+value+")")
// Group isolate tags between parentheses
func (e *GoogleSearch) Group(tags *GoogleSearch) *GoogleSearch {
e.tags = append(e.tags, "("+tags.String()+")")
return e
}

View File

@ -1,18 +1,19 @@
package dorkgen
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
assertion "github.com/stretchr/testify/assert"
)
var dork *GoogleSearch
func TestInit(t *testing.T) {
assert := assert.New(t)
assert := assertion.New(t)
t.Run("should convert to URL correctly", func(t *testing.T) {
dork = &GoogleSearch{}
dork = NewGoogleSearch()
result := dork.
Site("example.com").
@ -21,120 +22,128 @@ func TestInit(t *testing.T) {
assert.Equal(result, "https://www.google.com/search?q=site%3Aexample.com", "they should be equal")
})
t.Run("should convert to string correctly", func(t *testing.T) {
dork = NewGoogleSearch()
result := fmt.Sprint(dork.Site("example.com"))
assert.Equal(result, "site:example.com", "they should be equal")
})
t.Run("should handle site tag correctly", func(t *testing.T) {
dork = &GoogleSearch{}
dork = NewGoogleSearch()
result := dork.
Site("example.com").
ToString()
String()
assert.Equal(result, "site:example.com", "they should be equal")
})
t.Run("should handle intext tag correctly", func(t *testing.T) {
dork = &GoogleSearch{}
dork = NewGoogleSearch()
result := dork.
Intext("text").
ToString()
String()
assert.Equal(result, "intext:\"text\"", "they should be equal")
})
t.Run("should handle inurl tag correctly", func(t *testing.T) {
dork = &GoogleSearch{}
dork = NewGoogleSearch()
result := dork.
Inurl("index.php").
ToString()
String()
assert.Equal(result, "inurl:\"index.php\"", "they should be equal")
})
t.Run("should handle filetype tag correctly", func(t *testing.T) {
dork = &GoogleSearch{}
dork = NewGoogleSearch()
result := dork.
Filetype("pdf").
ToString()
String()
assert.Equal(result, "filetype:\"pdf\"", "they should be equal")
})
t.Run("should handle cache tag correctly", func(t *testing.T) {
dork = &GoogleSearch{}
dork = NewGoogleSearch()
result := dork.
Cache("www.google.com").
ToString()
String()
assert.Equal(result, "cache:\"www.google.com\"", "they should be equal")
})
t.Run("should handle related tag correctly", func(t *testing.T) {
dork = &GoogleSearch{}
dork = NewGoogleSearch()
result := dork.
Related("www.google.com").
ToString()
String()
assert.Equal(result, "related:\"www.google.com\"", "they should be equal")
})
t.Run("should handle ext tag correctly", func(t *testing.T) {
dork = &GoogleSearch{}
dork = NewGoogleSearch()
result := dork.
Ext("(doc | pdf | xls | txt | xml)").
ToString()
String()
assert.Equal(result, "ext:(doc | pdf | xls | txt | xml)", "they should be equal")
})
t.Run("should handle exclude tag correctly", func(t *testing.T) {
dork = &GoogleSearch{}
dork = NewGoogleSearch()
result := dork.
Exclude("html").
Exclude("htm").
Exclude("php").
Exclude("md5sums").
ToString()
String()
assert.Equal(result, "-html -htm -php -md5sums", "they should be equal")
})
t.Run("should handle or tag correctly", func(t *testing.T) {
dork = &GoogleSearch{}
dork = NewGoogleSearch()
result := dork.
Site("facebook.com").
Or().
Site("twitter.com").
ToString()
String()
assert.Equal(result, "site:facebook.com OR site:twitter.com", "they should be equal")
})
t.Run("should handle group tag correctly", func(t *testing.T) {
dork = &GoogleSearch{}
dork = NewGoogleSearch()
result := dork.
Site("linkedin.com").
Group((&GoogleSearch{}).Intext("1").Or().Intext("2").ToString()).
ToString()
Group((NewGoogleSearch()).Intext("1").Or().Intext("2")).
String()
assert.Equal(result, "site:linkedin.com (intext:\"1\" OR intext:\"2\")", "they should be equal")
})
t.Run("should handle group tag correctly", func(t *testing.T) {
dork = &GoogleSearch{}
dork = NewGoogleSearch()
result := dork.
Site("linkedin.com").
Group((&GoogleSearch{}).Intext("1").Or().Intext("2").ToString()).
Group((NewGoogleSearch()).Intext("1").Or().Intext("2")).
Intitle("jordan").
ToString()
String()
assert.Equal(result, "site:linkedin.com (intext:\"1\" OR intext:\"2\") intitle:\"jordan\"", "they should be equal")
})