Refactor: Move list print functions into print package (#273)

MV list issues -> print.IssuesList

MV list labels -> print.LabelsList & task.LabelsExport

MV list logins -> print.LoginsList

MV list miles -> print.MilestonesList

MV list pulls -> print.PullsList

MV list releases -> print.ReleasesList

MV list issues&pulls of mile -> print.IssuesPullsList

MV list notification threads -> print.NotificationsList

Unexport print.outputList

Unexport print.outputMarkdown

remove comd/flags dependency in print module

Reviewed-on: https://gitea.com/gitea/tea/pulls/273
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: Andrew Thornton <art27@cantab.net>
Co-Authored-By: 6543 <6543@obermui.de>
Co-Committed-By: 6543 <6543@obermui.de>
This commit is contained in:
6543 2020-12-08 18:28:54 +08:00
parent 2b11f408fd
commit 5cb3e1ded5
25 changed files with 397 additions and 319 deletions

View File

@ -6,7 +6,6 @@ package issues
import (
"log"
"strconv"
"code.gitea.io/tea/cmd/flags"
"code.gitea.io/tea/modules/config"
@ -50,44 +49,6 @@ func RunIssuesList(ctx *cli.Context) error {
log.Fatal(err)
}
headers := []string{
"Index",
"Title",
"State",
"Author",
"Milestone",
"Updated",
}
var values [][]string
if len(issues) == 0 {
print.OutputList(flags.GlobalOutputValue, headers, values)
return nil
}
for _, issue := range issues {
author := issue.Poster.FullName
if len(author) == 0 {
author = issue.Poster.UserName
}
mile := ""
if issue.Milestone != nil {
mile = issue.Milestone.Title
}
values = append(
values,
[]string{
strconv.FormatInt(issue.Index, 10),
issue.Title,
string(issue.State),
author,
mile,
print.FormatTime(issue.Updated),
},
)
}
print.OutputList(flags.GlobalOutputValue, headers, values)
print.IssuesList(issues, flags.GlobalOutputValue)
return nil
}

View File

@ -5,18 +5,15 @@
package cmd
import (
"fmt"
"log"
"os"
"strconv"
"code.gitea.io/tea/cmd/flags"
"code.gitea.io/tea/cmd/labels"
"code.gitea.io/tea/modules/config"
"code.gitea.io/tea/modules/print"
"code.gitea.io/tea/modules/task"
"code.gitea.io/sdk/gitea"
"github.com/muesli/termenv"
"github.com/urfave/cli/v2"
)
@ -46,54 +43,15 @@ var CmdLabels = cli.Command{
func runLabels(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
headers := []string{
"Index",
"Color",
"Name",
"Description",
}
var values [][]string
labels, _, err := login.Client().ListRepoLabels(owner, repo, gitea.ListLabelsOptions{ListOptions: flags.GetListOptions(ctx)})
if err != nil {
log.Fatal(err)
}
if len(labels) == 0 {
print.OutputList(flags.GlobalOutputValue, headers, values)
return nil
}
p := termenv.ColorProfile()
fPath := ctx.String("save")
if len(fPath) > 0 {
f, err := os.Create(fPath)
if err != nil {
return err
}
defer f.Close()
for _, label := range labels {
fmt.Fprintf(f, "#%s %s\n", label.Color, label.Name)
}
} else {
for _, label := range labels {
color := termenv.String(label.Color)
values = append(
values,
[]string{
strconv.FormatInt(label.ID, 10),
fmt.Sprint(color.Background(p.Color("#" + label.Color))),
label.Name,
label.Description,
},
)
}
print.OutputList(flags.GlobalOutputValue, headers, values)
if ctx.IsSet("save") {
return task.LabelsExport(labels, ctx.String("save"))
}
print.LabelsList(labels, flags.GlobalOutputValue)
return nil
}

View File

@ -7,6 +7,7 @@ package cmd
import (
"fmt"
"code.gitea.io/tea/cmd/flags"
"code.gitea.io/tea/cmd/login"
"code.gitea.io/tea/modules/config"
"code.gitea.io/tea/modules/print"
@ -48,6 +49,6 @@ func runLoginDetail(name string) error {
return nil
}
print.LoginDetails(l)
print.LoginDetails(l, flags.GlobalOutputValue)
return nil
}

View File

@ -5,7 +5,6 @@
package login
import (
"fmt"
"log"
"code.gitea.io/tea/cmd/flags"
@ -32,27 +31,6 @@ func RunLoginList(ctx *cli.Context) error {
log.Fatal(err)
}
headers := []string{
"Name",
"URL",
"SSHHost",
"User",
"Default",
}
var values [][]string
for _, l := range config.Config.Logins {
values = append(values, []string{
l.Name,
l.URL,
l.GetSSHHost(),
l.User,
fmt.Sprint(l.Default),
})
}
print.OutputList(flags.GlobalOutputValue, headers, values)
print.LoginsList(config.Config.Logins, flags.GlobalOutputValue)
return nil
}

View File

@ -6,7 +6,6 @@ package milestones
import (
"fmt"
"strconv"
"code.gitea.io/tea/cmd/flags"
"code.gitea.io/tea/modules/config"
@ -105,44 +104,7 @@ func runMilestoneIssueList(ctx *cli.Context) error {
return err
}
headers := []string{
"Index",
"State",
"Kind",
"Author",
"Updated",
"Title",
}
var values [][]string
if len(issues) == 0 {
print.OutputList(flags.GlobalOutputValue, headers, values)
return nil
}
for _, issue := range issues {
name := issue.Poster.FullName
if len(name) == 0 {
name = issue.Poster.UserName
}
kind := "Issue"
if issue.PullRequest != nil {
kind = "Pull"
}
values = append(
values,
[]string{
strconv.FormatInt(issue.Index, 10),
string(issue.State),
kind,
name,
print.FormatTime(issue.Updated),
issue.Title,
},
)
}
print.OutputList(flags.GlobalOutputValue, headers, values)
print.IssuesPullsList(issues, flags.GlobalOutputValue)
return nil
}

View File

@ -5,7 +5,6 @@
package milestones
import (
"fmt"
"log"
"code.gitea.io/tea/cmd/flags"
@ -55,40 +54,6 @@ func RunMilestonesList(ctx *cli.Context) error {
log.Fatal(err)
}
headers := []string{
"Title",
}
if state == gitea.StateAll {
headers = append(headers, "State")
}
headers = append(headers,
"Open/Closed Issues",
"DueDate",
)
var values [][]string
for _, m := range milestones {
var deadline = ""
if m.Deadline != nil && !m.Deadline.IsZero() {
deadline = print.FormatTime(*m.Deadline)
}
item := []string{
m.Title,
}
if state == gitea.StateAll {
item = append(item, string(m.State))
}
item = append(item,
fmt.Sprintf("%d/%d", m.OpenIssues, m.ClosedIssues),
deadline,
)
values = append(values, item)
}
print.OutputList(flags.GlobalOutputValue, headers, values)
print.MilestonesList(milestones, flags.GlobalOutputValue, state)
return nil
}

View File

@ -6,7 +6,6 @@ package cmd
import (
"log"
"strings"
"code.gitea.io/tea/cmd/flags"
"code.gitea.io/tea/modules/config"
@ -78,41 +77,6 @@ func runNotifications(ctx *cli.Context) error {
log.Fatal(err)
}
headers := []string{
"Type",
"Index",
"Title",
}
if ctx.Bool("all") {
headers = append(headers, "Repository")
}
var values [][]string
for _, n := range news {
if n.Subject == nil {
continue
}
// if pull or Issue get Index
var index string
if n.Subject.Type == "Issue" || n.Subject.Type == "Pull" {
index = n.Subject.URL
urlParts := strings.Split(n.Subject.URL, "/")
if len(urlParts) != 0 {
index = urlParts[len(urlParts)-1]
}
index = "#" + index
}
item := []string{n.Subject.Type, index, n.Subject.Title}
if ctx.Bool("all") {
item = append(item, n.Repository.FullName)
}
values = append(values, item)
}
if len(values) != 0 {
print.OutputList(flags.GlobalOutputValue, headers, values)
}
print.NotificationsList(news, flags.GlobalOutputValue, ctx.Bool("all"))
return nil
}

View File

@ -40,7 +40,7 @@ func RunOrganizationList(ctx *cli.Context) error {
log.Fatal(err)
}
print.OrganizationsList(userOrganizations)
print.OrganizationsList(userOrganizations, flags.GlobalOutputValue)
return nil
}

View File

@ -6,7 +6,6 @@ package pulls
import (
"log"
"strconv"
"code.gitea.io/tea/cmd/flags"
"code.gitea.io/tea/modules/config"
@ -48,47 +47,6 @@ func RunPullsList(ctx *cli.Context) error {
log.Fatal(err)
}
headers := []string{
"Index",
"Title",
"State",
"Author",
"Milestone",
"Updated",
}
var values [][]string
if len(prs) == 0 {
print.OutputList(flags.GlobalOutputValue, headers, values)
return nil
}
for _, pr := range prs {
if pr == nil {
continue
}
author := pr.Poster.FullName
if len(author) == 0 {
author = pr.Poster.UserName
}
mile := ""
if pr.Milestone != nil {
mile = pr.Milestone.Title
}
values = append(
values,
[]string{
strconv.FormatInt(pr.Index, 10),
pr.Title,
string(pr.State),
author,
mile,
print.FormatTime(*pr.Updated),
},
)
}
print.OutputList(flags.GlobalOutputValue, headers, values)
print.PullsList(prs, flags.GlobalOutputValue)
return nil
}

View File

@ -38,41 +38,7 @@ func RunReleasesList(ctx *cli.Context) error {
log.Fatal(err)
}
headers := []string{
"Tag-Name",
"Title",
"Published At",
"Status",
"Tar URL",
}
var values [][]string
if len(releases) == 0 {
print.OutputList(flags.GlobalOutputValue, headers, values)
return nil
}
for _, release := range releases {
status := "released"
if release.IsDraft {
status = "draft"
} else if release.IsPrerelease {
status = "prerelease"
}
values = append(
values,
[]string{
release.TagName,
release.Title,
print.FormatTime(release.PublishedAt),
status,
release.TarURL,
},
)
}
print.OutputList(flags.GlobalOutputValue, headers, values)
print.ReleasesList(releases, flags.GlobalOutputValue)
return nil
}

View File

@ -80,7 +80,7 @@ func RunReposList(ctx *cli.Context) error {
reposFiltered = filterReposByType(rps, typeFilter)
}
print.ReposList(reposFiltered, getFields(ctx))
print.ReposList(reposFiltered, flags.GlobalOutputValue, getFields(ctx))
return nil
}

View File

@ -123,6 +123,6 @@ func runReposSearch(ctx *cli.Context) error {
return err
}
print.ReposList(rps, getFields(ctx))
print.ReposList(rps, flags.GlobalOutputValue, getFields(ctx))
return nil
}

View File

@ -6,13 +6,14 @@ package print
import (
"fmt"
"strconv"
"code.gitea.io/sdk/gitea"
)
// IssueDetails print an issue rendered to stdout
func IssueDetails(issue *gitea.Issue) {
OutputMarkdown(fmt.Sprintf(
outputMarkdown(fmt.Sprintf(
"# #%d %s (%s)\n@%s created %s\n\n%s\n",
issue.Index,
issue.Title,
@ -22,3 +23,87 @@ func IssueDetails(issue *gitea.Issue) {
issue.Body,
))
}
// IssuesList prints a listing of issues
func IssuesList(issues []*gitea.Issue, output string) {
var values [][]string
headers := []string{
"Index",
"Title",
"State",
"Author",
"Milestone",
"Updated",
}
if len(issues) == 0 {
outputList(output, headers, values)
return
}
for _, issue := range issues {
author := issue.Poster.FullName
if len(author) == 0 {
author = issue.Poster.UserName
}
mile := ""
if issue.Milestone != nil {
mile = issue.Milestone.Title
}
values = append(
values,
[]string{
strconv.FormatInt(issue.Index, 10),
issue.Title,
string(issue.State),
author,
mile,
FormatTime(issue.Updated),
},
)
}
outputList(output, headers, values)
}
// IssuesPullsList prints a listing of issues & pulls
// TODO combine with IssuesList
func IssuesPullsList(issues []*gitea.Issue, output string) {
var values [][]string
headers := []string{
"Index",
"State",
"Kind",
"Author",
"Updated",
"Title",
}
if len(issues) == 0 {
outputList(output, headers, values)
return
}
for _, issue := range issues {
name := issue.Poster.FullName
if len(name) == 0 {
name = issue.Poster.UserName
}
kind := "Issue"
if issue.PullRequest != nil {
kind = "Pull"
}
values = append(
values,
[]string{
strconv.FormatInt(issue.Index, 10),
string(issue.State),
kind,
name,
FormatTime(issue.Updated),
issue.Title,
},
)
}
outputList(output, headers, values)
}

46
modules/print/label.go Normal file
View File

@ -0,0 +1,46 @@
// 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"
"strconv"
"code.gitea.io/sdk/gitea"
"github.com/muesli/termenv"
)
// LabelsList prints a listing of labels
func LabelsList(labels []*gitea.Label, output string) {
var values [][]string
headers := []string{
"Index",
"Color",
"Name",
"Description",
}
if len(labels) == 0 {
outputList(output, headers, values)
return
}
p := termenv.ColorProfile()
for _, label := range labels {
color := termenv.String(label.Color)
values = append(
values,
[]string{
strconv.FormatInt(label.ID, 10),
fmt.Sprint(color.Background(p.Color("#" + label.Color))),
label.Name,
label.Description,
},
)
}
outputList(output, headers, values)
}

View File

@ -72,9 +72,9 @@ func outputyaml(headers []string, values [][]string) {
}
}
// OutputList provides general function to convert given list of items
// outputList provides general function to convert given list of items
// into several outputs (table, csv, simple, tsv, yaml)
func OutputList(output string, headers []string, values [][]string) {
func outputList(output string, headers []string, values [][]string) {
switch {
case output == "" || output == "table":
outputtable(headers, values)

View File

@ -13,7 +13,7 @@ import (
)
// LoginDetails print login entry to stdout
func LoginDetails(login *config.Login) {
func LoginDetails(login *config.Login, output string) {
in := fmt.Sprintf("# %s\n\n[@%s](%s/%s)\n",
login.Name,
login.User,
@ -28,5 +28,29 @@ func LoginDetails(login *config.Login) {
}
in += fmt.Sprintf("\nCreated: %s", time.Unix(login.Created, 0).Format(time.RFC822))
OutputMarkdown(in)
outputMarkdown(in)
}
// LoginsList prints a listing of logins
func LoginsList(logins []config.Login, output string) {
var values [][]string
headers := []string{
"Name",
"URL",
"SSHHost",
"User",
"Default",
}
for _, l := range logins {
values = append(values, []string{
l.Name,
l.URL,
l.GetSSHHost(),
l.User,
fmt.Sprint(l.Default),
})
}
outputList(output, headers, values)
}

View File

@ -10,10 +10,10 @@ import (
"github.com/charmbracelet/glamour"
)
// OutputMarkdown prints markdown to stdout, formatted for terminals.
// outputMarkdown prints markdown to stdout, formatted for terminals.
// If the input could not be parsed, it is printed unformatted, the error
// is returned anyway.
func OutputMarkdown(markdown string) error {
func outputMarkdown(markdown string) error {
out, err := glamour.Render(markdown, "auto")
if err != nil {
fmt.Printf(markdown)

View File

@ -22,3 +22,42 @@ func MilestoneDetails(milestone *gitea.Milestone) {
fmt.Printf("\nDeadline: %s\n", FormatTime(*milestone.Deadline))
}
}
// MilestonesList prints a listing of milestones
func MilestonesList(miles []*gitea.Milestone, output string, state gitea.StateType) {
headers := []string{
"Title",
}
if state == gitea.StateAll {
headers = append(headers, "State")
}
headers = append(headers,
"Open/Closed Issues",
"DueDate",
)
var values [][]string
for _, m := range miles {
var deadline = ""
if m.Deadline != nil && !m.Deadline.IsZero() {
deadline = FormatTime(*m.Deadline)
}
item := []string{
m.Title,
}
if state == gitea.StateAll {
item = append(item, string(m.State))
}
item = append(item,
fmt.Sprintf("%d/%d", m.OpenIssues, m.ClosedIssues),
deadline,
)
values = append(values, item)
}
outputList(output, headers, values)
}

View File

@ -0,0 +1,51 @@
// 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 (
"strings"
"code.gitea.io/sdk/gitea"
)
// NotificationsList prints a listing of notification threads
func NotificationsList(news []*gitea.NotificationThread, output string, showRepository bool) {
var values [][]string
headers := []string{
"Type",
"Index",
"Title",
}
if showRepository {
headers = append(headers, "Repository")
}
for _, n := range news {
if n.Subject == nil {
continue
}
// if pull or Issue get Index
var index string
if n.Subject.Type == "Issue" || n.Subject.Type == "Pull" {
index = n.Subject.URL
urlParts := strings.Split(n.Subject.URL, "/")
if len(urlParts) != 0 {
index = urlParts[len(urlParts)-1]
}
index = "#" + index
}
item := []string{n.Subject.Type, index, n.Subject.Title}
if showRepository {
item = append(item, n.Repository.FullName)
}
values = append(values, item)
}
if len(values) != 0 {
outputList(output, headers, values)
}
return
}

View File

@ -8,11 +8,10 @@ import (
"fmt"
"code.gitea.io/sdk/gitea"
"code.gitea.io/tea/cmd/flags"
)
// OrganizationsList prints a listing of the organizations
func OrganizationsList(organizations []*gitea.Organization) {
func OrganizationsList(organizations []*gitea.Organization, output string) {
if len(organizations) == 0 {
fmt.Println("No organizations found")
return
@ -41,5 +40,5 @@ func OrganizationsList(organizations []*gitea.Organization) {
)
}
OutputList(flags.GlobalOutputValue, headers, values)
outputList(output, headers, values)
}

View File

@ -6,6 +6,7 @@ package print
import (
"fmt"
"strconv"
"code.gitea.io/sdk/gitea"
)
@ -54,5 +55,50 @@ func PullDetails(pr *gitea.PullRequest, reviews []*gitea.PullReview) {
out += "\nNo Conflicts"
}
OutputMarkdown(out)
outputMarkdown(out)
}
// PullsList prints a listing of pulls
func PullsList(prs []*gitea.PullRequest, output string) {
var values [][]string
headers := []string{
"Index",
"Title",
"State",
"Author",
"Milestone",
"Updated",
}
if len(prs) == 0 {
outputList(output, headers, values)
return
}
for _, pr := range prs {
if pr == nil {
continue
}
author := pr.Poster.FullName
if len(author) == 0 {
author = pr.Poster.UserName
}
mile := ""
if pr.Milestone != nil {
mile = pr.Milestone.Title
}
values = append(
values,
[]string{
strconv.FormatInt(pr.Index, 10),
pr.Title,
string(pr.State),
author,
mile,
FormatTime(*pr.Updated),
},
)
}
outputList(output, headers, values)
}

47
modules/print/release.go Normal file
View File

@ -0,0 +1,47 @@
// 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 (
"code.gitea.io/sdk/gitea"
)
// ReleasesList prints a listing of releases
func ReleasesList(releases []*gitea.Release, output string) {
var values [][]string
headers := []string{
"Tag-Name",
"Title",
"Published At",
"Status",
"Tar URL",
}
if len(releases) == 0 {
outputList(output, headers, values)
return
}
for _, release := range releases {
status := "released"
if release.IsDraft {
status = "draft"
} else if release.IsPrerelease {
status = "prerelease"
}
values = append(
values,
[]string{
release.TagName,
release.Title,
FormatTime(release.PublishedAt),
status,
release.TarURL,
},
)
}
outputList(output, headers, values)
}

View File

@ -11,7 +11,6 @@ import (
"time"
"code.gitea.io/sdk/gitea"
"code.gitea.io/tea/cmd/flags"
)
type rp = *gitea.Repository
@ -60,7 +59,7 @@ func init() {
}
// ReposList prints a listing of the repos
func ReposList(repos []*gitea.Repository, fields []string) {
func ReposList(repos []*gitea.Repository, output string, fields []string) {
if len(repos) == 0 {
fmt.Println("No repositories found")
return
@ -91,7 +90,7 @@ func ReposList(repos []*gitea.Repository, fields []string) {
}
}
OutputList(flags.GlobalOutputValue, fields, values)
outputList(output, fields, values)
}
// RepoDetails print an repo formatted to stdout
@ -150,7 +149,7 @@ func RepoDetails(repo *gitea.Repository, topics []string) {
tops = fmt.Sprintf("- Topics:\t%s\n", strings.Join(topics, ", "))
}
OutputMarkdown(fmt.Sprintf(
outputMarkdown(fmt.Sprintf(
"%s%s\n%s\n%s%s%s%s",
title,
desc,

View File

@ -59,5 +59,5 @@ func TrackedTimesList(times []*gitea.TrackedTime, outputType string, from, until
"User",
"Duration",
}
OutputList(outputType, headers, outputValues)
outputList(outputType, headers, outputValues)
}

View File

@ -0,0 +1,29 @@
// 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 task
import (
"fmt"
"log"
"os"
"code.gitea.io/sdk/gitea"
)
// LabelsExport save list of labels to disc
func LabelsExport(labels []*gitea.Label, path string) error {
f, err := os.Create(path)
if err != nil {
log.Fatal(err)
}
defer f.Close()
for _, label := range labels {
if _, err := fmt.Fprintf(f, "#%s %s\n", label.Color, label.Name); err != nil {
return err
}
}
return nil
}