filewatcher

package
Version: v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2020 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package filewatcher provides facilities for watching a file on a filesystem for changes..

When To Use

This package is meant to provide abstraction for watching a single file, be it on a local filesystem or remote system. This is useful for watching configuraiton files that may be local or stored on a remote service. For watching multiple files on a local filesystem, use fsnotify.

Usage Note

All file paths must be prepended with a *marker* that indicates what type of filesystem the file is on so it can be routed to the right handler.

For example:

"local:/path/to/file"

SideEffect Imports

To use the filewatcher, you must side-effect import a filesystem implementation, such as "local" in your main.

Example
// Grab the file in the local file system at "path/to/file".
// The "Local" marker indicates to use the local filesystem handler to fetch the file.
// Remember you need to side-effect import in main: "github.com/johnsiilver/golib/filewatcher/local".
ch, closer, err := Get(Local+"path/to/file", nil)
if err != nil {
	// Do something
}
defer closer()

// This fetches the initial content of the file.
content := <-ch
fmt.Println("Initial content of the file:")
fmt.Println(string(content))

// This fetches the content at each change.
for b := range <-ch {
	fmt.Println("New content of the file:")
	fmt.Println(string(b))
}
Output:

Index

Examples

Constants

View Source
const (
	// Local indicates the local filesystem, which was be done through standard Go libraries such as io and ioutil.
	Local = "local:"
)

Variables

This section is empty.

Functions

func Get

func Get(f string, i interface{}) (results chan []byte, closer func(), err error)

Get watches a file and sends changes on the returned channel. The file that exists when starting the watcher is streamed back as the first result. "f" must be prepended with a marker that indicates the file system that will be accessed. "i" provides the ability to pass data structures to the underlying file access implementation (such as auth credentials or RPC endpoint information).

func Register

func Register(marker string, watcher func() Watch)

Register registers a filewatcher for files with "marker" using "watcher". This is only used by developers of filewatcher modules.

Types

type Watch

type Watch interface {
	// File watches the file at "f".  "f" WILL NOT contain the marker. The first channel
	// contains the file being streamed back.  "i" provides the ability to
	// pass data structures to the underlying file access implementation (such as auth credentials or RPC endpoint information).
	File(f string, i interface{}) (chan []byte, error)

	// Close stops the watcher from watching the file.
	Close()
}

Watcher watches a file and sends changes on the returned channel. The Watcher will return the current file when started. Files always must be prepended with a marker indicating the type of file system we are accessing.

Source Files

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL