drone

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2022 License: Apache-2.0 Imports: 10 Imported by: 20

README

drone

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

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

编写插件

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

插件主文件plugin.go

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

package main

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

type plugin struct {
    config *config

    envs                  []string
    fastGithubExe         string
    fastGithubSuccessMark string
    gitExe                string
}

func newPlugin() drone.Plugin {
    return &plugin{
        config: new(config),

        envs:                  make([]string, 0),
        fastGithubExe:         `/opt/fastgithub/fastgithub`,
        fastGithubSuccessMark: `FastGithub启动完成`,
        gitExe:                `git`,
    }
}

func (p *plugin) Configuration() drone.Configuration {
    return p.config
}

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(logger simaqiang.Logger) (undo bool, err error) {
    return
}

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

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

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

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

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

  • github
  • clear
  • ssh
  • pull
  • push
配置文件config.go

配置文件用于插件运行过程中的配置,并实现drone.Configuration接口,大致代码如下

package main

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

type config struct {
	drone.Config

	// 远程仓库地址
	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}}"`
}
启动文件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 Async added in v0.0.3

func Async() *optionAsync

Async 配置异步执行

func Bootstrap

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

Bootstrap 启动插件

func Configs

func Configs(configs ...string) *optionConfigs

Configs 配置组

func Name

func Name(name string) *optionName

Name 配置插件名称

func NewOptions

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

NewOptions 创建选项

func Sync added in v0.0.3

func Sync() *optionAsync

Sync 配置同步执行

Types

type Config

type Config struct {
	// 是否启用默认配置
	Defaults bool `default:"${PLUGIN_DEFAULTS=${DEFAULTS=true}}"`
	// 是否显示详细信息
	Verbose bool `default:"${PLUGIN_VERBOSE=${VERBOSE=false}}"`
	// 是否显示调试信息
	Debug bool `default:"${PLUGIN_DEBUG=${DEBUG=false}}"`

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

Config 插件基础配置

func (*Config) Basic added in v0.0.2

func (c *Config) Basic() *Config

func (*Config) Fields added in v0.0.2

func (c *Config) Fields() gox.Fields

type Configuration added in v0.0.2

type Configuration interface {
	// Fields 导出所有字段
	Fields() gox.Fields

	// Basic 基础配置
	Basic() *Config
}

Configuration 配置

type Plugin added in v0.0.2

type Plugin interface {
	// Configuration 加载配置
	Configuration() (configuration Configuration)

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

Plugin 插件

type Step added in v0.0.2

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

Step 步骤

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