zmodule

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

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

Go to latest
Published: Jun 10, 2018 License: MIT Imports: 15 Imported by: 0

README

zmodule

正的模块模板


Launch line

[START]
zmodule.Main() => event.Init() => SYS.init
  |
program.Start() => SYS.start
  |
SYS.stop => go run()

[STOP]
program.STOP()

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Version string // `git describe --tags`

	Author     string // zhengxiaoyao0716
	Homepage   string // https://zhengxiaoyao0716.github.io/zmodule
	Repository string // https://github.com/zhengxiaoyao0716/zmodule
	License    string // https://github.com/zhengxiaoyao0716/zmodule/blob/master/LICENSE

	Built     string // `date +%FT%T%z`
	GitCommit string // `git rev-parse --short HEAD`
	GoVersion string // `go version`
)

Those values should be assigned during compile statement. ``` bash m=github.com/zhengxiaoyao0716/zmodule v=`git describe --tags` b=`date +%FT%T%z` go run -ldflags "-X $m.Version=$v -X $m.Built=$b ..." zexample.go version ``` But some of them may not need to changed frequently. So you can also set them in your code directly.

View Source
var Args = map[string]Argument{
	"config":   {"", "Config json file path."},
	"work_dir": {"", "Directory to find or storage files."},
	"log":      {"", "Path to storage logger files."},
}

Args . You can add your custom args directly, and then they would be parsed into config. Please don't change both the name and the type of the args reserved below.

View Source
var Cmds = map[string]Command{
	"": {
		"Quick run with last args.",
		func(parsed string, args []string) {
			if !config.HasSavedCfg() {
				parseFlag(args)
			}
			if err := srv.Run(); err != nil {

				if !service.Interactive() {
					file, err := os.OpenFile(filepath.Join(info.WorkDir(), "error.log"), os.O_APPEND|os.O_CREATE, 0666)
					if err != nil {

					}
					defer file.Close()

					log.SetOutput(file)
				}

				log.Fatalln(err)
			}
		},
	},
	"run": {
		"Run.",
		func(parsed string, args []string) {
			parseFlag(args)
			if err := srv.Run(); err != nil {
				log.Fatalln(err)
			}
		},
	},
	"version": {
		"Show the version.",
		func(string, []string) { console.Log(info.Version()) },
	},
	"info": {
		"Show the info.",
		func(string, []string) { console.Log(info.Info()) },
	},
	"service": {
		"Control the system service.",
		func() func(string, []string) {
			var handler func(string, []string)
			handler = func(parsed string, args []string) {
				control := func(arg string) {
					err := service.Control(srv, arg)
					if err != nil {
						log.Fatal(err)
					}
				}

				cmdHandler := ParseCmd(map[string]Command{
					"": {
						"Quick start with last args.",
						func(parsed string, args []string) {
							if !config.HasSavedCfg() {
								parseFlag(args)
							}
							control("start")
						},
					},
					"start": {
						"Start the service.",
						func(parsed string, args []string) {
							parseFlag(args)
							control("start")
						},
					},
					"stop": {
						"Stop the service.",
						func(string, []string) { control("stop") },
					},
					"restart": {
						"Restart the service.",
						func(string, []string) { control("restart") },
					},
					"install": {
						"Install the service.",
						func(string, []string) { control("install") },
					},
					"uninstall": {
						"Uninstall the service.",
						func(string, []string) { control("uninstall") },
					},
				})

				handler = func(parsed string, args []string) {
					if len(args) == 0 {
						args = []string{""}
					}
					cmdHandler(parsed, args)
				}
				handler(parsed, args)
			}
			return handler
		}(),
	},
}

Cmds .

Functions

func Main

func Main(name string, scfg *service.Config, run func(), cusKs ...[2]string)

Main .

func ParseCmd

func ParseCmd(cmds map[string]Command) func(parsed string, args []string)

ParseCmd .

func ParseFlag

func ParseFlag(args []string) config.C

ParseFlag parsed the remained args, load them to config, then dump the config to save them.

Types

type Argument

type Argument struct {
	Default interface{}
	Usage   string
}

Argument .

type Command

type Command struct {
	Usage   string
	Handler func(string, []string)
}

Command .

Directories

Path Synopsis
Package event can be used independence without zmodule.
Package event can be used independence without zmodule.
Package info helper provide some global static single instance.
Package info helper provide some global static single instance.

Jump to

Keyboard shortcuts

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