2020-09-30 05:11:33 +00:00
|
|
|
// Copyright 2020 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 print
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2020-10-10 01:17:31 +00:00
|
|
|
"time"
|
2020-09-30 05:11:33 +00:00
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
)
|
|
|
|
|
2020-10-06 08:05:22 +00:00
|
|
|
// ReposList prints a listing of the repos
|
2020-12-08 10:28:54 +00:00
|
|
|
func ReposList(repos []*gitea.Repository, output string, fields []string) {
|
2020-12-21 15:41:07 +00:00
|
|
|
var printables = make([]printable, len(repos))
|
|
|
|
for i, r := range repos {
|
|
|
|
printables[i] = &printableRepo{r}
|
2020-10-10 01:17:31 +00:00
|
|
|
}
|
PR listing: add --fields & expose additional fields (#415)
This PR adds the `--fields` flag to `tea pr ls` (#342), and exposes more fields specific to the `PullRequest` type:
```
--fields value, -f value Comma-separated list of fields to print.
Available values:
index,state,author,author-id,url,title,body,mergeable,base,base-commit,head,diff,patch,created,updated,deadline,assignees,milestone,labels,comments
(default: "index,title,state,author,milestone,updated,labels")
```
Co-authored-by: justusbunsi <61625851+justusbunsi@users.noreply.github.com>
Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/415
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Reviewed-by: techknowlogick <techknowlogick@gitea.io>
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: justusbunsi <justusbunsi@noreply.gitea.io>
Co-committed-by: justusbunsi <justusbunsi@noreply.gitea.io>
2021-09-28 20:36:33 +00:00
|
|
|
t := tableFromItems(fields, printables, isMachineReadable(output))
|
2020-12-09 22:04:36 +00:00
|
|
|
t.print(output)
|
2020-10-06 08:05:22 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 05:11:33 +00:00
|
|
|
// RepoDetails print an repo formatted to stdout
|
|
|
|
func RepoDetails(repo *gitea.Repository, topics []string) {
|
2020-10-10 01:17:31 +00:00
|
|
|
title := "# " + repo.FullName
|
2020-09-30 05:11:33 +00:00
|
|
|
if repo.Mirror {
|
2020-10-10 01:17:31 +00:00
|
|
|
title += " (mirror)"
|
2020-09-30 05:11:33 +00:00
|
|
|
}
|
|
|
|
if repo.Fork {
|
2020-10-10 01:17:31 +00:00
|
|
|
title += " (fork)"
|
2020-09-30 05:11:33 +00:00
|
|
|
}
|
|
|
|
if repo.Archived {
|
2020-10-10 01:17:31 +00:00
|
|
|
title += " (archived)"
|
2020-09-30 05:11:33 +00:00
|
|
|
}
|
|
|
|
if repo.Empty {
|
2020-10-10 01:17:31 +00:00
|
|
|
title += " (empty)"
|
2020-09-30 05:11:33 +00:00
|
|
|
}
|
2020-10-10 01:17:31 +00:00
|
|
|
title += "\n"
|
|
|
|
|
|
|
|
var desc string
|
|
|
|
if len(repo.Description) != 0 {
|
|
|
|
desc = fmt.Sprintf("*%s*\n\n", repo.Description)
|
2020-09-30 05:11:33 +00:00
|
|
|
}
|
2020-10-10 01:17:31 +00:00
|
|
|
|
|
|
|
stats := fmt.Sprintf(
|
|
|
|
"Issues: %d, Stars: %d, Forks: %d, Size: %s\n",
|
2020-09-30 05:11:33 +00:00
|
|
|
repo.OpenIssues,
|
|
|
|
repo.Stars,
|
|
|
|
repo.Forks,
|
|
|
|
formatSize(int64(repo.Size)),
|
|
|
|
)
|
|
|
|
|
2020-10-10 01:17:31 +00:00
|
|
|
// NOTE: for mirrors, this is the time the mirror was last fetched..
|
|
|
|
updated := fmt.Sprintf(
|
|
|
|
"Updated: %s (%s ago)\n",
|
|
|
|
repo.Updated.Format("2006-01-02 15:04"),
|
|
|
|
time.Now().Sub(repo.Updated).Truncate(time.Minute),
|
|
|
|
)
|
|
|
|
|
|
|
|
urls := fmt.Sprintf(
|
|
|
|
"- Browse:\t%s\n- Clone:\t%s\n",
|
|
|
|
repo.HTMLURL,
|
|
|
|
repo.SSHURL,
|
|
|
|
)
|
|
|
|
if len(repo.Website) != 0 {
|
|
|
|
urls += fmt.Sprintf("- Web:\t%s\n", repo.Website)
|
|
|
|
}
|
|
|
|
|
|
|
|
perm := fmt.Sprintf(
|
|
|
|
"- Permission:\t%s\n",
|
2020-12-21 15:41:07 +00:00
|
|
|
formatPermission(repo.Permissions),
|
2020-10-10 01:17:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var tops string
|
|
|
|
if len(topics) != 0 {
|
|
|
|
tops = fmt.Sprintf("- Topics:\t%s\n", strings.Join(topics, ", "))
|
|
|
|
}
|
|
|
|
|
2020-12-08 10:28:54 +00:00
|
|
|
outputMarkdown(fmt.Sprintf(
|
2020-10-10 01:17:31 +00:00
|
|
|
"%s%s\n%s\n%s%s%s%s",
|
|
|
|
title,
|
|
|
|
desc,
|
|
|
|
stats,
|
|
|
|
updated,
|
|
|
|
urls,
|
|
|
|
perm,
|
|
|
|
tops,
|
2021-03-12 12:28:46 +00:00
|
|
|
), repo.HTMLURL)
|
2020-09-30 05:11:33 +00:00
|
|
|
}
|
2020-12-21 15:41:07 +00:00
|
|
|
|
|
|
|
// RepoFields are the available fields to print with ReposList()
|
|
|
|
var RepoFields = []string{
|
|
|
|
"description",
|
|
|
|
"forks",
|
|
|
|
"id",
|
|
|
|
"name",
|
|
|
|
"owner",
|
|
|
|
"stars",
|
|
|
|
"ssh",
|
|
|
|
"updated",
|
|
|
|
"url",
|
|
|
|
"permission",
|
|
|
|
"type",
|
|
|
|
}
|
|
|
|
|
|
|
|
type printableRepo struct{ *gitea.Repository }
|
|
|
|
|
PR listing: add --fields & expose additional fields (#415)
This PR adds the `--fields` flag to `tea pr ls` (#342), and exposes more fields specific to the `PullRequest` type:
```
--fields value, -f value Comma-separated list of fields to print.
Available values:
index,state,author,author-id,url,title,body,mergeable,base,base-commit,head,diff,patch,created,updated,deadline,assignees,milestone,labels,comments
(default: "index,title,state,author,milestone,updated,labels")
```
Co-authored-by: justusbunsi <61625851+justusbunsi@users.noreply.github.com>
Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/415
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Reviewed-by: techknowlogick <techknowlogick@gitea.io>
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: justusbunsi <justusbunsi@noreply.gitea.io>
Co-committed-by: justusbunsi <justusbunsi@noreply.gitea.io>
2021-09-28 20:36:33 +00:00
|
|
|
func (x printableRepo) FormatField(field string, machineReadable bool) string {
|
2020-12-21 15:41:07 +00:00
|
|
|
switch field {
|
|
|
|
case "description":
|
|
|
|
return x.Description
|
|
|
|
case "forks":
|
|
|
|
return fmt.Sprintf("%d", x.Forks)
|
|
|
|
case "id":
|
|
|
|
return x.FullName
|
|
|
|
case "name":
|
|
|
|
return x.Name
|
|
|
|
case "owner":
|
|
|
|
return x.Owner.UserName
|
|
|
|
case "stars":
|
|
|
|
return fmt.Sprintf("%d", x.Stars)
|
|
|
|
case "ssh":
|
|
|
|
return x.SSHURL
|
|
|
|
case "updated":
|
|
|
|
return FormatTime(x.Updated)
|
|
|
|
case "url":
|
|
|
|
return x.HTMLURL
|
|
|
|
case "permission":
|
|
|
|
return formatPermission(x.Permissions)
|
|
|
|
case "type":
|
|
|
|
if x.Fork {
|
|
|
|
return "fork"
|
|
|
|
}
|
|
|
|
if x.Mirror {
|
|
|
|
return "mirror"
|
|
|
|
}
|
|
|
|
return "source"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|