diff --git a/command/command.go b/command/command.go deleted file mode 100644 index 37b8770..0000000 --- a/command/command.go +++ /dev/null @@ -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() -} diff --git a/command/option.go b/command/option.go deleted file mode 100644 index 379c9f1..0000000 --- a/command/option.go +++ /dev/null @@ -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 - } -} diff --git a/plugin.go b/plugin.go index 4abee2a..19b77b9 100644 --- a/plugin.go +++ b/plugin.go @@ -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() }