util

package module
v0.0.0-...-6a757b2 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2023 License: MIT Imports: 23 Imported by: 8

README

util herb-go 工具库

提供了大量用于创建herb-go应用程序的工具函数来构架完整的应用程序框架。

Documentation

Index

Constants

View Source
const (
	StageInit   = Stage(0)
	StageNormal = Stage(5)
	StageFinish = Stage(9)
)
View Source
const EnvAppDataPath = "Path.AppData"
View Source
const EnvConfigPath = "Path.Config"
View Source
const EnvConstantsPath = "Path.Constants"
View Source
const EnvForceDebugMode = "Debug"

EnvForceDebugMode env field to set force demog mode

View Source
const EnvResourcesPath = "Path.Resources"
View Source
const EnvRootPath = "Path.Root"

EnvRootPath env field to set root path

View Source
const EnvSystemPath = "Path.System"
View Source
const ErrTypeFileObjectNotWriteable = ErrorType("notwriteable")
View Source
const ErrTypeFileObjectSchemeNotavaliable = ErrorType("schemenotavaliable")
View Source
const RelativeFileLocationAppData = RelativeFileLocation("appdata")
View Source
const RelativeFileLocationConfig = RelativeFileLocation("config")
View Source
const RelativeFileLocationConstants = RelativeFileLocation("constants")
View Source
const RelativeFileLocationLogs = RelativeFileLocation("logs")
View Source
const RelativeFileLocationResources = RelativeFileLocation("resources")
View Source
const RelativeFileLocationRoot = RelativeFileLocation("")
View Source
const RelativeFileLocationSystem = RelativeFileLocation("system")

Variables

View Source
var AppDataPath string
View Source
var ApplicationLock sync.Mutex
View Source
var Args []string
View Source
var AutoDetectCWD = true
View Source
var ConfigPath string
View Source
var ConstantsPath string
View Source
var Debug = false
View Source
var DebugOutput = os.Stdout
View Source
var DefaultConfigPath string
View Source
var DefaultFileMode = os.FileMode(0660)
View Source
var DefaultFolderMode = os.FileMode(0770)
View Source
var Env = OsEnv
View Source
var ErrorLogger = func(s string) {
	log.Println(s)
}
View Source
var ForceDebug bool

ForceDebug force useing debug mode

View Source
var IDGenerator = NopIDGenerator

IDGenerator id generator

View Source
var IgnoreEnv = false

IgnoreEnv ignore os env settings.

View Source
var Initiators = map[string]modulelist{}
View Source
var IsErrorIgnored = func(err error) bool {
	for k := range LoggerIgnoredErrorsChecker {
		if LoggerIgnoredErrorsChecker[k](err) {
			return true
		}
	}
	return false
}
View Source
var LeaveMessage = "Bye."
View Source
var LoggerIgnoredErrorsChecker = []func(error) bool{
	func(err error) bool {
		if err == http.ErrHandlerTimeout || err == http.ErrAbortHandler {
			return true
		}
		if err == context.Canceled {
			return true
		}
		oe, ok := err.(*net.OpError)
		if ok {
			if oe.Err == syscall.EPIPE || oe.Err == syscall.ECONNRESET {
				return true
			}
			se, ok := oe.Err.(*os.SyscallError)
			if ok && (se.Err == syscall.EPIPE || se.Err == syscall.ECONNRESET) {
				return true
			}
		}
		return false
	},
}
View Source
var LoggerMaxLength = 5
View Source
var LogsPath string
View Source
var Modules = modulelist{}
View Source
var MustChRoot = func() {
	Must(os.Chdir(RootPath))
}
View Source
var OsEnv = NewPrefixEnv("")
View Source
var QuitDelayDuration = 500 * time.Microsecond
View Source
var RegisterLoggerIgnoredErrorsChecker = func(f func(error) bool) {
	LoggerIgnoredErrorsChecker = append(LoggerIgnoredErrorsChecker, f)
}
View Source
var ResourcesPath string
View Source
var RootPath string
View Source
var SecretFileMode = os.FileMode(0600)
View Source
var SignalChan = make(chan os.Signal)
View Source
var Stdout io.Writer = os.Stdout
View Source
var SystemPath string
View Source
var UpdatePaths = func() error {
	if RootPath == "" {
		var execute string
		execute = filepath.Dir(mustPath(os.Executable()))
		if AutoDetectCWD && ConfigPath == "" && !IsPathExists(filepath.Join(execute, "../", "config")) {
			execute = MustGetWD()
		}
		RootPath = filepath.Join(execute, "../")
	}
	if ResourcesPath == "" {
		ResourcesPath = filepath.Join(RootPath, "resources")
	}
	if AppDataPath == "" {
		AppDataPath = filepath.Join(RootPath, "appdata")
	}
	if LogsPath == "" {
		LogsPath = filepath.Join(RootPath, "logs")
	}
	if ConfigPath == "" {
		ConfigPath = filepath.Join(RootPath, "config")
	}
	if SystemPath == "" {
		SystemPath = filepath.Join(RootPath, "system")
	}
	if ConstantsPath == "" {
		ConstantsPath = filepath.Join(SystemPath, "constants")
	}
	if DefaultConfigPath == "" {
		DefaultConfigPath = filepath.Join(SystemPath, "defaultconfig")
	}
	return nil
}

Functions

func AppData

func AppData(filepaths ...string) string

func Bye

func Bye()

func Catch

func Catch(f func()) (err error)

func CleanInitiators

func CleanInitiators()

func CleanModules

func CleanModules()

func CleanWarnings

func CleanWarnings()

func Config

func Config(filepaths ...string) string

func Constants

func Constants(filepaths ...string) string

func DebugPrint

func DebugPrint(args ...interface{})

func DebugPrintf

func DebugPrintf(format string, args ...interface{})

func DebugPrintln

func DebugPrintln(args ...interface{})

func DefaultConfig

func DefaultConfig(filepaths ...string) string

func DelWarning

func DelWarning(module string)

func DelayAndQuit

func DelayAndQuit()

func EnvnameWithPrefix

func EnvnameWithPrefix(envname string) string

func GetHerbEnv

func GetHerbEnv(envname string) string

Getenv get env value with SystemEnvNamePrefix

func GetStackLines

func GetStackLines(from int, to int) []string

func Getenv

func Getenv(key string) string

func HasWarning

func HasWarning() bool

func InitModulesOrderByName

func InitModulesOrderByName(enabledModules ...string)

func InitOrderByName

func InitOrderByName(module string)

func IsPathExists

func IsPathExists(path string) bool

func LogError

func LogError(err error)

func LogIfError

func LogIfError(err error)

func Logs

func Logs(filepaths ...string) string

func MakeLoggerFolderIfNotExist

func MakeLoggerFolderIfNotExist()

func Must

func Must(err error)

func MustGetWD

func MustGetWD() string

func MustLoadRegisteredFolders

func MustLoadRegisteredFolders()

func NewFileObjectNotWriteableError

func NewFileObjectNotWriteableError(id string) error

func NewFileObjectSchemeError

func NewFileObjectSchemeError(id string) error

func NopIDGenerator

func NopIDGenerator() string

NopIDGenerator nop id generator which alawys return empty string

func OnQuit

func OnQuit(handlers ...func())

func OnQuitAndIgnoreError

func OnQuitAndIgnoreError(handlers ...func() error)

func OnQuitAndLogError

func OnQuitAndLogError(handlers ...func() error)

func OnUnloadModules

func OnUnloadModules(f func())

func Print

func Print(args ...interface{}) (n int, err error)

func Printf

func Printf(format string, args ...interface{}) (n int, err error)

func Println

func Println(args ...interface{}) (n int, err error)

func Quit

func Quit()

func QuitChan

func QuitChan() chan int

func Recover

func Recover()

func RecoverAndExit

func RecoverAndExit()

func RegisterDataFolder

func RegisterDataFolder(folder ...string) string

func RegisterInitiator

func RegisterInitiator(module string, Name string, handler func())

func Resources

func Resources(filepaths ...string) string

func Root

func Root(filepaths ...string) string

func SetHerbEnv

func SetHerbEnv(envname string, val string)

func SetSystemEnvNamePrefix

func SetSystemEnvNamePrefix(p string)

SetSystemEnvNamePrefix set system env name prefix

func SetWarning

func SetWarning(module string, info ...string)

func Setenv

func Setenv(key string, value string) error

func System

func System(filepaths ...string) string

func SystemEnvNamePrefix

func SystemEnvNamePrefix() string

SystemEnvNamePrefix system env name prefix

func UnloadModules

func UnloadModules()

func WaitingQuit

func WaitingQuit()

func Warnings

func Warnings() map[string][]string

Types

type AppEnv

type AppEnv interface {
	Getenv(string) string
	Setenv(string, string) error
}

type ErrorType

type ErrorType string

func GetErrorType

func GetErrorType(err error) ErrorType

type FileObjectError

type FileObjectError struct {
	Type   ErrorType
	Msg    string
	FileID string
}

func (*FileObjectError) Error

func (f *FileObjectError) Error() string

type Module

type Module struct {
	Stage    Stage
	Name     string
	Handler  func()
	Position string
}

func RegisterModule

func RegisterModule(Name string, handler func()) Module

func (*Module) Load

func (m *Module) Load()

type PrefixEnv

type PrefixEnv struct {
	Prefix string
}

func NewPrefixEnv

func NewPrefixEnv(prefix string) *PrefixEnv

func (*PrefixEnv) Getenv

func (e *PrefixEnv) Getenv(key string) string

func (*PrefixEnv) Setenv

func (e *PrefixEnv) Setenv(key string, value string) error

type PresetEnv

type PresetEnv map[string]string

func (PresetEnv) Getenv

func (e PresetEnv) Getenv(key string) string

func (PresetEnv) Setenv

func (e PresetEnv) Setenv(key string, value string) error

type RelativeFile

type RelativeFile struct {
	Location RelativeFileLocation
	Path     string
}

func AppDataFile

func AppDataFile(filepath ...string) *RelativeFile

func ConfigFile

func ConfigFile(filepath ...string) *RelativeFile

func ConstantsFile

func ConstantsFile(filepath ...string) *RelativeFile

func NewRelativeFile

func NewRelativeFile() *RelativeFile

func ResourcesFile

func ResourcesFile(filepath ...string) *RelativeFile

func RootFile

func RootFile(filepath ...string) *RelativeFile

func SystemFile

func SystemFile(filepath ...string) *RelativeFile

func (*RelativeFile) AbsolutePath

func (f *RelativeFile) AbsolutePath() string

func (*RelativeFile) ID

func (f *RelativeFile) ID() string

func (*RelativeFile) ReadRaw

func (f *RelativeFile) ReadRaw() ([]byte, error)

type RelativeFileLocation

type RelativeFileLocation string

type Stage

type Stage int

func (Stage) RegisterModule

func (stage Stage) RegisterModule(Name string, handler func()) Module

Jump to

Keyboard shortcuts

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