Merge pull request #5 from sundowndev/refact/engineFactory

Export engine factory
pull/6/head v1.0.3
Raphaël 2020-04-25 14:35:43 +01:00 committed by GitHub
commit 9ae2785339
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 34 deletions

1
.gitignore vendored
View File

@ -14,3 +14,4 @@
# Dependency directories (remove the comment below to include it)
# vendor/
.vscode/
.idea

View File

@ -1,17 +1,15 @@
package dorkgen
// engineFactory is the main interface for
// EngineFactory is the main interface for
// search engine implementations.
type engineFactory interface {
Site(string) *GoogleSearch
ToString() string
ToURL() string
Intext(string) *GoogleSearch
Inurl(string) *GoogleSearch
Filetype(string) *GoogleSearch
Cache(string) *GoogleSearch
Related(string) *GoogleSearch
Ext(string) *GoogleSearch
Exclude(string) *GoogleSearch
Group(string) *GoogleSearch
type EngineFactory struct {
tags []string
}
func (e *EngineFactory) concat(tag string, value string, quotes bool) string {
if quotes {
return tag + "\"" + value + "\""
}
return tag + value
}

View File

@ -18,18 +18,9 @@ const (
intextTag = "intext:"
)
// GoogleSearch is the Google implementation for Dorkgen
// GoogleSearch is the Google search implementation for Dorkgen
type GoogleSearch struct {
engineFactory
tags []string
}
func concat(tag string, value string, quotes bool) string {
if quotes {
return tag + "\"" + value + "\""
}
return tag + value
EngineFactory
}
// ToString converts all tags to a single request
@ -53,8 +44,7 @@ func (e *GoogleSearch) ToURL() string {
// Site specifically searches that particular site and lists all the results for that site.
func (e *GoogleSearch) Site(site string) *GoogleSearch {
e.tags = append(e.tags, concat(siteTag, site, false))
e.tags = append(e.tags, e.concat(siteTag, site, false))
return e
}
@ -66,43 +56,43 @@ func (e *GoogleSearch) Or() *GoogleSearch {
// Intext searches for the occurrences of keywords all at once or one at a time.
func (e *GoogleSearch) Intext(text string) *GoogleSearch {
e.tags = append(e.tags, concat(intextTag, text, true))
e.tags = append(e.tags, e.concat(intextTag, text, true))
return e
}
// Inurl searches for a URL matching one of the keywords.
func (e *GoogleSearch) Inurl(url string) *GoogleSearch {
e.tags = append(e.tags, concat(urlTag, url, true))
e.tags = append(e.tags, e.concat(urlTag, url, true))
return e
}
// Filetype searches for a particular filetype mentioned in the query.
func (e *GoogleSearch) Filetype(filetype string) *GoogleSearch {
e.tags = append(e.tags, concat(filetypeTag, filetype, true))
e.tags = append(e.tags, e.concat(filetypeTag, filetype, true))
return e
}
// Cache shows the version of the web page that Google has in its cache.
func (e *GoogleSearch) Cache(url string) *GoogleSearch {
e.tags = append(e.tags, concat(cacheTag, url, true))
e.tags = append(e.tags, e.concat(cacheTag, url, true))
return e
}
// Related list web pages that are “similar” to a specified web page.
func (e *GoogleSearch) Related(url string) *GoogleSearch {
e.tags = append(e.tags, concat(relatedTag, url, true))
e.tags = append(e.tags, e.concat(relatedTag, url, true))
return e
}
// Ext searches for a particular file extension mentioned in the query.
func (e *GoogleSearch) Ext(ext string) *GoogleSearch {
e.tags = append(e.tags, concat(extTag, ext, false))
e.tags = append(e.tags, e.concat(extTag, ext, false))
return e
}
// Exclude excludes some results.
func (e *GoogleSearch) Exclude(value string) *GoogleSearch {
e.tags = append(e.tags, concat(excludeTag, value, false))
e.tags = append(e.tags, e.concat(excludeTag, value, false))
return e
}
@ -114,6 +104,6 @@ func (e *GoogleSearch) Group(value string) *GoogleSearch {
// Intitle searches for occurrences of keywords in title all or one.
func (e *GoogleSearch) Intitle(value string) *GoogleSearch {
e.tags = append(e.tags, concat(intitleTag, value, true))
e.tags = append(e.tags, e.concat(intitleTag, value, true))
return e
}