Skip to content

Commit

Permalink
Merge pull request #6 from jguyomard/feature/restore-from-logs
Browse files Browse the repository at this point in the history
Add restore command
  • Loading branch information
jguyomard authored Oct 17, 2017
2 parents b7934cb + f5b5918 commit bee9d5a
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Do you use Slack? Do you share links with your co-workers?
SlackBot-Links store all theses urls, enrich them with metadata, and expose them through a REST API.
You can now find shared urls by searching in their title, content, author name...


## Get Started

### 1. Build Slackbot Links
Expand Down Expand Up @@ -50,6 +51,7 @@ You can now share link on `#links` and fetch them using REST API :
curl localhost:9300/v1/links/
```


## API

REST API Documentation can be found [here](https://jguyomard.github.io/slackbot-links).
Expand All @@ -62,6 +64,20 @@ make docapi
```


## Commands

### `restore`

This command recreates the Elasticsearch index from a given `links.log` log file.
This is very useful for fast testing with a given scenario, or for initializing a staging environment, for instance.

```
slackbot-links -config-file=/etc/slackbot-links/config.yaml -links-file=./links-tests.log restore
```

If a link already exists (same ID), it is updated.


## Testing

To run the test suite:
Expand All @@ -70,6 +86,7 @@ To run the test suite:
make test
```


## Issues

If you have any problems with or questions about this Service Provider, please contact me through a [GitHub issue](https://github.com/jguyomard/slackbot-links/issues).
Expand Down
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"flag"
"os"

"github.com/jguyomard/slackbot-links/src/api"
"github.com/jguyomard/slackbot-links/src/config"
Expand All @@ -13,6 +14,7 @@ func main() {

// Read command options
configFilePtr := flag.String("config-file", "/etc/slackbot-links/config.yaml", "conf file path")
linksFilePtr := flag.String("links-file", "", "links file path")
flag.Parse()

// Set config filepath
Expand All @@ -21,6 +23,15 @@ func main() {
// Connect to ES
links.Init()

// Commands
if flag.Arg(0) == "restore" {
if *linksFilePtr == "" {
panic("--links-file is mandadory.")
}
links.Restore(*linksFilePtr)
os.Exit(0)
}

// Listen for new Slack Messages
go slackbot.Listen()

Expand Down
101 changes: 101 additions & 0 deletions src/links/restore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package links

import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
"time"
)

type logData struct {
Action string
Level string
Msg string
Time *time.Time
Link Link
}

var (
linksIdsAnalysed = []string{}
)

// Restore links from json file
func Restore(filepath string) bool {

file, err := os.Open(filepath)
if err != nil {
fmt.Println("Error opening links-file:", err)
os.Exit(1)
}

defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
restoreLink(scanner.Bytes())
}

if err = scanner.Err(); err != nil {
log.Fatal(err)
}

return true
}

func restoreLink(lineStr []byte) bool {
var line logData

// json decode
if err := json.Unmarshal(lineStr, &line); err != nil {
fmt.Printf("jsonDecode ERROR (Unmarshal): %v\n", err)
return false
}

switch line.Action {

// Save Link (insert or update)
case "save":
if linkAlreadyAnalysed(line.Link) {
fmt.Printf(" - Link %s (url=%s) already analysed. Continue.\n", line.Link.ID, line.Link.URL)
return false
}
if line.Link.Save() {
markLinkAsAnalysed(line.Link)
fmt.Printf(" - Link %s (%s) restored.\n", line.Link.ID, line.Link.URL)
return true
}
fmt.Printf(" - Error saving Link %s (%s). Continue.\n", line.Link.ID, line.Link.URL)
return false

// Delete Link
case "delete":
if line.Link.Delete() {
fmt.Printf(" - Link %s (%s) deletion restored.\n", line.Link.ID, line.Link.URL)
return true
}
fmt.Printf(" - Error deleting Link %s (%s). Continue.\n", line.Link.ID, line.Link.URL)
return false

}

return false
}

func markLinkAsAnalysed(link Link) {
linksIdsAnalysed = append(linksIdsAnalysed, link.ID)
}

func linkAlreadyAnalysed(link Link) bool {
return inArr(link.ID, linksIdsAnalysed)
}

func inArr(str string, arr []string) bool {
for _, val := range arr {
if str == val {
return true
}
}
return false
}

0 comments on commit bee9d5a

Please sign in to comment.