2018-09-03 02:43:00 -04: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 12:32:09 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-09-03 02:43:00 -04:00
|
|
|
|
2018-10-21 14:52:57 -04:00
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
|
2020-01-04 12:44:25 -05:00
|
|
|
"github.com/urfave/cli/v2"
|
2018-09-03 02:43:00 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// CmdReleases represents to login a gitea server.
|
|
|
|
var CmdReleases = cli.Command{
|
|
|
|
Name: "releases",
|
2019-11-03 15:34:41 -05:00
|
|
|
Usage: "Create releases",
|
|
|
|
Description: `Create releases`,
|
2018-09-03 02:43:00 -04:00
|
|
|
Action: runReleases,
|
2020-01-04 12:44:25 -05:00
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
&CmdReleaseCreate,
|
2018-10-21 14:52:57 -04:00
|
|
|
},
|
2019-10-23 11:58:18 -04:00
|
|
|
Flags: AllDefaultFlags,
|
2018-09-03 02:43:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func runReleases(ctx *cli.Context) error {
|
2019-10-23 11:58:18 -04:00
|
|
|
login, owner, repo := initCommand()
|
2018-09-03 02:43:00 -04:00
|
|
|
|
|
|
|
releases, err := login.Client().ListReleases(owner, repo)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-09-15 04:38:18 -04:00
|
|
|
headers := []string{
|
|
|
|
"Tag-Name",
|
|
|
|
"Title",
|
|
|
|
"Published At",
|
|
|
|
"Tar URL",
|
|
|
|
}
|
|
|
|
|
|
|
|
var values [][]string
|
|
|
|
|
2018-09-03 02:43:00 -04:00
|
|
|
if len(releases) == 0 {
|
2019-10-26 10:25:54 -04:00
|
|
|
Output(outputValue, headers, values)
|
2018-09-03 02:43:00 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, release := range releases {
|
2019-09-15 04:38:18 -04:00
|
|
|
values = append(
|
|
|
|
values,
|
|
|
|
[]string{
|
|
|
|
release.TagName,
|
|
|
|
release.Title,
|
|
|
|
release.PublishedAt.Format("2006-01-02 15:04:05"),
|
|
|
|
release.TarURL,
|
|
|
|
},
|
|
|
|
)
|
2018-09-03 02:43:00 -04:00
|
|
|
}
|
2019-10-26 10:25:54 -04:00
|
|
|
Output(outputValue, headers, values)
|
2018-09-03 02:43:00 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-10-21 14:52:57 -04:00
|
|
|
|
2019-04-25 13:06:53 -04:00
|
|
|
// CmdReleaseCreate represents a sub command of Release to create release.
|
2018-10-21 14:52:57 -04:00
|
|
|
var CmdReleaseCreate = cli.Command{
|
|
|
|
Name: "create",
|
2019-11-03 15:34:41 -05:00
|
|
|
Usage: "Create a release",
|
|
|
|
Description: `Create a release`,
|
2018-10-21 14:52:57 -04:00
|
|
|
Action: runReleaseCreate,
|
2019-10-23 11:58:18 -04:00
|
|
|
Flags: append([]cli.Flag{
|
2020-01-04 12:44:25 -05:00
|
|
|
&cli.StringFlag{
|
2018-10-21 14:52:57 -04:00
|
|
|
Name: "tag",
|
2019-11-03 15:34:41 -05:00
|
|
|
Usage: "Tag name",
|
2018-10-21 14:52:57 -04:00
|
|
|
},
|
2020-01-04 12:44:25 -05:00
|
|
|
&cli.StringFlag{
|
2018-10-21 14:52:57 -04:00
|
|
|
Name: "target",
|
2019-11-03 15:34:41 -05:00
|
|
|
Usage: "Target refs, branch name or commit id",
|
2018-10-21 14:52:57 -04:00
|
|
|
},
|
2020-01-04 12:44:25 -05:00
|
|
|
&cli.StringFlag{
|
2020-03-05 22:43:28 -05:00
|
|
|
Name: "title",
|
|
|
|
Aliases: []string{"t"},
|
|
|
|
Usage: "Release title",
|
2018-10-21 14:52:57 -04:00
|
|
|
},
|
2020-01-04 12:44:25 -05:00
|
|
|
&cli.StringFlag{
|
2020-03-05 22:43:28 -05:00
|
|
|
Name: "note",
|
|
|
|
Aliases: []string{"n"},
|
|
|
|
Usage: "Release notes",
|
2018-10-21 14:52:57 -04:00
|
|
|
},
|
2020-01-04 12:44:25 -05:00
|
|
|
&cli.BoolFlag{
|
2020-03-05 22:43:28 -05:00
|
|
|
Name: "draft",
|
|
|
|
Aliases: []string{"d"},
|
|
|
|
Usage: "Is a draft",
|
2018-10-21 14:52:57 -04:00
|
|
|
},
|
2020-01-04 12:44:25 -05:00
|
|
|
&cli.BoolFlag{
|
2020-03-05 22:43:28 -05:00
|
|
|
Name: "prerelease",
|
|
|
|
Aliases: []string{"p"},
|
|
|
|
Usage: "Is a pre-release",
|
2018-10-21 14:52:57 -04:00
|
|
|
},
|
2020-01-04 12:44:25 -05:00
|
|
|
&cli.StringSliceFlag{
|
2020-03-05 22:43:28 -05:00
|
|
|
Name: "asset",
|
|
|
|
Aliases: []string{"a"},
|
|
|
|
Usage: "List of files to attach",
|
2019-03-11 12:32:09 -04:00
|
|
|
},
|
2019-10-23 11:58:18 -04:00
|
|
|
}, LoginRepoFlags...),
|
2018-10-21 14:52:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func runReleaseCreate(ctx *cli.Context) error {
|
2019-10-23 11:58:18 -04:00
|
|
|
login, owner, repo := initCommand()
|
2018-10-21 14:52:57 -04:00
|
|
|
|
2019-03-11 12:32:09 -04:00
|
|
|
release, err := login.Client().CreateRelease(owner, repo, gitea.CreateReleaseOption{
|
2018-10-21 14:52:57 -04: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 12:32:09 -04:00
|
|
|
if err.Error() == "409 Conflict" {
|
|
|
|
log.Fatal("error: There already is a release for this tag")
|
|
|
|
}
|
|
|
|
|
2018-10-21 14:52:57 -04:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-03-11 12:32:09 -04: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 14:52:57 -04:00
|
|
|
return nil
|
|
|
|
}
|