tea/cmd/releases.go

144 lines
2.8 KiB
Go
Raw Normal View History

2018-09-03 14:43:00 +08:00
// Copyright 2018 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package cmd
import (
"log"
"os"
"path/filepath"
2018-09-03 14:43:00 +08:00
"code.gitea.io/sdk/gitea"
"github.com/urfave/cli/v2"
2018-09-03 14:43:00 +08:00
)
// CmdReleases represents to login a gitea server.
var CmdReleases = cli.Command{
Name: "releases",
Usage: "Create releases",
Description: `Create releases`,
2018-09-03 14:43:00 +08:00
Action: runReleases,
Subcommands: []*cli.Command{
&CmdReleaseCreate,
},
Flags: AllDefaultFlags,
2018-09-03 14:43:00 +08:00
}
func runReleases(ctx *cli.Context) error {
login, owner, repo := initCommand()
2018-09-03 14:43:00 +08:00
releases, err := login.Client().ListReleases(owner, repo)
if err != nil {
log.Fatal(err)
}
2019-09-15 16:38:18 +08:00
headers := []string{
"Tag-Name",
"Title",
"Published At",
"Tar URL",
}
var values [][]string
2018-09-03 14:43:00 +08:00
if len(releases) == 0 {
2019-10-26 22:25:54 +08:00
Output(outputValue, headers, values)
2018-09-03 14:43:00 +08:00
return nil
}
for _, release := range releases {
2019-09-15 16:38:18 +08:00
values = append(
values,
[]string{
release.TagName,
release.Title,
release.PublishedAt.Format("2006-01-02 15:04:05"),
release.TarURL,
},
)
2018-09-03 14:43:00 +08:00
}
2019-10-26 22:25:54 +08:00
Output(outputValue, headers, values)
2018-09-03 14:43:00 +08:00
return nil
}
// CmdReleaseCreate represents a sub command of Release to create release.
var CmdReleaseCreate = cli.Command{
Name: "create",
Usage: "Create a release",
Description: `Create a release`,
Action: runReleaseCreate,
Flags: append([]cli.Flag{
&cli.StringFlag{
Name: "tag",
Usage: "Tag name",
},
&cli.StringFlag{
Name: "target",
Usage: "Target refs, branch name or commit id",
},
&cli.StringFlag{
Name: "title, t",
Usage: "Release title",
},
&cli.StringFlag{
Name: "note, n",
Usage: "Release notes",
},
&cli.BoolFlag{
Name: "draft, d",
Usage: "Is a draft",
},
&cli.BoolFlag{
Name: "prerelease, p",
Usage: "Is a pre-release",
},
&cli.StringSliceFlag{
Name: "asset, a",
Usage: "List of files to attach",
},
}, LoginRepoFlags...),
}
func runReleaseCreate(ctx *cli.Context) error {
login, owner, repo := initCommand()
release, err := login.Client().CreateRelease(owner, repo, gitea.CreateReleaseOption{
TagName: ctx.String("tag"),
Target: ctx.String("target"),
Title: ctx.String("title"),
Note: ctx.String("note"),
IsDraft: ctx.Bool("draft"),
IsPrerelease: ctx.Bool("prerelease"),
})
if err != nil {
if err.Error() == "409 Conflict" {
log.Fatal("error: There already is a release for this tag")
}
log.Fatal(err)
}
for _, asset := range ctx.StringSlice("asset") {
var file *os.File
if file, err = os.Open(asset); err != nil {
log.Fatal(err)
}
filePath := filepath.Base(asset)
if _, err = login.Client().CreateReleaseAttachment(owner, repo, release.ID, file, filePath); err != nil {
file.Close()
log.Fatal(err)
}
file.Close()
}
return nil
}