refactor: improve code quality and efficiency in various files (#548)

- Replace loadConfig() with _ = loadConfig()
- Update file permissions from 0660 to 0o660
- Simplify variable declarations
- Replace golang.org/x/crypto/ssh/terminal with golang.org/x/term
- Remove unused getCertPrincipals function
- Replace time.Now().Sub() with time.Since()
- Add test for ArgToIndex function

Signed-off-by: appleboy <appleboy.tw@gmail.com>

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-on: https://gitea.com/gitea/tea/pulls/548
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: techknowlogick <techknowlogick@noreply.gitea.io>
Co-authored-by: appleboy <appleboy.tw@gmail.com>
Co-committed-by: appleboy <appleboy.tw@gmail.com>
This commit is contained in:
appleboy
2023-04-30 11:43:26 +08:00
committed by Lunny Xiao
parent 4915862b95
commit b02263adb0
16 changed files with 78 additions and 48 deletions

View File

@ -18,12 +18,12 @@ func Comments(comments []*gitea.Comment) {
baseURL = getRepoURL(comments[0].HTMLURL)
}
var out = make([]string, len(comments))
out := make([]string, len(comments))
for i, c := range comments {
out[i] = formatComment(c)
}
outputMarkdown(fmt.Sprintf(
_ = outputMarkdown(fmt.Sprintf(
// this will become a heading by means of the first --- from a comment
"Comments\n%s",
strings.Join(out, "\n"),
@ -32,7 +32,7 @@ func Comments(comments []*gitea.Comment) {
// Comment renders a comment to stdout
func Comment(c *gitea.Comment) {
outputMarkdown(formatComment(c), getRepoURL(c.HTMLURL))
_ = outputMarkdown(formatComment(c), getRepoURL(c.HTMLURL))
}
func formatComment(c *gitea.Comment) string {

View File

@ -12,12 +12,12 @@ import (
"code.gitea.io/sdk/gitea"
"github.com/muesli/termenv"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/term"
)
// IsInteractive checks if the output is piped, but NOT if the session is run interactively..
func IsInteractive() bool {
return terminal.IsTerminal(int(os.Stdout.Fd()))
return term.IsTerminal(int(os.Stdout.Fd()))
}
// captures the repo URL part <host>/<owner>/<repo> of an url

View File

@ -28,7 +28,7 @@ func IssueDetails(issue *gitea.Issue, reactions []*gitea.Reaction) {
out += fmt.Sprintf("\n---\n\n%s\n", formatReactions(reactions))
}
outputMarkdown(out, getRepoURL(issue.HTMLURL))
_ = outputMarkdown(out, getRepoURL(issue.HTMLURL))
}
func formatReactions(reactions []*gitea.Reaction) string {
@ -74,7 +74,7 @@ var IssueFields = []string{
func printIssues(issues []*gitea.Issue, output string, fields []string) {
labelMap := map[int64]string{}
var printables = make([]printable, len(issues))
printables := make([]printable, len(issues))
machineReadable := isMachineReadable(output)
for i, x := range issues {
@ -133,13 +133,13 @@ func (x printableIssue) FormatField(field string, machineReadable bool) string {
}
return ""
case "labels":
var labels = make([]string, len(x.Labels))
labels := make([]string, len(x.Labels))
for i, l := range x.Labels {
labels[i] = (*x.formattedLabels)[l.ID]
}
return strings.Join(labels, " ")
case "assignees":
var assignees = make([]string, len(x.Assignees))
assignees := make([]string, len(x.Assignees))
for i, a := range x.Assignees {
assignees[i] = formatUserName(a)
}

View File

@ -28,7 +28,7 @@ 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

View File

@ -9,7 +9,7 @@ import (
"os"
"github.com/charmbracelet/glamour"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/term"
)
// outputMarkdown prints markdown to stdout, formatted for terminals.
@ -47,8 +47,8 @@ func outputMarkdown(markdown string, baseURL string) error {
func getWordWrap() int {
fd := int(os.Stdout.Fd())
width := 80
if terminal.IsTerminal(fd) {
if w, _, err := terminal.GetSize(fd); err == nil {
if term.IsTerminal(fd) {
if w, _, err := term.GetSize(fd); err == nil {
width = w
}
}

View File

@ -12,7 +12,7 @@ import (
// OrganizationDetails prints details of an org with formatting
func OrganizationDetails(org *gitea.Organization) {
outputMarkdown(fmt.Sprintf(
_ = outputMarkdown(fmt.Sprintf(
"# %s\n%s\n\n- Visibility: %s\n- Location: %s\n- Website: %s\n",
org.UserName,
org.Description,

View File

@ -14,7 +14,7 @@ import (
// ReposList prints a listing of the repos
func ReposList(repos []*gitea.Repository, output string, fields []string) {
var printables = make([]printable, len(repos))
printables := make([]printable, len(repos))
for i, r := range repos {
printables[i] = &printableRepo{r}
}
@ -56,7 +56,7 @@ func RepoDetails(repo *gitea.Repository, topics []string) {
updated := fmt.Sprintf(
"Updated: %s (%s ago)\n",
repo.Updated.Format("2006-01-02 15:04"),
time.Now().Sub(repo.Updated).Truncate(time.Minute),
time.Since(repo.Updated).Truncate(time.Minute),
)
urls := fmt.Sprintf(

View File

@ -66,7 +66,6 @@ func (t *table) sort(column uint, desc bool) {
func (t table) Len() int { return len(t.values) }
func (t table) Swap(i, j int) { t.values[i], t.values[j] = t.values[j], t.values[i] }
func (t table) Less(i, j int) bool {
const column = 0
if t.sortDesc {
i, j = j, i
}