add --asset flag to `tea releases create` (#6)

Co-Authored-By: noerw <noerw@users.noreply.github.com>
This commit is contained in:
Norwin 2019-03-11 17:32:09 +01:00 committed by Lauris BH
parent 6220a10628
commit 4de0b04d77
4 changed files with 47 additions and 14 deletions

View File

@ -186,7 +186,7 @@ func curGitRepoPath() (*Login, string, error) {
cmd := git.NewCommand("remote", "get-url", "origin")
u, err := cmd.RunInDir(filepath.Dir(os.Args[0]))
if err != nil || len(u) == 0 {
return nil, "", errors.New("You have to indicated a repo or execute the command in a repo")
return nil, "", errors.New("You have to indicate a repo or execute the command in a repo")
}
p, err := local_git.ParseURL(strings.TrimSpace(u))

View File

@ -130,32 +130,38 @@ func initCommand(ctx *cli.Context) (*Login, string, string) {
}
var login *Login
if ctx.IsSet("login") {
login = getLoginByName(ctx.String("login"))
if login == nil {
log.Fatal("indicated login name", ctx.String("login"), "is not exist")
}
} else {
if loginFlag := getGlobalFlag(ctx, "login"); loginFlag == "" {
login, err = getActiveLogin()
if err != nil {
log.Fatal("get active login failed")
log.Fatal(err)
}
} else {
login = getLoginByName(loginFlag)
if login == nil {
log.Fatal("indicated login name", loginFlag, "does not exist")
}
}
var repoPath string
if !ctx.IsSet("repo") {
repoPath := getGlobalFlag(ctx, "repo")
if repoPath == "" {
login, repoPath, err = curGitRepoPath()
if err != nil {
log.Fatal(err.Error())
}
} else {
repoPath = ctx.String("repo")
}
owner, repo := splitRepo(repoPath)
return login, owner, repo
}
func getGlobalFlag(ctx *cli.Context, flag string) string {
var val = ctx.String(flag)
if val == "" {
return ctx.GlobalString(flag)
}
return val
}
func runIssuesCreate(ctx *cli.Context) error {
login, owner, repo := initCommand(ctx)

View File

@ -7,6 +7,8 @@ package cmd
import (
"fmt"
"log"
"os"
"path/filepath"
"code.gitea.io/sdk/gitea"
@ -87,13 +89,17 @@ var CmdReleaseCreate = cli.Command{
Name: "prerelease, p",
Usage: "the release is a prerelease",
},
cli.StringSliceFlag{
Name: "asset, a",
Usage: "a list of files to attach to the release",
},
},
}
func runReleaseCreate(ctx *cli.Context) error {
login, owner, repo := initCommand(ctx)
_, err := login.Client().CreateRelease(owner, repo, gitea.CreateReleaseOption{
release, err := login.Client().CreateRelease(owner, repo, gitea.CreateReleaseOption{
TagName: ctx.String("tag"),
Target: ctx.String("target"),
Title: ctx.String("title"),
@ -103,8 +109,29 @@ func runReleaseCreate(ctx *cli.Context) error {
})
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
}

View File

@ -30,7 +30,7 @@ func init() {
func main() {
app := cli.NewApp()
app.Name = "Tea"
app.Usage = "Command line tool to interactive with Gitea"
app.Usage = "Command line tool to interact with Gitea"
app.Description = ``
app.Version = Version + formatBuiltWith(Tags)
app.Commands = []cli.Command{