mirror of
https://github.com/Threnklyn/jira.git
synced 2026-05-18 20:23:28 +02:00
27f57b2bbe
There should be no reason to use gopkg.in versioned imports now that we're using go modules. I think, IANAE. gopkg.in kind of gets in the way of modules, as it only pulls over tagged releases from github.com -- this then means that you need to use go modules 'replace' syntax in the go.mod to use a non-versioned commit or branch. This is feasible, but kind of ugly. go modules defaults to pulling the latest version, so the default behavior is the same as when pulling go-jira.v1 from gopkg.in.
80 lines
1.9 KiB
Go
80 lines
1.9 KiB
Go
package jiracmd
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/coryb/figtree"
|
|
"github.com/coryb/oreo"
|
|
jira "github.com/go-jira/jira"
|
|
"github.com/go-jira/jira/jiracli"
|
|
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
|
)
|
|
|
|
type AttachGetOptions struct {
|
|
AttachmentID string `yaml:"attachment-id,omitempty" json:"attachment-id,omitempty"`
|
|
OutputFile string `yaml:"output-file,omitempty" json:"output-file,omitempty"`
|
|
}
|
|
|
|
func CmdAttachGetRegistry() *jiracli.CommandRegistryEntry {
|
|
opts := AttachGetOptions{}
|
|
|
|
return &jiracli.CommandRegistryEntry{
|
|
"Fetch attachment",
|
|
func(fig *figtree.FigTree, cmd *kingpin.CmdClause) error {
|
|
jiracli.LoadConfigs(cmd, fig, &opts)
|
|
return CmdAttachGetUsage(cmd, &opts)
|
|
},
|
|
func(o *oreo.Client, globals *jiracli.GlobalOptions) error {
|
|
return CmdAttachGet(o, globals, &opts)
|
|
},
|
|
}
|
|
}
|
|
|
|
func CmdAttachGetUsage(cmd *kingpin.CmdClause, opts *AttachGetOptions) error {
|
|
cmd.Flag("output", "Write attachment to specified file name, '-' for stdout").Short('o').StringVar(&opts.OutputFile)
|
|
cmd.Arg("ATTACHMENT-ID", "Attachment id to fetch").StringVar(&opts.AttachmentID)
|
|
return nil
|
|
}
|
|
|
|
func CmdAttachGet(o *oreo.Client, globals *jiracli.GlobalOptions, opts *AttachGetOptions) error {
|
|
attachment, err := jira.GetAttachment(o, globals.Endpoint.Value, opts.AttachmentID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp, err := o.Get(attachment.Content)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var output *os.File
|
|
if opts.OutputFile == "-" {
|
|
output = os.Stdout
|
|
} else if opts.OutputFile != "" {
|
|
output, err = os.Create(opts.OutputFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer output.Close()
|
|
} else {
|
|
output, err = os.Create(attachment.Filename)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer output.Close()
|
|
}
|
|
|
|
_, err = io.Copy(output, resp.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
output.Close()
|
|
if opts.OutputFile != "-" && !globals.Quiet.Value {
|
|
fmt.Printf("OK Wrote %s\n", output.Name())
|
|
}
|
|
return nil
|
|
}
|