gosync

package module
v0.0.0-...-ef49813 Latest Latest
Warning

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

Go to latest
Published: May 5, 2023 License: Apache-2.0 Imports: 6 Imported by: 2

Documentation

Index

Examples

Constants

View Source
const (
	// AddOnly only runs add operations.
	AddOnly OperatingMode = "Add"
	// RemoveOnly only runs remove operations.
	RemoveOnly OperatingMode = "Remove"
	// RemoveAdd first removes things, then adds them.
	RemoveAdd OperatingMode = "RemoveAdd"
	// AddRemove first adds things, then removes them.
	AddRemove OperatingMode = "AddRemove"
	// NoChangeLimit tells Sync not to set a change limit.
	NoChangeLimit int = -1
)

Variables

View Source
var ErrCacheEmpty = errors.New("cache is empty, run Get first")

ErrCacheEmpty is returned if an adapter expects Get to be called before Add/Remove.

View Source
var ErrInvalidConfig = errors.New("invalid configuration")

ErrInvalidConfig is returned when an InitFn is passed an invalid configuration.

View Source
var ErrMissingConfig = errors.New("missing configuration")

ErrMissingConfig is returned when an InitFn is missing a required configuration.

View Source
var ErrNotImplemented = errors.New("not implemented")

ErrNotImplemented is for brand-new adapters that are still being worked on.

View Source
var ErrReadOnly = errors.New("cannot perform action, adapter is readonly")

ErrReadOnly is returned for adapters that cannot Add/Remove, but have been set as a destination.

View Source
var ErrTooManyChanges = errors.New("too many changes")

ErrTooManyChanges is returned when a change limit has been set, and the number of changes exceeds it.

Functions

func SetCaseSensitive

func SetCaseSensitive(caseSensitive bool) func(*Sync)

SetCaseSensitive can configure Go Sync's case sensitivity when comparing things.

Example
package main

import (
	"github.com/ovotech/go-sync/packages/gosync"
)

func main() {
	// Any Go Sync adapter.
	var source gosync.Adapter

	gosync.New(source, gosync.SetCaseSensitive(true))
}

func SetMaximumChanges

func SetMaximumChanges(maximumChanges int) func(*Sync)

SetMaximumChanges sets a maximum number of changes that Sync will allow before returning an ErrTooManyChanges error.

Example
package main

import (
	"github.com/ovotech/go-sync/packages/gosync"
)

func main() {
	// Any Go Sync adapter.
	var source gosync.Adapter

	gosync.New(source, gosync.SetMaximumChanges(5))
}

func SetOperatingMode

func SetOperatingMode(mode OperatingMode) func(*Sync)

SetOperatingMode sets a custom operating mode.

Example
package main

import (
	"github.com/ovotech/go-sync/packages/gosync"
)

func main() {
	// Any Go Sync adapter.
	var source gosync.Adapter

	// Set the operating mode to add only (don't remove things).
	operatingMode := gosync.SetOperatingMode(gosync.AddOnly)

	gosync.New(source, operatingMode)
}

Types

type Adapter

type Adapter interface {
	Get(ctx context.Context) (things []string, err error) // Get things in a service.
	Add(ctx context.Context, things []string) error       // Add things to a service.
	Remove(ctx context.Context, things []string) error    // Remove things from a service.
}

Adapter interfaces are used to allow Sync to communicate with third party services.

type ConfigKey

type ConfigKey = string

ConfigKey is a configuration key to Init a new adapter.

type InitFn

type InitFn = func(ctx context.Context, config map[string]string) (Adapter, error)

InitFn is an optional adapter function that can initialise a new adapter using a static configuration. This is to make it easier to use an adapter in a CLI or other service that invokes adapters programmatically.

type OperatingMode

type OperatingMode string

OperatingMode specifies how Sync operates, which sync operations are run and in what order.

type Service

type Service interface {
	SyncWith(ctx context.Context, adapter Adapter) error // Sync the things in a source service with this service.
}

Service can be used for downstream services that implement Sync in your own workflow.

type Sync

type Sync struct {
	DryRun bool // DryRun mode calculates membership, but doesn't add or remove.
	// contains filtered or unexported fields
}

func New

func New(source Adapter, configFns ...func(*Sync)) *Sync

New creates a new gosync.Sync service.

Example
package main

import (
	"github.com/ovotech/go-sync/packages/gosync"
)

func main() {
	// Any Go Sync adapter.
	var source gosync.Adapter

	gosync.New(source)
}

func (*Sync) SyncWith

func (s *Sync) SyncWith(ctx context.Context, adapter Adapter) error

SyncWith synchronises the destination service with the source service, adding & removing things as necessary.

Example
package main

import (
	"context"
	"log"

	"github.com/ovotech/go-sync/packages/gosync"
)

func main() {
	// Any Go Sync adapters.
	var source, destination gosync.Adapter

	sync := gosync.New(source)

	// By default, Go Sync runs in dry run mode. To make changes this must manually be set to false.
	sync.DryRun = false

	err := sync.SyncWith(context.Background(), destination)
	if err != nil {
		log.Panic(err)
	}
}

Jump to

Keyboard shortcuts

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