From 4de0b04d775391648bc0a6b79a89f69015aad9a5 Mon Sep 17 00:00:00 2001 From: Norwin Date: Mon, 11 Mar 2019 17:32:09 +0100 Subject: [PATCH] add --asset flag to `tea releases create` (#6) Co-Authored-By: noerw --- cmd/config.go | 2 +- cmd/issues.go | 28 +++++++++++++++++----------- cmd/releases.go | 29 ++++++++++++++++++++++++++++- main.go | 2 +- 4 files changed, 47 insertions(+), 14 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index 78cd625..16b0bdb 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -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)) diff --git a/cmd/issues.go b/cmd/issues.go index ff6fdd2..bd4d2dc 100644 --- a/cmd/issues.go +++ b/cmd/issues.go @@ -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) diff --git a/cmd/releases.go b/cmd/releases.go index 0a4c4f2..7d435ee 100644 --- a/cmd/releases.go +++ b/cmd/releases.go @@ -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 } diff --git a/main.go b/main.go index e8afc23..e5b0a42 100644 --- a/main.go +++ b/main.go @@ -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{