mirror of
				https://gitea.com/gitea/tea.git
				synced 2025-10-31 01:05:26 +01:00 
			
		
		
		
	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:
		| @@ -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 | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|   | ||||
| @@ -40,7 +40,7 @@ func RunOrganizationList(ctx *cli.Context) error { | ||||
| 		log.Fatal(err) | ||||
| 	} | ||||
|  | ||||
| 	print.OrganizationsList(userOrganizations) | ||||
| 	print.OrganizationsList(userOrganizations, flags.GlobalOutputValue) | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 6543
					6543