Skip to content
This repository was archived by the owner on May 24, 2019. It is now read-only.

Commit 4de0b04

Browse files
noerwlafriks
authored andcommitted
add --asset flag to tea releases create (#6)
Co-Authored-By: noerw <[email protected]>
1 parent 6220a10 commit 4de0b04

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

cmd/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func curGitRepoPath() (*Login, string, error) {
186186
cmd := git.NewCommand("remote", "get-url", "origin")
187187
u, err := cmd.RunInDir(filepath.Dir(os.Args[0]))
188188
if err != nil || len(u) == 0 {
189-
return nil, "", errors.New("You have to indicated a repo or execute the command in a repo")
189+
return nil, "", errors.New("You have to indicate a repo or execute the command in a repo")
190190
}
191191

192192
p, err := local_git.ParseURL(strings.TrimSpace(u))

cmd/issues.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,32 +130,38 @@ func initCommand(ctx *cli.Context) (*Login, string, string) {
130130
}
131131

132132
var login *Login
133-
if ctx.IsSet("login") {
134-
login = getLoginByName(ctx.String("login"))
135-
if login == nil {
136-
log.Fatal("indicated login name", ctx.String("login"), "is not exist")
137-
}
138-
} else {
133+
if loginFlag := getGlobalFlag(ctx, "login"); loginFlag == "" {
139134
login, err = getActiveLogin()
140135
if err != nil {
141-
log.Fatal("get active login failed")
136+
log.Fatal(err)
137+
}
138+
} else {
139+
login = getLoginByName(loginFlag)
140+
if login == nil {
141+
log.Fatal("indicated login name", loginFlag, "does not exist")
142142
}
143143
}
144144

145-
var repoPath string
146-
if !ctx.IsSet("repo") {
145+
repoPath := getGlobalFlag(ctx, "repo")
146+
if repoPath == "" {
147147
login, repoPath, err = curGitRepoPath()
148148
if err != nil {
149149
log.Fatal(err.Error())
150150
}
151-
} else {
152-
repoPath = ctx.String("repo")
153151
}
154152

155153
owner, repo := splitRepo(repoPath)
156154
return login, owner, repo
157155
}
158156

157+
func getGlobalFlag(ctx *cli.Context, flag string) string {
158+
var val = ctx.String(flag)
159+
if val == "" {
160+
return ctx.GlobalString(flag)
161+
}
162+
return val
163+
}
164+
159165
func runIssuesCreate(ctx *cli.Context) error {
160166
login, owner, repo := initCommand(ctx)
161167

cmd/releases.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package cmd
77
import (
88
"fmt"
99
"log"
10+
"os"
11+
"path/filepath"
1012

1113
"code.gitea.io/sdk/gitea"
1214

@@ -87,13 +89,17 @@ var CmdReleaseCreate = cli.Command{
8789
Name: "prerelease, p",
8890
Usage: "the release is a prerelease",
8991
},
92+
cli.StringSliceFlag{
93+
Name: "asset, a",
94+
Usage: "a list of files to attach to the release",
95+
},
9096
},
9197
}
9298

9399
func runReleaseCreate(ctx *cli.Context) error {
94100
login, owner, repo := initCommand(ctx)
95101

96-
_, err := login.Client().CreateRelease(owner, repo, gitea.CreateReleaseOption{
102+
release, err := login.Client().CreateRelease(owner, repo, gitea.CreateReleaseOption{
97103
TagName: ctx.String("tag"),
98104
Target: ctx.String("target"),
99105
Title: ctx.String("title"),
@@ -103,8 +109,29 @@ func runReleaseCreate(ctx *cli.Context) error {
103109
})
104110

105111
if err != nil {
112+
if err.Error() == "409 Conflict" {
113+
log.Fatal("error: There already is a release for this tag")
114+
}
115+
106116
log.Fatal(err)
107117
}
108118

119+
for _, asset := range ctx.StringSlice("asset") {
120+
var file *os.File
121+
122+
if file, err = os.Open(asset); err != nil {
123+
log.Fatal(err)
124+
}
125+
126+
filePath := filepath.Base(asset)
127+
128+
if _, err = login.Client().CreateReleaseAttachment(owner, repo, release.ID, file, filePath); err != nil {
129+
file.Close()
130+
log.Fatal(err)
131+
}
132+
133+
file.Close()
134+
}
135+
109136
return nil
110137
}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func init() {
3030
func main() {
3131
app := cli.NewApp()
3232
app.Name = "Tea"
33-
app.Usage = "Command line tool to interactive with Gitea"
33+
app.Usage = "Command line tool to interact with Gitea"
3434
app.Description = ``
3535
app.Version = Version + formatBuiltWith(Tags)
3636
app.Commands = []cli.Command{

0 commit comments

Comments
 (0)