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") cmd := git.NewCommand("remote", "get-url", "origin")
u, err := cmd.RunInDir(filepath.Dir(os.Args[0])) u, err := cmd.RunInDir(filepath.Dir(os.Args[0]))
if err != nil || len(u) == 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)) 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 var login *Login
if ctx.IsSet("login") { if loginFlag := getGlobalFlag(ctx, "login"); loginFlag == "" {
login = getLoginByName(ctx.String("login"))
if login == nil {
log.Fatal("indicated login name", ctx.String("login"), "is not exist")
}
} else {
login, err = getActiveLogin() login, err = getActiveLogin()
if err != nil { 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 repoPath := getGlobalFlag(ctx, "repo")
if !ctx.IsSet("repo") { if repoPath == "" {
login, repoPath, err = curGitRepoPath() login, repoPath, err = curGitRepoPath()
if err != nil { if err != nil {
log.Fatal(err.Error()) log.Fatal(err.Error())
} }
} else {
repoPath = ctx.String("repo")
} }
owner, repo := splitRepo(repoPath) owner, repo := splitRepo(repoPath)
return login, owner, repo 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 { func runIssuesCreate(ctx *cli.Context) error {
login, owner, repo := initCommand(ctx) login, owner, repo := initCommand(ctx)

View File

@ -7,6 +7,8 @@ package cmd
import ( import (
"fmt" "fmt"
"log" "log"
"os"
"path/filepath"
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
@ -87,13 +89,17 @@ var CmdReleaseCreate = cli.Command{
Name: "prerelease, p", Name: "prerelease, p",
Usage: "the release is a prerelease", 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 { func runReleaseCreate(ctx *cli.Context) error {
login, owner, repo := initCommand(ctx) 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"), TagName: ctx.String("tag"),
Target: ctx.String("target"), Target: ctx.String("target"),
Title: ctx.String("title"), Title: ctx.String("title"),
@ -103,8 +109,29 @@ func runReleaseCreate(ctx *cli.Context) error {
}) })
if err != nil { if err != nil {
if err.Error() == "409 Conflict" {
log.Fatal("error: There already is a release for this tag")
}
log.Fatal(err) 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 return nil
} }

View File

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