mirror of
https://gitea.com/jolheiser/sip
synced 2024-11-22 19:51:58 +01:00
Add release attachments (#26)
imp Signed-off-by: jolheiser <john.olheiser@gmail.com> Add release attachments Signed-off-by: jolheiser <john.olheiser@gmail.com> Co-authored-by: jolheiser <john.olheiser@gmail.com> Reviewed-on: https://gitea.com/jolheiser/sip/pulls/26
This commit is contained in:
parent
3e49adf604
commit
a51305f816
@ -3,10 +3,11 @@ package cmd
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/skratchdot/open-golang/open"
|
||||
"github.com/urfave/cli/v2"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/skratchdot/open-golang/open"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var Open = cli.Command{
|
||||
|
@ -21,6 +21,7 @@ var Release = cli.Command{
|
||||
Action: doRelease,
|
||||
Subcommands: []*cli.Command{
|
||||
&ReleaseCreate,
|
||||
&ReleaseAttach,
|
||||
},
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
|
117
cmd/release_attach.go
Normal file
117
cmd/release_attach.go
Normal file
@ -0,0 +1,117 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gitea.com/jolheiser/sip/modules/sdk"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"github.com/AlecAivazis/survey/v2"
|
||||
"github.com/urfave/cli/v2"
|
||||
"go.jolheiser.com/beaver"
|
||||
"go.jolheiser.com/beaver/color"
|
||||
)
|
||||
|
||||
var ReleaseAttach = cli.Command{
|
||||
Name: "attach",
|
||||
Usage: "Attach files to a release",
|
||||
Action: doReleaseAttach,
|
||||
}
|
||||
|
||||
func doReleaseAttach(ctx *cli.Context) error {
|
||||
client, err := getClient(ctx, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
releases, err := sdk.GetReleases(client, ctx.String("owner"), ctx.String("repo"), gitea.ListReleasesOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
releaseNames := make([]string, len(releases))
|
||||
releaseMap := make(map[string]*gitea.Release)
|
||||
for idx, rel := range releases {
|
||||
releaseNames[idx] = rel.TagName
|
||||
releaseMap[rel.TagName] = rel
|
||||
}
|
||||
|
||||
questions := []*survey.Question{
|
||||
{
|
||||
Name: "release",
|
||||
Prompt: &survey.Select{Message: "Release", Options: releaseNames},
|
||||
Validate: survey.Required,
|
||||
},
|
||||
{
|
||||
Name: "attachments",
|
||||
Prompt: &survey.Multiline{Message: "Attachments", Help: "Enter file globs, each new line being a separate glob"},
|
||||
},
|
||||
}
|
||||
answers := struct {
|
||||
Release string
|
||||
Attachments string
|
||||
}{}
|
||||
|
||||
if err := survey.Ask(questions, &answers); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
release := releaseMap[answers.Release]
|
||||
files, err := fileGlobs(answers.Attachments)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := attachFiles(ctx, client, release.ID, files); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
info := color.Info
|
||||
cyan := color.New(color.FgCyan)
|
||||
fmt.Println(info.Format("Release"), cyan.Format(release.TagName), info.Format("updated!"))
|
||||
fmt.Println(cyan.Format(fmt.Sprintf("%s/%s/%s/releases/tag/%s",
|
||||
ctx.String("url"), ctx.String("owner"), ctx.String("repo"), release.TagName)))
|
||||
return nil
|
||||
}
|
||||
|
||||
func fileGlobs(globList string) ([]string, error) {
|
||||
files := make([]string, 0)
|
||||
for _, glob := range strings.Split(globList, "\n") {
|
||||
glob = strings.TrimSpace(glob)
|
||||
matches, err := filepath.Glob(glob)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
files = append(files, matches...)
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func attachFiles(ctx *cli.Context, client *gitea.Client, releaseID int64, files []string) error {
|
||||
beaver.Infof("Attachments:\n\t%s", strings.Join(files, "\n\t"))
|
||||
|
||||
var confirm bool
|
||||
if err := survey.AskOne(&survey.Confirm{Message: "The above files will be attached, is the list correct?"}, &confirm); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if confirm {
|
||||
for _, file := range files {
|
||||
fi, err := os.Open(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, _, err := client.CreateReleaseAttachment(ctx.String("owner"), ctx.String("repo"), releaseID, fi, fi.Name()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := fi.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
@ -54,14 +54,19 @@ func doReleaseCreate(ctx *cli.Context) error {
|
||||
Prompt: &survey.Confirm{Message: "Pre-Release", Default: false},
|
||||
Validate: survey.Required,
|
||||
},
|
||||
{
|
||||
Name: "attachments",
|
||||
Prompt: &survey.Multiline{Message: "Attachments", Help: "Enter file globs, each new line being a separate glob"},
|
||||
},
|
||||
}
|
||||
answers := struct {
|
||||
Tag string
|
||||
Target string
|
||||
Title string
|
||||
Note string
|
||||
Draft bool
|
||||
Pre bool
|
||||
Tag string
|
||||
Target string
|
||||
Title string
|
||||
Note string
|
||||
Draft bool
|
||||
Pre bool
|
||||
Attachments string
|
||||
}{}
|
||||
|
||||
if err := survey.Ask(questions, &answers); err != nil {
|
||||
@ -80,10 +85,20 @@ func doReleaseCreate(ctx *cli.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if answers.Attachments != "" {
|
||||
files, err := fileGlobs(answers.Attachments)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := attachFiles(ctx, client, release.ID, files); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
info := color.Info
|
||||
cyan := color.New(color.FgCyan)
|
||||
fmt.Println(info.Format("Release"), cyan.Format(release.TagName), info.Format("created!"))
|
||||
// TODO Change to specific release page once supported
|
||||
fmt.Println(cyan.Format(fmt.Sprintf("%s/%s/%s/releases/", ctx.String("url"), ctx.String("owner"), ctx.String("repo"))))
|
||||
fmt.Println(cyan.Format(fmt.Sprintf("%s/%s/%s/releases/tag/%s",
|
||||
ctx.String("url"), ctx.String("owner"), ctx.String("repo"), release.TagName)))
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user