From 149d8bc34fc1955bc46463af5a02ad920ac3d945 Mon Sep 17 00:00:00 2001 From: Don Date: Fri, 6 Sep 2019 16:34:27 -0700 Subject: [PATCH] Add urfave parsing of Step --- go.mod | 5 +++ go.sum | 5 +++ pkg/urfave/urfave.go | 72 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 pkg/urfave/urfave.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..18c0c22 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/drone-plugins/drone-plugin-lib + +go 1.13 + +require github.com/urfave/cli v1.21.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6de7d0f --- /dev/null +++ b/go.sum @@ -0,0 +1,5 @@ +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/urfave/cli v1.21.0 h1:wYSSj06510qPIzGSua9ZqsncMmWE3Zr55KBERygyrxE= +github.com/urfave/cli v1.21.0/go.mod h1:lxDj6qX9Q6lWQxIrbrT0nwecwUtRnhVZAJjJZrVUZZQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/pkg/urfave/urfave.go b/pkg/urfave/urfave.go new file mode 100644 index 0000000..a2bf26e --- /dev/null +++ b/pkg/urfave/urfave.go @@ -0,0 +1,72 @@ +// Copyright (c) 2019, the Drone Plugins project authors. +// Please see the AUTHORS file for details. All rights reserved. +// Use of this source code is governed by an Apache 2.0 license that can be +// found in the LICENSE file. + +// Package urfave provides helpers for interacting with the `urfave/cli` +// package when creating plugins for use by the Drone CI/CD service. +// +// Drone communicates to plugins by passing in environment variables that have +// information on the currently executing build. The `urfave/cli` package can +// read these environment variables and extract them into structs. +// +// import( +// "github.com/drone-plugins/drone-plugin-lib/pkg/urfave" +// "github.com/urfave/cli" +// ) +// +// func main() { +// app := cli.New() +// app.Name = "my awesome Drone plugin" +// app.Run = run +// app.Flags = []cli.Flags{ +// // All my plugin flags +// } +// app.Flags = append( +// app.Flags, +// urfave.CommitFlags()..., +// ) +// } +package urfave + +import ( + "github.com/drone-plugins/drone-plugin-lib/pkg/plugin" + "github.com/urfave/cli" +) + +//--------------------------------------------------------------------- +// Step Flags +//--------------------------------------------------------------------- + +const ( + // StepNameFlag is the flag name for setting plugin.Step.Name. + StepNameFlag = "step.name" + // StepNumberFlag is the flag name for setting plugin.Step.Number. + StepNumberFlag = "step.number" +) + +// StepFlags has the cli.Flags for the plugin.Step. +func StepFlags() []cli.Flag { + return []cli.Flag{ + cli.StringFlag{ + Name: StepNameFlag, + Usage: "step name", + EnvVar: plugin.StepNameEnvVar, + Hidden: true, + }, + cli.StringFlag{ + Name: StepNumberFlag, + Usage: "step number", + EnvVar: plugin.StepNumberEnvVar, + Hidden: true, + }, + } +} + +// StepFromContext creates a plugin.Step from the cli.Context. +func StepFromContext(ctx cli.Context) plugin.Step { + return plugin.Step{ + Name: ctx.String(StepNameFlag), + Number: ctx.Int(StepNumberFlag), + } +}