2023-08-11 10:27:16 +00:00
|
|
|
// Copyright 2023 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-13 20:03:55 +00:00
|
|
|
"fmt"
|
2023-08-16 13:02:13 +00:00
|
|
|
"net/url"
|
2023-08-11 10:27:16 +00:00
|
|
|
"os"
|
2023-08-16 13:02:13 +00:00
|
|
|
"strconv"
|
2023-08-13 20:03:55 +00:00
|
|
|
"strings"
|
2023-08-11 10:27:16 +00:00
|
|
|
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2024-05-03 20:51:11 +00:00
|
|
|
//nolint:lll
|
|
|
|
const appHelpTemplate = `NAME:
|
|
|
|
{{template "helpNameTemplate" .}}
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}{{if .Args}}[arguments...]{{end}}{{end}}{{end}}{{if .Version}}{{if not .HideVersion}}
|
|
|
|
|
|
|
|
VERSION:
|
|
|
|
{{.Version}}{{end}}{{end}}{{if .Description}}
|
|
|
|
|
|
|
|
DESCRIPTION:
|
|
|
|
{{template "descriptionTemplate" .}}{{end}}
|
|
|
|
{{- if len .Authors}}
|
|
|
|
|
|
|
|
AUTHOR{{template "authorsTemplate" .}}{{end}}{{if .VisibleCommands}}
|
|
|
|
|
|
|
|
COMMANDS:{{template "visibleCommandCategoryTemplate" .}}{{end}}{{if .VisibleFlagCategories}}
|
|
|
|
|
|
|
|
GLOBAL OPTIONS:{{range .VisibleFlagCategories}}{{if and .Name (ne .Name "Plugin Flags")}}{{continue}}{{end}}
|
|
|
|
{{if .Name}}{{.Name}}
|
|
|
|
|
|
|
|
{{end}}{{$flglen := len .Flags}}{{range $i, $e := .Flags}}{{if eq (subtract $flglen $i) 1}}{{$e}}
|
|
|
|
{{else}}{{$e}}
|
|
|
|
{{end}}{{end}}{{end}}{{else if .VisibleFlags}}
|
|
|
|
|
|
|
|
GLOBAL OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}{{if .Copyright}}
|
|
|
|
|
|
|
|
COPYRIGHT:
|
|
|
|
{{template "copyrightTemplate" .}}{{end}}
|
|
|
|
`
|
|
|
|
|
2023-08-11 10:27:16 +00:00
|
|
|
// Options defines the options for the plugin.
|
|
|
|
type Options struct {
|
|
|
|
// Name of the plugin.
|
|
|
|
Name string
|
|
|
|
// Description of the plugin.
|
|
|
|
Description string
|
|
|
|
// Version of the plugin.
|
|
|
|
Version string
|
2023-08-13 20:03:55 +00:00
|
|
|
// Version metadata of the plugin.
|
|
|
|
VersionMetadata string
|
2023-08-11 10:27:16 +00:00
|
|
|
// Flags of the plugin.
|
|
|
|
Flags []cli.Flag
|
|
|
|
// Execute function of the plugin.
|
|
|
|
Execute ExecuteFunc
|
2024-05-03 20:51:11 +00:00
|
|
|
// Hide woodpecker system flags.
|
|
|
|
HideWoodpeckerFlags bool
|
2023-08-11 10:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Plugin defines the plugin instance.
|
|
|
|
type Plugin struct {
|
2023-08-21 09:04:35 +00:00
|
|
|
App *cli.App
|
2023-08-11 10:27:16 +00:00
|
|
|
execute ExecuteFunc
|
2023-08-21 09:04:35 +00:00
|
|
|
Context *cli.Context
|
2023-08-15 20:46:42 +00:00
|
|
|
// Network options.
|
|
|
|
Network Network
|
2023-08-11 10:27:16 +00:00
|
|
|
// Metadata of the current pipeline.
|
|
|
|
Metadata Metadata
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecuteFunc defines the function that is executed by the plugin.
|
2023-08-21 09:04:35 +00:00
|
|
|
type ExecuteFunc func(ctx context.Context) error
|
2023-08-11 10:27:16 +00:00
|
|
|
|
|
|
|
// New plugin instance.
|
|
|
|
func New(opt Options) *Plugin {
|
|
|
|
if _, err := os.Stat("/run/woodpecker/env"); err == nil {
|
|
|
|
_ = godotenv.Overload("/run/woodpecker/env")
|
|
|
|
}
|
|
|
|
|
2024-05-07 08:10:59 +00:00
|
|
|
_ = SetupConsoleLogger(nil)
|
|
|
|
|
2023-08-11 10:27:16 +00:00
|
|
|
app := &cli.App{
|
2023-08-15 22:16:07 +00:00
|
|
|
Name: opt.Name,
|
|
|
|
Usage: opt.Description,
|
|
|
|
Version: opt.Version,
|
|
|
|
Flags: append(opt.Flags, Flags()...),
|
2024-05-03 20:51:11 +00:00
|
|
|
Before: SetupConsoleLogger,
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt.HideWoodpeckerFlags {
|
|
|
|
app.CustomAppHelpTemplate = appHelpTemplate
|
2023-08-11 10:27:16 +00:00
|
|
|
}
|
|
|
|
|
2023-08-13 20:03:55 +00:00
|
|
|
cli.VersionPrinter = func(c *cli.Context) {
|
|
|
|
version := fmt.Sprintf("%s version=%s %s\n", c.App.Name, c.App.Version, opt.VersionMetadata)
|
2024-05-04 13:50:28 +00:00
|
|
|
fmt.Println(strings.TrimSpace(version))
|
2023-08-13 20:03:55 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 10:27:16 +00:00
|
|
|
plugin := &Plugin{
|
2023-08-21 09:04:35 +00:00
|
|
|
App: app,
|
2023-08-11 10:27:16 +00:00
|
|
|
execute: opt.Execute,
|
|
|
|
}
|
2023-08-21 09:04:35 +00:00
|
|
|
plugin.App.Action = plugin.action
|
2023-08-11 10:27:16 +00:00
|
|
|
|
|
|
|
return plugin
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugin) action(ctx *cli.Context) error {
|
|
|
|
p.Metadata = MetadataFromContext(ctx)
|
2023-08-15 20:46:42 +00:00
|
|
|
p.Network = NetworkFromContext(ctx)
|
2023-08-21 09:04:35 +00:00
|
|
|
p.Context = ctx
|
2023-08-11 10:27:16 +00:00
|
|
|
|
2023-08-16 13:02:13 +00:00
|
|
|
if p.Metadata.Pipeline.URL == "" {
|
|
|
|
url, err := url.JoinPath(
|
|
|
|
p.Metadata.System.URL,
|
|
|
|
"repos",
|
|
|
|
p.Metadata.Repository.Slug,
|
|
|
|
"pipeline",
|
|
|
|
strconv.FormatInt(p.Metadata.Pipeline.Number, 10),
|
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
p.Metadata.Pipeline.URL = url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-11 10:27:16 +00:00
|
|
|
if p.execute == nil {
|
|
|
|
panic("plugin execute function is not set")
|
|
|
|
}
|
|
|
|
|
2023-08-21 09:04:35 +00:00
|
|
|
return p.execute(ctx.Context)
|
2023-08-11 10:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the plugin.
|
|
|
|
func (p *Plugin) Run() {
|
2023-08-21 09:04:35 +00:00
|
|
|
if err := p.App.Run(os.Args); err != nil {
|
2023-08-11 10:27:16 +00:00
|
|
|
log.Error().Err(err).Msg("execution failed")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|