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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
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()...),
|
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)
|
|
|
|
fmt.Print(strings.TrimSpace(version))
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
if err := SetupConsoleLogger(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|