Skip to content

Commit

Permalink
new: git watcher added
Browse files Browse the repository at this point in the history
Cloudwatcher can now notify all the changes applied to a git repository
  • Loading branch information
Matrix86 committed Mar 18, 2021
1 parent 4b88dca commit 69b6e52
Show file tree
Hide file tree
Showing 7 changed files with 685 additions and 1 deletion.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Currently it implements the watchers for the following services:
- Amazon S3
- Google Drive
- Dropbox
- Git
- local filesystem (fsnotify/polling)

It is possible specify the directory to monitor and the polling time (how much often the watcher should check that directory),
Expand Down Expand Up @@ -124,4 +125,26 @@ It is not mandatory to call the `SetConfig()` function, and the polling time arg

Setting `disable_fsnotify` parameter on config to "true" the watcher doesn't use fsnotify and use the listing approach instead.

> :warning: not set `disable_fsnotify` to "true" if you plan to use it on a big directory!!! It could increase the I/O on disk
> :warning: not set `disable_fsnotify` to "true" if you plan to use it on a big directory!!! It could increase the I/O on disk
## Git

Git watcher has the following configurations:

| Name | Description
| --- | --- |
| `debug` | if "true" the debug mode is enabled (default "false") |
| `monitor_type` | it can be "file" or "repo" (default is "repo") |
| `auth_type` | authentication type to use: "none", "ssh", "http_token", "http_user_pass" (default "none") |
| `ssh_pkey` | path of the ssh private key (required if auth_type = "ssh") |
| `ssh_pkey_password` | password of the private key if set |
| `http_token` | token to use if auth_type = "http_token" |
| `http_username` | username of github account (auth_type = "http_user_pass") |
| `http_password` | password of github account (auth_type = "http_user_pass") |
| `repo_url` | url of the repository |
| `repo_branch` | branch to watch (if `monitor_type` is "repo" you can leave it empty to watch all the branches) |
| `assemble_events` | if "true" the events could contain one or more commit events (only if `monitor_type` = "repo") |
| `temp_dir` | temporary directory to use for clone the repo: if empty the tmp dir will be used |

If `monitor_type` is set to "repo", the event channel will receive an event with the `Object` field filled with commits or tags.
If `assemble_events` is "true" the `Object` field could contains one or more commits.
40 changes: 40 additions & 0 deletions examples/git/git_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"fmt"
"github.com/Matrix86/cloudwatcher"
"time"
)

func main() {
s, err := cloudwatcher.New("git", "", 2*time.Second)
if err != nil {
fmt.Printf("ERROR: %s", err)
return
}

config := map[string]string{
"debug": "true",
"monitor_type": "file",
"repo_url": "git@github.com:Matrix86/cloudwatcher.git",
"repo_branch": "main",
}

err = s.SetConfig(config)
if err != nil {
fmt.Printf("ERROR: %s", err)
return
}

err = s.Start()
defer s.Close()
for {
select {
case v := <-s.GetEvents():
fmt.Printf("EVENT: %s %s\n", v.Key, v.TypeString())

case e := <-s.GetErrors():
fmt.Printf("ERROR: %s\n", e)
}
}
}
50 changes: 50 additions & 0 deletions examples/git/git_repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"fmt"
"github.com/Matrix86/cloudwatcher"
"time"
)

func main() {
s, err := cloudwatcher.New("git", "", 2*time.Second)
if err != nil {
fmt.Printf("ERROR: %s", err)
return
}

config := map[string]string{
"debug": "true",
"monitor_type": "repo",
"repo_url": "git@github.com:Matrix86/cloudwatcher.git",
"assemble_events": "true",
}

err = s.SetConfig(config)
if err != nil {
fmt.Printf("ERROR: %s", err)
return
}

err = s.Start()
defer s.Close()
for {
select {
case v := <-s.GetEvents():
if v.Key == "commit" {
fmt.Println("New commits:")
for _, c := range v.Object.(*cloudwatcher.GitObject).Commits {
fmt.Printf("- hash=%s branch=%s : %s\n", c.Hash, c.Branch, c.Message)
}
} else if v.Key == "tag" {
fmt.Println("New tags:")
for _, c := range v.Object.(*cloudwatcher.GitObject).Commits {
fmt.Printf("- %s\n", c.Message)
}
}

case e := <-s.GetErrors():
fmt.Printf("ERROR: %s\n", e)
}
}
}
Loading

0 comments on commit 69b6e52

Please sign in to comment.