From 5cb3e1ded5b5198aa097d04d805d7d026fc06a8b Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 8 Dec 2020 18:28:54 +0800 Subject: [PATCH] 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 Reviewed-by: Andrew Thornton Co-Authored-By: 6543 <6543@obermui.de> Co-Committed-By: 6543 <6543@obermui.de> --- cmd/issues/list.go | 41 +---------------- cmd/labels.go | 50 ++------------------ cmd/login.go | 3 +- cmd/login/list.go | 24 +--------- cmd/milestones/issues.go | 40 +--------------- cmd/milestones/list.go | 37 +-------------- cmd/notifications.go | 38 +-------------- cmd/organizations/list.go | 2 +- cmd/pulls/list.go | 44 +----------------- cmd/releases/list.go | 36 +-------------- cmd/repos/list.go | 2 +- cmd/repos/search.go | 2 +- modules/print/issue.go | 87 ++++++++++++++++++++++++++++++++++- modules/print/label.go | 46 ++++++++++++++++++ modules/print/list.go | 4 +- modules/print/login.go | 28 ++++++++++- modules/print/markdown.go | 4 +- modules/print/milestone.go | 39 ++++++++++++++++ modules/print/notification.go | 51 ++++++++++++++++++++ modules/print/organization.go | 5 +- modules/print/pull.go | 48 ++++++++++++++++++- modules/print/release.go | 47 +++++++++++++++++++ modules/print/repo.go | 7 ++- modules/print/times.go | 2 +- modules/task/labels_export.go | 29 ++++++++++++ 25 files changed, 397 insertions(+), 319 deletions(-) create mode 100644 modules/print/label.go create mode 100644 modules/print/notification.go create mode 100644 modules/print/release.go create mode 100644 modules/task/labels_export.go diff --git a/cmd/issues/list.go b/cmd/issues/list.go index ed79bb9..10b64d0 100644 --- a/cmd/issues/list.go +++ b/cmd/issues/list.go @@ -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 } diff --git a/cmd/labels.go b/cmd/labels.go index b171614..468d7f4 100644 --- a/cmd/labels.go +++ b/cmd/labels.go @@ -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 } diff --git a/cmd/login.go b/cmd/login.go index c65c66c..2461b39 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -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 } diff --git a/cmd/login/list.go b/cmd/login/list.go index a49397e..2660f90 100644 --- a/cmd/login/list.go +++ b/cmd/login/list.go @@ -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 } diff --git a/cmd/milestones/issues.go b/cmd/milestones/issues.go index 16debc0..403a4d8 100644 --- a/cmd/milestones/issues.go +++ b/cmd/milestones/issues.go @@ -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 } diff --git a/cmd/milestones/list.go b/cmd/milestones/list.go index ba2db1b..780b786 100644 --- a/cmd/milestones/list.go +++ b/cmd/milestones/list.go @@ -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 } diff --git a/cmd/notifications.go b/cmd/notifications.go index 69d6d7a..348cad2 100644 --- a/cmd/notifications.go +++ b/cmd/notifications.go @@ -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 } diff --git a/cmd/organizations/list.go b/cmd/organizations/list.go index de2fe0f..ddab497 100644 --- a/cmd/organizations/list.go +++ b/cmd/organizations/list.go @@ -40,7 +40,7 @@ func RunOrganizationList(ctx *cli.Context) error { log.Fatal(err) } - print.OrganizationsList(userOrganizations) + print.OrganizationsList(userOrganizations, flags.GlobalOutputValue) return nil } diff --git a/cmd/pulls/list.go b/cmd/pulls/list.go index 73ee2fe..66d9325 100644 --- a/cmd/pulls/list.go +++ b/cmd/pulls/list.go @@ -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 } diff --git a/cmd/releases/list.go b/cmd/releases/list.go index 349efe0..6374b26 100644 --- a/cmd/releases/list.go +++ b/cmd/releases/list.go @@ -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 } diff --git a/cmd/repos/list.go b/cmd/repos/list.go index 6a045f7..22b7e21 100644 --- a/cmd/repos/list.go +++ b/cmd/repos/list.go @@ -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 } diff --git a/cmd/repos/search.go b/cmd/repos/search.go index 2efab97..13f7cd3 100644 --- a/cmd/repos/search.go +++ b/cmd/repos/search.go @@ -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 } diff --git a/modules/print/issue.go b/modules/print/issue.go index 2df7dfc..c352b89 100644 --- a/modules/print/issue.go +++ b/modules/print/issue.go @@ -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) +} diff --git a/modules/print/label.go b/modules/print/label.go new file mode 100644 index 0000000..936327c --- /dev/null +++ b/modules/print/label.go @@ -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) +} diff --git a/modules/print/list.go b/modules/print/list.go index 3981b09..55aa5f9 100644 --- a/modules/print/list.go +++ b/modules/print/list.go @@ -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) diff --git a/modules/print/login.go b/modules/print/login.go index e217515..870cded 100644 --- a/modules/print/login.go +++ b/modules/print/login.go @@ -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) } diff --git a/modules/print/markdown.go b/modules/print/markdown.go index 6c5e323..718944e 100644 --- a/modules/print/markdown.go +++ b/modules/print/markdown.go @@ -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) diff --git a/modules/print/milestone.go b/modules/print/milestone.go index bb2f470..a40503d 100644 --- a/modules/print/milestone.go +++ b/modules/print/milestone.go @@ -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) +} diff --git a/modules/print/notification.go b/modules/print/notification.go new file mode 100644 index 0000000..1f58d2d --- /dev/null +++ b/modules/print/notification.go @@ -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 +} diff --git a/modules/print/organization.go b/modules/print/organization.go index 35d5db8..194257f 100644 --- a/modules/print/organization.go +++ b/modules/print/organization.go @@ -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) } diff --git a/modules/print/pull.go b/modules/print/pull.go index 5f1e946..da4d6f8 100644 --- a/modules/print/pull.go +++ b/modules/print/pull.go @@ -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) } diff --git a/modules/print/release.go b/modules/print/release.go new file mode 100644 index 0000000..40b6068 --- /dev/null +++ b/modules/print/release.go @@ -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) +} diff --git a/modules/print/repo.go b/modules/print/repo.go index 754a4e9..59b024c 100644 --- a/modules/print/repo.go +++ b/modules/print/repo.go @@ -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, diff --git a/modules/print/times.go b/modules/print/times.go index b1d0bd3..60fcc17 100644 --- a/modules/print/times.go +++ b/modules/print/times.go @@ -59,5 +59,5 @@ func TrackedTimesList(times []*gitea.TrackedTime, outputType string, from, until "User", "Duration", } - OutputList(outputType, headers, outputValues) + outputList(outputType, headers, outputValues) } diff --git a/modules/task/labels_export.go b/modules/task/labels_export.go new file mode 100644 index 0000000..a41a98a --- /dev/null +++ b/modules/task/labels_export.go @@ -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 +}