Skip to content

Commit

Permalink
replaced tags with attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
spy16 committed Jul 16, 2018
1 parent 70bb08b commit 333b753
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 25 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ You can run `radium --help` to see the list of available commands.
### Querying from command-line

```bash
radium query "append file" --tag language:go --tag color:yes
radium query "append file" --attr language:go --attr color:yes

radium query dir --tag platform:windows
radium query dir --attr platform:windows

radium query go
```

> `--tag` is not part of radium framework but part of the source
> implementation itself. For example `--tag color:yes` does not
> `--attr` is not part of radium framework but part of the source
> implementation itself. For example `--attr color:yes` does not
> work always and works with only results returned by `CheatSh`
> source.
Expand All @@ -46,7 +46,7 @@ curl "localhost:8080/search?q=go"
```

> When using http api, all query parameters except `q` will be
> assumed to be tags
> assumed to be attributes

### Running as Clipboard Monitor
Expand Down
14 changes: 7 additions & 7 deletions cmd/radium/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ func newQueryCmd(cfg *config) *cobra.Command {
Args: cobra.ExactArgs(1),
}

var tags []string
cmd.Flags().StringSliceVarP(&tags, "tag", "t", []string{}, "Tags to narrow the search scope")
var attribs []string
cmd.Flags().StringSliceVarP(&attribs, "attr", "a", []string{}, "Attributes to narrow the search scope")

cmd.Run = func(_ *cobra.Command, args []string) {
query := radium.Query{}
query.Text = args[0]
query.Tags = map[string]string{}
query.Attribs = map[string]string{}

for _, tag := range tags {
parts := strings.Split(tag, ":")
for _, attrib := range attribs {
parts := strings.Split(attrib, ":")
if len(parts) == 2 {
query.Tags[parts[0]] = parts[1]
query.Attribs[parts[0]] = parts[1]
} else {
fmt.Println("Err: invalid tag format. must be <name>:<value>")
fmt.Println("Err: invalid attrib format. must be <name>:<value>")
os.Exit(1)
}
}
Expand Down
8 changes: 4 additions & 4 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type Article struct {
// should be one of markdown, json, yaml, html
ContentType string `json:"content_type"`

// Tags can contain type of the article, keywords etc.
Tags map[string]string `json:"tags"`
// Attribs can contain type of the article, keywords etc.
Attribs map[string]string `json:"attribs"`

// License can contain name of the license if applicable
License string `json:"license"`
Expand Down Expand Up @@ -50,9 +50,9 @@ type Query struct {
// use this to find relevant results
Text string `json:"text"`

// Tags can be used by sources to further filter down
// Attribs can be used by sources to further filter down
// the results
Tags map[string]string `json:"tags"`
Attribs map[string]string `json:"attribs"`
}

// Validate for empty or invalid queries
Expand Down
4 changes: 2 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ func (srv Server) handleSearch(wr http.ResponseWriter, req *http.Request) {

query := Query{}
query.Text = req.FormValue("q")
query.Tags = map[string]string{}
query.Attribs = map[string]string{}

for key, val := range req.URL.Query() {
if key != "q" && len(val) > 0 {
query.Tags[key] = val[0]
query.Attribs[key] = val[0]
}
}

Expand Down
6 changes: 3 additions & 3 deletions sources/cheatsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ type CheatSh struct {
func (csh CheatSh) Search(query radium.Query) ([]radium.Article, error) {
var results []radium.Article

if lang, found := query.Tags["language"]; found {
if lang, found := query.Attribs["language"]; found {
color := false
if val, found := query.Tags["color"]; found {
if val, found := query.Attribs["color"]; found {
if val == "yes" || val == "true" {
color = true
}
Expand Down Expand Up @@ -78,7 +78,7 @@ func (csh CheatSh) makeLangRequest(q string, lang string, color bool) (*radium.A
result.Content = string(data)
result.ContentType = "plaintext"
result.Title = q
result.Tags = map[string]string{
result.Attribs = map[string]string{
"language": lang,
}
return result, nil
Expand Down
2 changes: 1 addition & 1 deletion sources/learnxiny.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (lxy LearnXInY) getLanguageMarkdown(language string) (*radium.Article, erro
result.Content = string(data)
result.ContentType = "markdown"
result.Title = language
result.Tags = map[string]string{}
result.Attribs = map[string]string{}
result.License = "CC BY-SA 3.0"
return result, nil
}
2 changes: 1 addition & 1 deletion sources/radium.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (rad Radium) Search(query radium.Query) ([]radium.Article, error) {

urlQuery.Set("q", query.Text)

for name, val := range query.Tags {
for name, val := range query.Attribs {
urlQuery.Set(name, val)
}

Expand Down
4 changes: 2 additions & 2 deletions sources/tldr.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (tldr TLDR) Search(query radium.Query) ([]radium.Article, error) {
tool := strings.Replace(query.Text, " ", "-", -1)
platform := "common"

if val, found := query.Tags["platform"]; found {
if val, found := query.Attribs["platform"]; found {
platform = val
}

Expand Down Expand Up @@ -72,7 +72,7 @@ func (tldr TLDR) getPlatformToolInfo(tool, platform string) (*radium.Article, er
result.Content = string(data)
result.ContentType = "markdown"
result.Title = tool
result.Tags = map[string]string{
result.Attribs = map[string]string{
"platform": platform,
}
result.License = "The MIT License (MIT)"
Expand Down

0 comments on commit 333b753

Please sign in to comment.