service

package module
v0.0.0-...-97d6017 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2023 License: Apache-2.0 Imports: 18 Imported by: 0

README

win-helper

win-helper

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNameFieldRequired is returned when Config.Name is empty.
	ErrNameFieldRequired = errors.New("缺失配置文件")
	// ErrNoServiceSystemDetected is returned when no system was detected.
	ErrNoServiceSystemDetected = errors.New("未检测到服务系统")
	// ErrNotInstalled is returned when the service is not installed.
	ErrNotInstalled = errors.New("服务未安装")
	ErrNotSuport    = errors.New("服务不支持")
)
View Source
var ConsoleLogger = consoleLogger{}

ConsoleLogger logs to the std err.

View Source
var ControlAction = [5]string{"start", "stop", "restart", "install", "uninstall"}

ControlAction list valid string texts to use in Control.

Functions

func ChooseSystem

func ChooseSystem(a ...System)

ChooseSystem chooses a system from the given system services. SystemServices are considered in the order they are suggested. Calling this may change what Interactive and Platform return.

func Control

func Control(s Service, action string) error

Control issues control functions to the service from a given action string.

func Interactive

func Interactive() bool

Interactive returns false if running under the OS service manager and true otherwise.

func Platform

func Platform() string

Platform returns a description of the system service.

Types

type Config

type Config struct {
	Name        string   // Required name of the service. No spaces suggested.
	DisplayName string   // Display name, spaces allowed.
	Description string   // Long description of service.
	UserName    string   // Run as username.
	Arguments   []string // Run with arguments.

	// Optional field to specify the executable for service.
	// If empty the current executable is used.
	Executable string

	// Array of service dependencies.
	// Not yet fully implemented on Linux or OS X:
	//  1. Support linux-systemd dependencies, just put each full line as the
	//     element of the string array, such as
	//     "After=network.target syslog.target"
	//     "Requires=syslog.target"
	//     Note, such lines will be directly appended into the [Unit] of
	//     the generated service config file, will not check their correctness.
	Dependencies []string

	// The following fields are not supported on Windows.
	WorkingDirectory string // Initial working directory.
	ChRoot           string

	// System specific options.
	Option KeyValue

	EnvVars map[string]string
}

type Interface

type Interface interface {
	Start(s Service) error
	Stop(s Service) error
}

type KeyValue

type KeyValue map[string]interface{}

type Logger

type Logger interface {
	Error(v ...interface{}) error
	Warning(v ...interface{}) error
	Info(v ...interface{}) error

	Errorf(format string, a ...interface{}) error
	Warningf(format string, a ...interface{}) error
	Infof(format string, a ...interface{}) error
}

Logger writes to the system log.

type Service

type Service interface {
	// Run should be called shortly after the program entry point.
	// After Interface.Stop has finished running, Run will stop blocking.
	// After Run stops blocking, the program must exit shortly after.
	Run() error

	// Start signals to the OS service manager the given service should start.
	Start() error

	// Stop signals to the OS service manager the given service should stop.
	Stop() error

	// Restart signals to the OS service manager the given service should stop then start.
	Restart() error

	// Install setups up the given service in the OS service manager. This may require
	// greater rights. Will return an error if it is already installed.
	Install() error

	// Uninstall removes the given service from the OS service manager. This may require
	// greater rights. Will return an error if the service is not present.
	Uninstall() error

	// Opens and returns a system logger. If the user program is running
	// interactively rather then as a service, the returned logger will write to
	// os.Stderr. If errs is non-nil errors will be sent on errs as well as
	// returned from Logger's functions.
	Logger(errs chan<- error) (Logger, error)

	// SystemLogger opens and returns a system logger. If errs is non-nil errors
	// will be sent on errs as well as returned from Logger's functions.
	SystemLogger(errs chan<- error) (Logger, error)

	// String displays the name of the service. The display name if present,
	// otherwise the name.
	String() string

	// Platform displays the name of the system that manages the service.
	// In most cases this will be the same as service.Platform().
	Platform() string

	// Status returns the current service status.
	Status() (Status, error)
}

func NewService

func NewService(i Interface, c *Config) (Service, error)

NewService creates a new service based on a service interface and configuration.

type Status

type Status byte

Status represents service status as an byte value

const (
	StatusUnknown Status = iota // Status is unable to be determined due to an error or it was not installed.
	StatusRunning
	StatusStopped
	StatusUninstall
)

Status of service represented as an byte

type System

type System interface {
	// String returns a description of the system.
	String() string

	// Detect returns true if the system is available to use.
	Detect() bool

	// Interactive returns false if running under the system service manager
	// and true otherwise.
	Interactive() bool

	// New creates a new service for this system.
	New(i Interface, c *Config) (Service, error)
}

func AvailableSystems

func AvailableSystems() []System

AvailableSystems returns the list of system services considered when choosing the system service.

func ChosenSystem

func ChosenSystem() System

ChosenSystem returns the system that service will use.

Directories

Path Synopsis
example
cli

Jump to

Keyboard shortcuts

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