refactor: commands

pull/2/head
sundowndev 2020-04-12 01:15:24 +01:00
parent 0092581e2e
commit 7861328123
9 changed files with 122 additions and 23 deletions

33
cmd/drop.go Normal file
View File

@ -0,0 +1,33 @@
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/sundowndev/go-search/engine"
)
func init() {
// Register command
rootCmd.AddCommand(dropCmd)
}
var dropCmd = &cobra.Command{
Use: "drop",
Short: "Drop all indexes",
Run: func(cmd *cobra.Command, args []string) {
client, err := engine.NewRedisClient(redisAddr, redisPort)
if err != nil {
fmt.Println("Failed to connect to database", redisAddr, redisPort)
os.Exit(1)
}
defer client.Close()
fmt.Println("Dropping database...")
client.FlushAll()
fmt.Println("Done.")
},
}

View File

@ -22,8 +22,9 @@ var dumpCmd = &cobra.Command{
fmt.Println("Failed to connect to database", redisAddr, redisPort)
os.Exit(1)
}
defer client.Close()
fmt.Printf("Dumping 15 last keys...\n\n")
fmt.Printf("Dumping last 15 keys...\n\n")
keys, _ := client.GetAllKeys()

View File

@ -2,6 +2,8 @@ package cmd
import (
"fmt"
"io/ioutil"
"log"
"os"
"github.com/spf13/cobra"
@ -23,10 +25,18 @@ var indexCmd = &cobra.Command{
fmt.Println("Failed to connect to database", redisAddr, redisPort)
os.Exit(1)
}
defer client.Close()
filePath := args[0]
client.AddFile(filePath, 3)
fmt.Printf("Walking %v...\n", filePath)
data, err := ioutil.ReadFile(filePath)
if err != nil {
log.Fatal(err)
}
client.AddFile(filePath, string(data))
fmt.Println("Successfully indexed file", filePath)
},

View File

@ -2,6 +2,7 @@ package cmd
import (
"fmt"
"log"
"os"
"github.com/spf13/cobra"
@ -16,13 +17,34 @@ func init() {
var queryCmd = &cobra.Command{
Use: "query",
Short: "Run a query against the database",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
_, err := engine.NewRedisClient(redisAddr, redisPort)
client, err := engine.NewRedisClient(redisAddr, redisPort)
if err != nil {
fmt.Println("Failed to connect to database", redisAddr, redisPort)
os.Exit(1)
}
defer client.Close()
print("query")
word := args[0]
fmt.Printf("Querying index for \"%s\":\n\n", word)
results, err := client.ZRevRange(word)
if err != nil {
log.Fatal(err)
}
var queryResults []*engine.QueryResult
for _, file := range results {
queryResults = append(queryResults, &engine.QueryResult{
File: file,
Count: 1,
FirstMatch: "...",
})
}
engine.ShowResults(queryResults)
},
}

View File

@ -17,9 +17,9 @@ func init() {
}
var rootCmd = &cobra.Command{
Use: "gosearch [COMMANDS] [OPTIONS]",
Use: "search [COMMANDS] [OPTIONS]",
Short: "A simple CLI search engine for your file system backed by Redis",
Example: "gosearch index $(pwd)/fixtures",
Example: "search index $(pwd)/fixtures",
}
// Execute is a function that executes the root command

View File

@ -1,8 +1,6 @@
package engine
import (
"encoding/json"
"io/ioutil"
"strings"
"github.com/go-redis/redis/v7"
@ -31,38 +29,43 @@ func NewRedisClient(addr, port string) (*RedisClient, error) {
}
// AddFile index a file
func (c *RedisClient) AddFile(file string, score int) error {
data, err := ioutil.ReadFile(file)
if err != nil {
return err
func (c *RedisClient) AddFile(key string, content string) error {
words := strings.Split(content, " ")
for _, v := range words {
if err := c.conn.ZAdd(strings.ToLower(v), &redis.Z{
Score: float64(1),
Member: key,
}).Err(); err != nil {
return err
}
}
words := strings.Split(string(data), " ")
return nil
}
wordsJSONArray, err := json.Marshal(&words)
if err != nil {
return err
}
return c.conn.Set(file, wordsJSONArray, 0).Err()
// ZRevRange search for a key
func (c *RedisClient) ZRevRange(key string) ([]string, error) {
return c.conn.ZRevRange(key, 0, -1).Result()
}
// GetKey returns a key value
func (c *RedisClient) GetKey(key string) (string, error) {
// Utilisez Redigo pour lire toutes les valeurs de la clef, et les
// placer dans une tranche de chaînes. Renvoyez une erreur si nécessaire.
return c.conn.Get(key).Result()
}
// GetAllKeys returns a key value
func (c *RedisClient) GetAllKeys() (keys []string, err error) {
// Utilisez Redigo pour lire toutes les valeurs de la clef, et les
// placer dans une tranche de chaînes. Renvoyez une erreur si nécessaire.
keys, _, err = c.conn.Scan(0, "*", 15).Result()
return
}
// FlushAll drop the database
func (c *RedisClient) FlushAll() error {
return c.conn.FlushAll().Err()
}
// Close closes the Redis connection
func (c *RedisClient) Close() error {
return c.conn.Close()

View File

@ -1 +1,28 @@
package engine
import (
"fmt"
"os"
"text/tabwriter"
)
const tabPadding = 3
const tabPaddingChar = ' '
// QueryResult ...
type QueryResult struct {
File string
Count int
FirstMatch string
}
// ShowResults display results in a table
func ShowResults(results []*QueryResult) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, tabPadding, tabPaddingChar, tabwriter.AlignRight|tabwriter.Debug)
defer w.Flush()
fmt.Fprintln(w, "File\tCount\tFirst match\t")
for _, v := range results {
fmt.Fprintf(w, "%v\t%v\t%v\t", v.File, v.Count, v.FirstMatch)
}
}

1
go.mod
View File

@ -3,6 +3,7 @@ module github.com/sundowndev/go-search
go 1.13
require (
github.com/evilsocket/islazy v1.10.6 // indirect
github.com/go-redis/redis/v7 v7.2.0
github.com/spf13/cobra v1.0.0
github.com/stretchr/testify v1.2.2

2
go.sum
View File

@ -18,6 +18,8 @@ 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/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/evilsocket/islazy v1.10.6 h1:MFq000a1ByoumoJWlytqg0qon0KlBeUfPsDjY0hK0bo=
github.com/evilsocket/islazy v1.10.6/go.mod h1:OrwQGYg3DuZvXUfmH+KIZDjwTCbrjy48T24TUpGqVVw=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=