2018-09-03 08:43:00 +02: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"
|
2019-03-11 17:32:09 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-09-03 08:43:00 +02:00
|
|
|
|
2018-10-21 20:52:57 +02:00
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
|
2018-09-03 08:43:00 +02:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CmdReleases represents to login a gitea server.
|
|
|
|
var CmdReleases = cli.Command{
|
|
|
|
Name: "releases",
|
2019-03-12 02:36:14 +01:00
|
|
|
Usage: "Operate with releases of the repository",
|
|
|
|
Description: `Operate with releases of the repository`,
|
2018-09-03 08:43:00 +02:00
|
|
|
Action: runReleases,
|
2018-10-21 20:52:57 +02:00
|
|
|
Subcommands: []cli.Command{
|
|
|
|
CmdReleaseCreate,
|
|
|
|
},
|
2019-05-02 07:44:57 +02:00
|
|
|
Flags: append([]cli.Flag{}, RepoDefaultFlags...),
|
2018-09-03 08:43:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func runReleases(ctx *cli.Context) error {
|
2019-05-02 07:44:57 +02:00
|
|
|
login, owner, repo := initCommand()
|
2018-09-03 08:43:00 +02:00
|
|
|
|
|
|
|
releases, err := login.Client().ListReleases(owner, repo)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-05-08 21:01:07 +02:00
|
|
|
headers := []string{
|
|
|
|
string("Tag-Name"),
|
|
|
|
string("Title"),
|
|
|
|
string("Published At"),
|
|
|
|
string("Tar URL"),
|
|
|
|
}
|
|
|
|
|
|
|
|
var values [][]string
|
|
|
|
|
2018-09-03 08:43:00 +02:00
|
|
|
if len(releases) == 0 {
|
2019-05-08 21:01:07 +02:00
|
|
|
Output(output, headers, values)
|
2018-09-03 08:43:00 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, release := range releases {
|
2019-05-08 21:01:07 +02:00
|
|
|
values = append(
|
|
|
|
values,
|
|
|
|
[]string{
|
|
|
|
release.TagName,
|
|
|
|
release.Title,
|
|
|
|
release.PublishedAt.Format("2006-01-02 15:04:05"),
|
|
|
|
release.TarURL,
|
|
|
|
},
|
|
|
|
)
|
2018-09-03 08:43:00 +02:00
|
|
|
}
|
2019-05-08 21:01:07 +02:00
|
|
|
Output(output, headers, values)
|
2018-09-03 08:43:00 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-10-21 20:52:57 +02:00
|
|
|
|
2019-04-25 19:06:53 +02:00
|
|
|
// CmdReleaseCreate represents a sub command of Release to create release.
|
2018-10-21 20:52:57 +02:00
|
|
|
var CmdReleaseCreate = cli.Command{
|
|
|
|
Name: "create",
|
|
|
|
Usage: "Create a release in repository",
|
|
|
|
Description: `Create a release in repository`,
|
|
|
|
Action: runReleaseCreate,
|
2019-05-02 07:44:57 +02:00
|
|
|
Flags: append([]cli.Flag{
|
2018-10-21 20:52:57 +02:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "tag",
|
|
|
|
Usage: "release tag name",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "target",
|
|
|
|
Usage: "release target refs, branch name or commit id",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "title, t",
|
|
|
|
Usage: "release title to create",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "note, n",
|
|
|
|
Usage: "release note to create",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "draft, d",
|
|
|
|
Usage: "the release is a draft",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "prerelease, p",
|
|
|
|
Usage: "the release is a prerelease",
|
|
|
|
},
|
2019-03-11 17:32:09 +01:00
|
|
|
cli.StringSliceFlag{
|
|
|
|
Name: "asset, a",
|
|
|
|
Usage: "a list of files to attach to the release",
|
|
|
|
},
|
2019-05-02 07:44:57 +02:00
|
|
|
}, RepoDefaultFlags...),
|
2018-10-21 20:52:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func runReleaseCreate(ctx *cli.Context) error {
|
2019-05-02 07:44:57 +02:00
|
|
|
login, owner, repo := initCommand()
|
2018-10-21 20:52:57 +02:00
|
|
|
|
2019-03-11 17:32:09 +01:00
|
|
|
release, err := login.Client().CreateRelease(owner, repo, gitea.CreateReleaseOption{
|
2018-10-21 20:52:57 +02:00
|
|
|
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 {
|
2019-03-11 17:32:09 +01:00
|
|
|
if err.Error() == "409 Conflict" {
|
|
|
|
log.Fatal("error: There already is a release for this tag")
|
|
|
|
}
|
|
|
|
|
2018-10-21 20:52:57 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-03-11 17:32:09 +01:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2018-10-21 20:52:57 +02:00
|
|
|
return nil
|
|
|
|
}
|