gpoll

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2019 License: MIT Imports: 17 Imported by: 4

README

GPoll

GoDoc

Go library for polling a Git repository.

Installation

go get github.com/eddieowens/gpoll

Usage

package main

import (
    "fmt"
    "github.com/eddieowens/gpoll"
    "log"
)

func main() {
    poller, err := gpoll.NewPoller(gpoll.PollConfig{
        Git: gpoll.GitConfig{
            Auth: gpoll.GitAuthConfig{
                // Uses the SSH key from my local directory.
                SshKey: "~/.ssh/id_rsa",
            },
            // The target remote.
            Remote: "git@github.com:eddieowens/gpoll.git",
        },
        OnUpdate: func(change gpoll.GitChange) {
            switch change.EventType {
            case gpoll.EventTypeDelete:
                fmt.Printf("%s was deleted", change.Filename)
            case gpoll.EventTypeUpdate:
                fmt.Printf("%s was updated", change.Filename)
            case gpoll.EventTypeCreate:
                fmt.Printf("%s was created", change.Filename)
            }
        },
    })
    
    if err != nil {
        panic(err)
    }
    
    // Will poll the repo until poller.Stop() is called.
    log.Fatal(poller.Start())
}

Documentation

Overview

A library for polling a Git repository for changes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChangeType

type ChangeType int
const (
	ChangeTypeUpdate ChangeType = iota
	ChangeTypeCreate
	ChangeTypeDelete
)

type FilterFunc

type FilterFunc func(change GitChange) bool

type GitAuthConfig

type GitAuthConfig struct {
	// The filepath to the SSH key. Required if the Username and Password are not set.
	SshKey string `validation:"required_without=Username Password"`

	// The username for the git repo. Required if the SshKey is not set or if the Password is set.
	Username string `validation:"required_without=SshKey,required_with=Password"`

	// The password for the git repo. Required if the SshKey is not set or if the Username is set.
	Password string `validation:"require_without=SshKey,required_with=Username"`
}

type GitChange

type GitChange struct {
	// The name and absolute path to the changed file.
	Filepath string

	// The commit sha associated with the change.
	Sha string

	// The type of change that occurred e.g. added, created, deleted the file.
	ChangeType ChangeType
}

Represents a change to a file within the target Git repo.

type GitConfig

type GitConfig struct {
	// Authentication/authorization for the git repo to poll. Required.
	Auth GitAuthConfig `validate:"required"`

	// The remote git repository that should be polled. Required.
	Remote string `validate:"required"`

	// The branch of the git repo to poll. Defaults to master.
	Branch string

	// The directory that the git repository will be cloned into. Defaults to the current directory.
	CloneDirectory string
}

type HandleFunc

type HandleFunc func(change GitChange)

type PollConfig

type PollConfig struct {
	Git GitConfig `validate:"required"`

	// Function that is called when a change is detected. If true is returned for the change, The function set for
	// HandleChange will trigger. If false is returned, HandleChange will not be called.
	Filter FilterFunc

	// Function that is called when a change occurs to a file in the git repository.
	HandleChange HandleFunc

	// The polling interval. Defaults to 30 seconds.
	Interval time.Duration
}

type Poller

type Poller interface {
	// Start polling your git repo without blocking. The poller will diff the remote against the local clone directory at
	// the specified interval and return all changes through the configured callback and the returned channel.
	StartAsync() (chan GitChange, error)

	// Start polling your git repo blocking whatever thread it is run on. The poller will diff the remote against the
	// local clone directory at the specified interval and return all changes through the configured callback.
	Start() error

	// Stop all polling.
	Stop()

	// Diff the remote and the local and return all differences.
	Poll() ([]GitChange, error)
}

func NewPoller

func NewPoller(config PollConfig) (Poller, error)

Create a new Poller from config. Will return an error for misconfiguration.

Jump to

Keyboard shortcuts

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