Merge pull request #8 from drone-plugins/simplify-and-fix

Read spec and drop command package
This commit is contained in:
Thomas Boerger
2018-10-21 17:13:22 +02:00
committed by GitHub
5 changed files with 68 additions and 174 deletions
Generated
+18 -3
View File
@@ -2,43 +2,58 @@
[[projects]]
digest = "1:4333e9d9cc731d01e67e19c33da46e95860b95f8b8032bc586d69b169e1b0c1b"
name = "github.com/aymerick/raymond"
packages = [
".",
"ast",
"lexer",
"parser"
"parser",
]
pruneopts = "UT"
revision = "2eebd0f5dd9564c0c6e439df11d8a3c7b9b9ab11"
version = "v2.0.2"
[[projects]]
digest = "1:0ef770954bca104ee99b3b6b7f9b240605ac03517d9f98cbc1893daa03f3c038"
name = "github.com/coreos/go-semver"
packages = ["semver"]
pruneopts = "UT"
revision = "8ab6407b697782a06568d4b7f1db25550ec2e4c6"
version = "v0.2.0"
[[projects]]
branch = "master"
digest = "1:424d6a3ee46171455054a1d821c658c22802a798ba8e687fdc2edaca9eb30c8a"
name = "github.com/drone/drone-template-lib"
packages = ["template"]
pruneopts = "UT"
revision = "5748d3149f4859c6d6cf43edb4fa35bba379c918"
[[projects]]
digest = "1:40e195917a951a8bf867cd05de2a46aaf1806c50cf92eebf4c16f78cd196f747"
name = "github.com/pkg/errors"
packages = ["."]
pruneopts = "UT"
revision = "645ef00459ed84a119197bfb8d8205042c6df63d"
version = "v0.8.0"
[[projects]]
branch = "master"
digest = "1:3fb803863fd713550106aa42c4545b428401c6aad1002de19b1e634339e9cef6"
name = "github.com/urfave/cli"
packages = ["."]
revision = "8e01ec4cd3e2d84ab2fe90d8210528ffbb06d8ff"
pruneopts = "UT"
revision = "934abfb2f102315b5794e15ebc7949e4ca253920"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "0fe5369d1efd31b54aea7d9c97316da432b06cf9a01e0a0c4a46ec4a5f148446"
input-imports = [
"github.com/coreos/go-semver/semver",
"github.com/drone/drone-template-lib/template",
"github.com/pkg/errors",
"github.com/urfave/cli",
]
solver-name = "gps-cdcl"
solver-version = 1
-4
View File
@@ -1,7 +1,3 @@
[[constraint]]
name = "github.com/aymerick/raymond"
version = "2.0.2"
[[constraint]]
name = "github.com/coreos/go-semver"
version = "0.2.0"
-102
View File
@@ -1,102 +0,0 @@
package command
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
"github.com/pkg/errors"
)
type Command struct {
username string
password string
spec string
platforms []string
target string
template string
path string
ignoreMissing bool
}
func New(opts ...Option) *Command {
c := &Command{}
for _, opt := range opts {
opt(c)
}
return c
}
func (c *Command) Exec() error {
args := []string{}
if c.username != "" {
args = append(args, fmt.Sprintf("--username=%s", c.username))
}
if c.password != "" {
args = append(args, fmt.Sprintf("--password=%s", c.password))
}
args = append(args, "push")
if c.spec != "" {
tmpfile, err := ioutil.TempFile(c.path, "manifest-")
if err != nil {
return errors.Wrap(err, "failed to create tempfile")
}
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write([]byte(c.spec)); err != nil {
return errors.Wrap(err, "failed to write tempfile")
}
if err := tmpfile.Close(); err != nil {
return errors.Wrap(err, "failed to close temp file")
}
args = append(args, "from-spec")
args = append(args, tmpfile.Name())
} else {
args = append(args, "from-args")
if len(c.platforms) != 0 {
args = append(args, fmt.Sprintf("--platforms=%s", strings.Join(c.platforms, ",")))
}
if c.target != "" {
args = append(args, fmt.Sprintf("--target=%s", c.target))
}
if c.template != "" {
args = append(args, fmt.Sprintf("--template=%s", c.template))
}
}
if c.ignoreMissing {
args = append(args, "--ignore-missing")
}
cmd := exec.Command(
"manifest-tool",
args...,
)
buf := bytes.NewBufferString("")
cmd.Stdout = io.MultiWriter(os.Stdout, buf)
cmd.Stderr = io.MultiWriter(os.Stderr, buf)
if c.path != "" {
cmd.Dir = c.path
}
return cmd.Run()
}
-51
View File
@@ -1,51 +0,0 @@
package command
type Option func(*Command)
func WithUsername(val string) Option {
return func(t *Command) {
t.username = val
}
}
func WithPassword(val string) Option {
return func(t *Command) {
t.password = val
}
}
func WithSpec(val string) Option {
return func(t *Command) {
t.spec = val
}
}
func WithPlatforms(val []string) Option {
return func(t *Command) {
t.platforms = val
}
}
func WithTarget(val string) Option {
return func(t *Command) {
t.target = val
}
}
func WithTemplate(val string) Option {
return func(t *Command) {
t.template = val
}
}
func WithPath(val string) Option {
return func(t *Command) {
t.path = val
}
}
func IgnoreMissing() Option {
return func(t *Command) {
t.ignoreMissing = true
}
}
+50 -14
View File
@@ -1,12 +1,15 @@
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"github.com/drone/drone-template-lib/template"
"github.com/drone-plugins/drone-manifest/command"
"github.com/pkg/errors"
)
type (
@@ -58,28 +61,53 @@ type (
)
func (p *Plugin) Exec() error {
opts := make([]command.Option, 0)
args := []string{}
if p.Config.Username == "" {
return errors.New("you must provide a username")
} else {
opts = append(opts, command.WithUsername(p.Config.Username))
args = append(args, fmt.Sprintf("--username=%s", p.Config.Username))
}
if p.Config.Password == "" {
return errors.New("you must provide a password")
} else {
opts = append(opts, command.WithPassword(p.Config.Password))
args = append(args, fmt.Sprintf("--password=%s", p.Config.Password))
}
args = append(args, "push")
if p.Config.Spec != "" {
spec, err := template.RenderTrim(p.Config.Spec, p)
raw, err := ioutil.ReadFile(p.Config.Spec)
if err != nil {
return err
return errors.Wrap(err, "failed to read template")
}
opts = append(opts, command.WithSpec(spec))
spec, err := template.RenderTrim(string(raw), p)
if err != nil {
return errors.Wrap(err, "failed to render template")
}
tmpfile, err := ioutil.TempFile(p.Build.Path, "manifest-")
if err != nil {
return errors.Wrap(err, "failed to create tempfile")
}
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write([]byte(spec)); err != nil {
return errors.Wrap(err, "failed to write tempfile")
}
if err := tmpfile.Close(); err != nil {
return errors.Wrap(err, "failed to close tempfile")
}
args = append(args, "from-spec")
args = append(args, tmpfile.Name())
log.Printf(
"pushing by spec",
@@ -88,19 +116,19 @@ func (p *Plugin) Exec() error {
if len(p.Config.Platforms) == 0 {
return errors.New("you must provide platforms")
} else {
opts = append(opts, command.WithPlatforms(p.Config.Platforms))
args = append(args, fmt.Sprintf("--platforms=%s", strings.Join(p.Config.Platforms, ",")))
}
if p.Config.Target == "" {
return errors.New("you must provide a target")
} else {
opts = append(opts, command.WithTarget(p.Config.Target))
args = append(args, fmt.Sprintf("--target=%s", p.Config.Target))
}
if p.Config.Template == "" {
return errors.New("you must provide a template")
} else {
opts = append(opts, command.WithTemplate(p.Config.Template))
args = append(args, fmt.Sprintf("--template=%s", p.Config.Template))
}
log.Printf(
@@ -112,12 +140,20 @@ func (p *Plugin) Exec() error {
}
if p.Config.IgnoreMissing {
opts = append(opts, command.IgnoreMissing())
args = append(args, "--ignore-missing")
}
cmd := exec.Command(
"manifest-tool",
args...,
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if p.Build.Path != "" {
opts = append(opts, command.WithPath(p.Build.Path))
cmd.Dir = p.Build.Path
}
return command.New(opts...).Exec()
return cmd.Run()
}