appcore

package
v0.0.0-...-a23f37e Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2024 License: MIT Imports: 17 Imported by: 0

README

该包主要功能是启动项目,并读取配置文件,然后调用客户端指定的启动函数,用来启动项目

示例代码如下:

import (
	"fmt"
	"gitee.com/tylf2018/go-micro-framework/pkg/appcore"
	cliflag "gitee.com/tylf2018/go-micro-framework/pkg/common/cli/flag"
	"gitee.com/tylf2018/go-micro-framework/pkg/logger"
)
// 配置文件
var configOptions = config.NewConfig()

func main() {

	appcore.NewCoreApp("hello_test", "test",
		appcore.WithOptions(configOptions),
		appcore.WithRunFunc(run),
	).Run()
}

// 回调的执行函数
func run(basename string) error {
	fmt.Println(configOptions)
	return nil
}



type Config struct {
	Log *logger.Options `json:"log" mapstructure:"log"` // log日志包 相关配置
}

func NewConfig() *Config {
	return &Config{
		Log: logger.NewOptions(),
	}
}
// 配置文件需要实现CliConfigOptions接口,用来给flag注册命令行参数和校验配置文件的准确性
func (c *Config) Flags() (fss cliflag.NamedFlagSets) {
	c.Log.AddFlags(fss.FlagSet("log"))
	return
}

func (c *Config) Validate() []error {
	var errors []error
	errors = append(errors, c.Log.Validate())
	return errors
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatBaseName

func FormatBaseName(basename string) string

FormatBaseName 根据给定的名称,根据不同操作系统格式化为可执行文件名

Types

type CliConfigOptions

type CliConfigOptions interface {
	Flags() (fss cliflag.NamedFlagSets) // 生成一个被 cliflag.NamedFlagSets 封装后的日志对象
	Validate() []error                  // 添加你想要的验证函数
}

CliConfigOptions abstracts configuration options for reading parameters from the command line. 配置文件实现的的添加和验证flag命令行的接口

type Command

type Command struct {
	// contains filtered or unexported fields
}

Command 是 cli 应用程序的子命令结构。建议使用 app.NewCommand() 函数创建命令

func NewCommand

func NewCommand(usage string, desc string, opts ...CommandOption) *Command

NewCommand 根据给定的命令名称和其他选项创建一个新的子命令实例

func (*Command) AddCommand

func (c *Command) AddCommand(cmds ...*Command)

AddCommand 用于将多个子命令添加到当前命令

type CommandOption

type CommandOption func(*Command)

CommandOption 定义了初始化命令结构的可选参数

func WithCommandOptions

func WithCommandOptions(opt CliConfigOptions) CommandOption

WithCommandOptions 方法开启应用程序的命令行读取功能

func WithCommandRunFunc

func WithCommandRunFunc(run RunCommandFunc) CommandOption

WithCommandRunFunc 用于设置应用程序命令的启动回调函数选项

type CoreApp

type CoreApp struct {
	// contains filtered or unexported fields
}

CoreApp 是一个命令行应用程序的主要结构。 建议使用 app.NewCoreApp() 函数创建一个 app

func NewCoreApp

func NewCoreApp(name string, basename string, opts ...Option) *CoreApp

NewCoreApp 根据给定的应用程序名称、项目文件名称以及其他选项创建一个新的应用程序实例

func (*CoreApp) AddCommand

func (a *CoreApp) AddCommand(cmds ...*Command)

AddCommand 向应用程序添加多个子命令

func (*CoreApp) Command

func (a *CoreApp) Command() *cobra.Command

Command 方法返回应用程序中的 cobra.Command 实例

func (*CoreApp) Run

func (a *CoreApp) Run()

Run 用于启动应用程序

type Option

type Option func(*CoreApp)

Option 定义了初始化应用程序结构的可选参数

func WithDefaultValidArgs

func WithDefaultValidArgs() Option

WithDefaultValidArgs set default validation function to valid non-flag arguments.

func WithDescription

func WithDescription(desc string) Option

WithDescription 用于设置应用程序的描述

func WithNoConfig

func WithNoConfig() Option

WithNoConfig 设置应用程序 不使用 config 文件

func WithNoVersion

func WithNoVersion() Option

WithNoVersion 设置应用程序不提供版本标志

func WithOptions

func WithOptions(opt CliConfigOptions) Option

WithOptions 用于开启应用程序从命令行读取参数或从配置文件中读取参数的功能

func WithRunFunc

func WithRunFunc(run RunFunc) Option

WithRunFunc 用于设置应用程序启动回调函数的选项

func WithSilence

func WithSilence() Option

WithSilence 用于将应用程序设置为静默模式,在该模式下,程序启动信息、配置信息和版本信息不会在控制台中打印出来

func WithValidArgs

func WithValidArgs(args cobra.PositionalArgs) Option

WithValidArgs set the validation function to valid non-flag arguments.

type RunCommandFunc

type RunCommandFunc func(args []string) error

RunCommandFunc 定义了应用程序的子命令启动回调函数

type RunFunc

type RunFunc func(basename string) error

RunFunc 定义应用程序的启动回调函数

Jump to

Keyboard shortcuts

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