domain

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidActionType = errors.New("invalid action type")
View Source
var ErrNotValidNativeEventType = errors.New("not a valid native event type")

Functions

func DestroyHandlerChain

func DestroyHandlerChain(handlerChain EventHandler)

Types

type ActionExecutorCreator

type ActionExecutorCreator interface {
	CreateExecutor(action *HookAction, hookDef *HookDefinition) (EventHandler, error)
}

type AppProfile

type AppProfile struct {
	Log       LogConfig       `koanf:"log"`
	Syncthing SyncthingConfig `koanf:"syncthing"`
	Hooks     []Hook          `koanf:"hooks"`
}

type ConnectionServiceStatus

type ConnectionServiceStatus struct {
	Error        *string  `json:"error"`
	LanAddresses []string `json:"lanAddresses"`
	WanAddresses []string `json:"wanAddresses"`
}

type DiscoveryStatus

type DiscoveryStatus struct {
	Error *string `json:"error"`
}

type Event

type Event struct {
	Time time.Time   `json:"time"`
	Type EventType   `json:"type"`
	Data interface{} `json:"data"`
}

func NewEventFromStEvent

func NewEventFromStEvent(event events.Event) (*Event, error)

func (*Event) String

func (e *Event) String() string

type EventCh

type EventCh = <-chan *Event

func ConvertNativeEventChannel

func ConvertNativeEventChannel(nativeEventCh <-chan events.Event) EventCh

ConvertNativeEventChannel converts native event channel into custom event channel. The returned channel will be closed automatically once the given native event channel is closed.

Events that failed to convert to local event will be ignored.

type EventHandler

type EventHandler interface {
	SetNext(next EventHandler)
	GetNext() EventHandler
	// Handle processes the event. It should call next handler in most cases.
	Handle(event *Event)
	// Destroy will be called when the handler will not be used anymore.
	// Typically, it should cancel any ongoing coroutines or release other resources.
	//
	// Do not call next handler.
	Destroy()
}

EventHandler implements the chain of responsibility pattern, one event will be processed by different handlers one by one. The handler may terminate event propagation (e.g. filter).

func BuildEventHandlerChain

func BuildEventHandlerChain(handlers ...EventHandler) EventHandler

BuildEventHandlerChain connects all given handlers into a chain, and returns the first one. If there is no handler, return nil. This function doesn't modify the tail handler.

type EventSource

type EventSource interface {
	Subscribe(eventType EventType, params *HookParameters, hookDef *HookDefinition) (EventCh, error)
	// Unsubscribe must eventually close the given channel.
	Unsubscribe(eventCh EventCh)
}

EventSource is a unified event subscription portal, including native syncthing event and extra event detected by syncthing hook.

type EventType

type EventType string
const (
	UnknownEventType EventType = ""

	ConfigSaved             EventType = "st:ConfigSaved"
	DeviceConnected         EventType = "st:DeviceConnected"
	DeviceDisconnected      EventType = "st:DeviceDisconnected"
	DeviceDiscovered        EventType = "st:DeviceDiscovered"
	DevicePaused            EventType = "st:DevicePaused"
	DeviceResumed           EventType = "st:DeviceResumed"
	DownloadProgress        EventType = "st:DownloadProgress"
	Failure                 EventType = "st:Failure"
	FolderCompletion        EventType = "st:FolderCompletion"
	FolderErrors            EventType = "st:FolderErrors"
	FolderPaused            EventType = "st:FolderPaused"
	FolderResumed           EventType = "st:FolderResumed"
	FolderScanProgress      EventType = "st:FolderScanProgress"
	FolderSummary           EventType = "st:FolderSummary"
	FolderWatchStateChanged EventType = "st:FolderWatchStateChanged"
	ItemFinished            EventType = "st:ItemFinished"
	ItemStarted             EventType = "st:ItemStarted"
	ListenAddressesChanged  EventType = "st:ListenAddressesChanged"
	LocalChangeDetected     EventType = "st:LocalChangeDetected"
	LocalIndexUpdated       EventType = "st:LocalIndexUpdated"
	LoginAttempt            EventType = "st:LoginAttempt"
	PendingDevicesChanged   EventType = "st:PendingDevicesChanged"
	PendingFoldersChanged   EventType = "st:PendingFoldersChanged"
	RemoteChangeDetected    EventType = "st:RemoteChangeDetected"
	RemoteDownloadProgress  EventType = "st:RemoteDownloadProgress"
	RemoteIndexUpdated      EventType = "st:RemoteIndexUpdated"
	Starting                EventType = "st:Starting"
	StartupComplete         EventType = "st:StartupComplete"
	StateChanged            EventType = "st:StateChanged"

	LocalFolderContentChangeDetected EventType = "ex:LocalFolderContentChangeDetected"
)

Supported event types. Each type except UnknownEventType MUST be registered in mapToStType.

func UnmarshalEventType

func UnmarshalEventType(evType string) EventType

func (EventType) ConvertToNative

func (t EventType) ConvertToNative() (events.EventType, error)

ConvertToNative converts current event type to syncthing native type. Returns ErrNotValidNativeEventType if failed.

func (EventType) IsNativeEvent

func (t EventType) IsNativeEvent() bool

type Hook

type Hook struct {
	Name       string         `koanf:"name"`
	EventType  string         `koanf:"event-type"`
	Parameter  HookParameters `koanf:"parameter"`
	Conditions []struct {
		Type  string `koanf:"type"`
		Var   string `koanf:"var"`
		Value string `koanf:"value"`
	} `koanf:"conditions"`
	Action HookAction `koanf:"action"`
}

type HookAction

type HookAction struct {
	Type string   `koanf:"type"`
	Cmd  []string `koanf:"cmd"`
}

type HookDefinition

type HookDefinition struct {
	Name  string
	Index int
}

HookDefinition represents a Hook item in the configuration.

func NewHookDefinition

func NewHookDefinition(name string, index int) *HookDefinition

func (*HookDefinition) AddToLogger

func (d *HookDefinition) AddToLogger(logger *zap.SugaredLogger) *zap.SugaredLogger

AddToLogger adds hook definition info to the given logger, returns new logger.

type HookManager

type HookManager interface {
	RegisterHook(hook *Hook, hookDef *HookDefinition) error
	UnregisterAll()
}

HookManager registers hook and executes action.

type HookParameters

type HookParameters map[string]any

func (HookParameters) ContainsKey

func (p HookParameters) ContainsKey(key string) (ex bool)

func (HookParameters) ExtractInt64IfExist

func (p HookParameters) ExtractInt64IfExist(key string, target *int64)

func (HookParameters) ExtractIntIfExist

func (p HookParameters) ExtractIntIfExist(key string, target *int)

func (HookParameters) ExtractStringIfExist

func (p HookParameters) ExtractStringIfExist(key string, target *string)

func (HookParameters) GetInt64

func (p HookParameters) GetInt64(key string, defaultValue int64) int64

func (HookParameters) GetString

func (p HookParameters) GetString(key string, defaultValue string) string

type IllegalEventParamError

type IllegalEventParamError struct {
	Message string
}

func NewIllegalEventParamError

func NewIllegalEventParamError(message string) *IllegalEventParamError

func (*IllegalEventParamError) Error

func (e *IllegalEventParamError) Error() string

type LastDialStatus

type LastDialStatus struct {
	When  time.Time `json:"when"`
	Error *string   `json:"error"`
}

type LogConfig

type LogConfig struct {
	Stdout struct {
		Enabled bool   `koanf:"enabled"`
		Level   string `koanf:"level"`
	} `koanf:"stdout"`
	File LogFileConfig `koanf:"file"`
}

type LogFileConfig

type LogFileConfig struct {
	Enabled    bool   `koanf:"enabled"`
	Level      string `koanf:"level"`
	Dir        string `koanf:"dir"`
	MaxSize    int    `koanf:"max-size"`
	MaxBackups int    `koanf:"max-backups"`
}

type StApiError

type StApiError struct {
	HttpStatusCode int
	Err            error
}

func NewStApiHttpError

func NewStApiHttpError(resp *resty.Response) StApiError

func NewStApiReqError

func NewStApiReqError(err error) StApiError

func (StApiError) Error

func (e StApiError) Error() string

func (StApiError) Unwrap

func (e StApiError) Unwrap() error

type SyncthingClient

type SyncthingClient interface {
	GetSystemStatus() (*SystemStatus, error)

	// GetEvents receives events. since sets the ID of the last event you’ve already seen. The default value is 0, which
	// returns all events. The timeout duration can be customized with the parameter timeout(seconds).
	// To receive only a limited number of events, add the limit parameter with a suitable value for n and only the last n
	// events will be returned.
	//
	// Note: if more than one eventTypes filter is given, only subsequent events can be fetched. This function will wait
	// until a new event or timeout. However, if there's only one event type or empty, cached events can be fetched.
	// This is the intended behavior of Syncthing API, details: https://github.com/syncthing/syncthing/issues/8902
	GetEvents(eventTypes []events.EventType, since int, timeout int, limit int) ([]events.Event, error)

	GetDiskEvents(since int, timeout int, limit int) ([]events.Event, error)

	SubscribeEvent(eventTypes []events.EventType, since int) <-chan events.Event

	UnsubscribeEvent(eventCh <-chan events.Event)
}

type SyncthingConfig

type SyncthingConfig struct {
	Url    string `koanf:"url"`
	ApiKey string `koanf:"apikey"`
}

type SystemStatus

type SystemStatus struct {
	Alloc                   int                                `json:"alloc"`
	ConnectionServiceStatus map[string]ConnectionServiceStatus `json:"connectionServiceStatus"`
	DiscoveryEnabled        bool                               `json:"discoveryEnabled"`
	DiscoveryErrors         map[string]string                  `json:"discoveryErrors"`
	DiscoveryStatus         map[string]DiscoveryStatus         `json:"discoveryStatus"`
	DiscoveryMethods        int                                `json:"discoveryMethods"`
	Goroutines              int                                `json:"goroutines"`
	LastDialStatus          map[string]LastDialStatus          `json:"lastDialStatus"`
	MyID                    string                             `json:"myID"`
	PathSeparator           string                             `json:"pathSeparator"`
	StartTime               time.Time                          `json:"startTime"`
	Sys                     int                                `json:"sys"`
	Themes                  []string                           `json:"themes"`
	Tilde                   string                             `json:"tilde"`
	Uptime                  int                                `json:"uptime"`
}

type TimeProvider

type TimeProvider interface {
	NowUnixMilli() int64
}

Jump to

Keyboard shortcuts

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