watcher

package module
v0.0.0-...-64d3ca4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 2, 2023 License: MIT Imports: 14 Imported by: 1

README

watcher

Build Status | codecov | Go Report Card | GoDoc

A simple cross-platform file watcher

If i miss something or you have something interesting, please be part of this project. Let me know! My contact is at the end.

With support for

  • Multi directories
  • Exclusions
  • Extensions

Events

  • OperationCreate, when added a new file
  • OperationUpdate, when updated an existing file
  • OperationDelete, when deleted a file
  • OperationChanges, all files loaded and has some of the previous events

Dependecy Management

Dep

Project dependencies are managed using Dep. Read more about Dep.

  • Install dependencies: dep ensure
  • Update dependencies: dep ensure -update
Go
go get github.com/joaosoft/watcher

Usage

This examples are available in the project at watcher/main/main.go

import (
	"fmt"
	"os"
	"os/signal"
	"sync"
	"syscall"
	github.com/joaosoft/watcher
)

func main() {
	c := make(chan *watcher.Event)
	quit := make(chan int)

	termChan := make(chan os.Signal, 1)
	signal.Notify(termChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR1)

	w := watcher.NewWatcher(watcher.WithEventChannel(c), watcher.WithQuitChannel(quit))

	go func() {
		for {
			select {
			case <-termChan:
				quit <- 1
				return
			case event := <-c:
				fmt.Printf("received event %+v\n", event)
			}
		}
	}()

	if err := w.Start(nil); err != nil {
		panic(err)
	}

	<-termChan
	if err := w.Stop(nil); err != nil {
		panic(err)
	}
}

Configuration file

{
  "watcher": {
    "reload_time": 1,
    "dirs": {
      "watch":[ "examples/" ],
      "excluded":[ "examples/test_2" ],
      "extensions": [ "go", "json" ]
    },
    "log": {
      "level": "error"
    }
  },
  "manager": {
    "log": {
      "level": "error"
    }
  }
}

Known issues

Follow me at

Facebook: https://www.facebook.com/joaosoft

LinkedIn: https://www.linkedin.com/in/jo%C3%A3o-ribeiro-b2775438/

If you have something to add, please let me know joaosoft@gmail.com

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Changed = true

Functions

func Exists

func Exists(file string) bool

func GetEnv

func GetEnv() string

func ReadFile

func ReadFile(file string, obj interface{}) ([]byte, error)

func ReadFileLines

func ReadFileLines(file string) ([]string, error)

func WriteFile

func WriteFile(file string, obj interface{}) error

Types

type AppConfig

type AppConfig struct {
	Watcher *WatcherConfig `json:"watcher"`
}

AppConfig ...

func NewConfig

func NewConfig() (*AppConfig, manager.IConfig, error)

NewConfig ...

type Event

type Event struct {
	File      string
	Operation Operation
}

type FileInfo

type FileInfo struct {
	FullName string
	Name     string
	Size     int64
	ModTime  time.Time
}

type Operation

type Operation string
const (
	OperationCreate  Operation = "create"
	OperationUpdate  Operation = "update"
	OperationDelete  Operation = "delete"
	OperationChanges Operation = "changes"
)

type Watcher

type Watcher struct {
	// contains filtered or unexported fields
}

func NewWatcher

func NewWatcher(options ...WatcherOption) *Watcher

func (*Watcher) AddExcluded

func (w *Watcher) AddExcluded(excluded ...string) *Watcher

func (*Watcher) AddExtension

func (w *Watcher) AddExtension(extensions ...string) *Watcher

func (*Watcher) AddWatch

func (w *Watcher) AddWatch(watchs ...string) *Watcher

func (*Watcher) Reconfigure

func (w *Watcher) Reconfigure(options ...WatcherOption)

Reconfigure ...

func (*Watcher) Start

func (w *Watcher) Start(waitGroup ...*sync.WaitGroup) error

Start ...

func (*Watcher) Started

func (w *Watcher) Started() bool

Started ...

func (*Watcher) Stop

func (w *Watcher) Stop(waitGroup ...*sync.WaitGroup) error

Stop ...

type WatcherConfig

type WatcherConfig struct {
	ReloadTime int64 `json:"reload_time"`
	Dirs       struct {
		Watch      []string `json:"watch"`
		Excluded   []string `json:"excluded"`
		Extensions []string `json:"extensions"`
	} `json:"dirs"`
	Log struct {
		Level string `json:"level"`
	} `json:"log"`
}

WatcherConfig ...

type WatcherOption

type WatcherOption func(watcher *Watcher)

WatcherOption ...

func WithConfiguration

func WithConfiguration(config *WatcherConfig) WatcherOption

WithConfiguration ...

func WithEventChannel

func WithEventChannel(event chan *Event) WatcherOption

WithEventChannel ...

func WithLogLevel

func WithLogLevel(level logger.Level) WatcherOption

WithLogLevel ...

func WithLogger

func WithLogger(logger logger.ILogger) WatcherOption

WithLogger ...

func WithManager

func WithManager(mgr *manager.Manager) WatcherOption

WithManager ...

func WithQuitChannel

func WithQuitChannel(quit chan int) WatcherOption

WithQuitChannel ...

func WithReloadTime

func WithReloadTime(reloadTime int64) WatcherOption

WithReloadTime ...

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL