drone

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2022 License: Apache-2.0 Imports: 13 Imported by: 19

README

drone

Drone插件基础框架,提供如下功能

  • 基础配置
    • 重试
    • 背压
    • 配置解析
    • 命名
  • 接口抽象
    • 步骤
    • 配置
    • 插件

编写插件

使用drone库编写插件非常简单,只需要两步

插件主文件plugin.go

用于描述插件的主文件,主要是实现drone.Plugin接口,大致代码如下

package main

import (
    `github.com/dronestock/drone`
)

type plugin struct {
    drone.PluginBase

    // 远程仓库地址
    Remote string `default:"${PLUGIN_REMOTE=${REMOTE=${DRONE_GIT_HTTP_URL}}}" validate:"required"`
    // 模式
    Mode string `default:"${PLUGIN_MODE=${MODE=push}}"`
    // SSH密钥
    SSHKey string `default:"${PLUGIN_SSH_KEY=${SSH_KEY}}"`
    // 目录
    Folder string `default:"${PLUGIN_FOLDER=${FOLDER=.}}" validate:"required"`
    // 目录列表
    Folders []string `default:"${PLUGIN_FOLDERS=${FOLDERS}}"`
    // 分支
    Branch string `default:"${PLUGIN_BRANCH=${BRANCH=master}}" validate:"required_without=Commit"`
    // 标签
    Tag string `default:"${PLUGIN_TAG=${TAG}}"`
    // 作者
    Author string `default:"${PLUGIN_AUTHOR=${AUTHOR=${DRONE_COMMIT_AUTHOR}}}"`
    // 邮箱
    Email string `default:"${PLUGIN_EMAIL=${EMAIL=${DRONE_COMMIT_AUTHOR_EMAIL}}}"`
    // 提交消息
    Message string `default:"${PLUGIN_MESSAGE=${MESSAGE=${PLUGIN_COMMIT_MESSAGE=drone}}}"`
    // 是否强制提交
    Force bool `default:"${PLUGIN_FORCE=${FORCE=true}}"`

    // 子模块
    Submodules bool `default:"${PLUGIN_SUBMODULES=${SUBMODULES=true}}"`
    // 深度
    Depth int `default:"${PLUGIN_DEPTH=${DEPTH=50}}"`
    // 提交
    Commit string `default:"${PLUGIN_COMMIT=${COMMIT=${DRONE_COMMIT}}}" validate:"required_without=Branch"`

    // 是否清理
    Clear bool `default:"${PLUGIN_CLEAR=${CLEAR=true}}"`
}

func newPlugin() drone.Plugin {
    return new(plugin)
}

func (p *plugin) Config() drone.Config {
    return p
}

func (p *plugin) Steps() []*drone.Step {
    return []*drone.Step{
        drone.NewStep(p.github, drone.Name(`Github加速`)),
        drone.NewStep(p.clear, drone.Name(`清理Git目录`)),
        drone.NewStep(p.ssh, drone.Name(`写入SSH配置`)),
        drone.NewStep(p.pull, drone.Name(`拉代码`)),
        drone.NewStep(p.push, drone.Name(`推代码`)),
    }
}

// 业务逻辑代码
func (p *plugin) github() (undo bool, err error) {
    return
}

// 业务逻辑代码
func (p *plugin) clear() (undo bool, err error) {
    return
}

// 业务逻辑代码
func (p *plugin) ssh() (undo bool, err error) {
    return
}

// 业务逻辑代码
func (p *plugin) pull() (undo bool, err error) {
    return
}

// 业务逻辑代码
func (p *plugin) push() (undo bool, err error) {
    return
}

其中,需要实现的方法步骤有

  • github
  • clear
  • ssh
  • pull
  • push
启动文件main.go

启动文件负责启动整个程序,是一个非常轻量的壳

package main

import (
	`github.com/dronestock/drone`
)

func main() {
	panic(drone.Bootstrap(newPlugin, drone.Configs(`FOLDERS`)))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Aliases added in v0.1.2

func Aliases(aliases ...*alias) *optionAliases

Aliases 别名

func Args added in v0.1.1

func Args(args ...interface{}) *optionArgs

Args 参数

func Async added in v0.0.3

func Async() *optionAsync

Async 配置异步执行

func Bootstrap

func Bootstrap(constructor constructor, opts ...option) (err error)

Bootstrap 启动插件

func Break added in v0.0.7

func Break() *optionBreak

Break 遇到错误中断执行

func Contains added in v0.1.3

func Contains(contains string) *optionContains

Contains 检查是否包含字符串

func Continue added in v0.0.6

func Continue() *optionBreak

Continue 遇到错误继续执行

func Dir added in v0.1.0

func Dir(dir string) *optionDir

Dir 配置命令执行目录

func DisablePwe added in v0.3.9

func DisablePwe() *optionPwe

DisablePwe 禁用PWE

func Env added in v0.3.3

func Env(key string, value string) *optionEnvironment

Env 环境变量

func Environment added in v0.3.3

func Environment(key string, value string) *optionEnvironment

Environment 环境变量

func Equal added in v0.1.3

func Equal(equal string) *optionEqual

Equal 检查是否字符串相等

func Fields added in v0.1.1

func Fields(fields ...gox.Field) *optionFields

Fields 字段

func Interrupt added in v0.0.6

func Interrupt() *optionRetry

Interrupt 遇到错误中断执行

func Name

func Name(name string) *optionName

Name 配置插件名称

func NewAlias added in v0.1.2

func NewAlias(name string, value string) *alias

NewAlias 创建别名

func NewExecOptions added in v0.3.9

func NewExecOptions(options ...execOption) []execOption

NewExecOptions 创建运行选项

func NewOptions

func NewOptions(options ...option) []option

NewOptions 创建选项

func Pwe added in v0.3.9

func Pwe() *optionPwe

Pwe 启用PWE

func Retry added in v0.0.8

func Retry() *optionRetry

Retry 遇到错误重试

func String added in v0.3.6

func String(output *string) *optionString

String 字符串输出

func StringEnv added in v0.3.2

func StringEnv(env string) *optionStringEnvironments

StringEnv 环境变量

func StringEnvironment added in v0.3.3

func StringEnvironment(environment string) *optionStringEnvironments

StringEnvironment 环境变量

func StringEnvironments added in v0.3.3

func StringEnvironments(environments ...string) *optionStringEnvironments

StringEnvironments 环境变量列表

func StringEnvs added in v0.3.2

func StringEnvs(envs ...string) *optionStringEnvironments

StringEnvs 环境变量列表

func Sync added in v0.0.3

func Sync() *optionAsync

Sync 配置同步执行

Types

type Base added in v0.1.0

type Base struct {
	simaqian.Logger

	// 是否启用默认配置
	Defaults bool `default:"${PLUGIN_DEFAULTS=${DEFAULTS=true}}"`
	// 是否显示详细信息
	Verbose bool `default:"${PLUGIN_VERBOSE=${VERBOSE=false}}"`
	// 是否显示调试信息
	Debug bool `default:"${PLUGIN_DEBUG=${DEBUG=false}}"`
	// 是否在出错时打印输出
	Pwe bool `default:"${PLUGIN_PWE=${PWE=true}}"`

	// 是否重试
	Retry bool `default:"${PLUGIN_RETRY=${RETRY=true}}"`
	// 重试次数
	Counts int `default:"${PLUGIN_COUNTS=${COUNTS=5}}"`
	// 重试间隔
	Backoff time.Duration `default:"${PLUGIN_BACKOFF=${BACKOFF=5s}}"`
}

Base 插件基础

func (*Base) BaseConfig added in v0.3.7

func (b *Base) BaseConfig() *Base

func (*Base) Exec added in v0.1.0

func (b *Base) Exec(command string, opts ...execOption) error

func (*Base) Fields added in v0.1.0

func (b *Base) Fields() gox.Fields

func (*Base) Parse added in v0.1.0

func (b *Base) Parse(to map[string]string, configs ...string)

func (*Base) Parses added in v0.1.0

func (b *Base) Parses(to map[string][]string, configs ...string)

func (*Base) Setup added in v0.1.0

func (b *Base) Setup() (unset bool, err error)

type Config

type Config interface {
	// Setup 设置配置信息
	Setup() (unset bool, err error)

	// Fields 导出所有字段
	Fields() gox.Fields

	// BaseConfig 基础配置
	BaseConfig() *Base
}

Config 配置

type Plugin added in v0.0.2

type Plugin interface {
	// Config 加载配置
	Config() (config Config)

	// Steps 插件运行步骤
	Steps() []*Step
}

Plugin 插件接口,任何插件都需要实现这个接口

type Step added in v0.0.2

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

Step 步骤

func NewDefaultDelayStep added in v0.2.5

func NewDefaultDelayStep() *Step

NewDefaultDelayStep 创建延迟步骤,调试使用

func NewDelayStep added in v0.2.5

func NewDelayStep(delay time.Duration) *Step

NewDelayStep 创建延迟步骤,调试使用

func NewStep added in v0.0.2

func NewStep(do do, opts ...stepOption) *Step

NewStep 创建一个步骤

Jump to

Keyboard shortcuts

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