File: user.go

package info (click to toggle)
tea-cli 0.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,364 kB
  • sloc: makefile: 120; sh: 17
file content (135 lines) | stat: -rw-r--r-- 2,945 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2021 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"

	"code.gitea.io/sdk/gitea"
)

// UserDetails print a formatted user to stdout
func UserDetails(user *gitea.User) {
	title := "# " + user.UserName
	if user.IsAdmin {
		title += " (admin)"
	}
	if !user.IsActive {
		title += " (disabled)"
	}
	if user.Restricted {
		title += " (restricted)"
	}
	if user.ProhibitLogin {
		title += " (login prohibited)"
	}
	title += "\n"

	var desc string
	if len(user.Description) != 0 {
		desc = fmt.Sprintf("*%s*\n\n", user.Description)
	}
	var website string
	if len(user.Website) != 0 {
		website = fmt.Sprintf("%s\n\n", user.Website)
	}

	stats := fmt.Sprintf(
		"Follower Count: %d, Following Count: %d, Starred Repos: %d\n",
		user.FollowerCount,
		user.FollowingCount,
		user.StarredRepoCount,
	)

	outputMarkdown(fmt.Sprintf(
		"%s%s\n%s\n%s",
		title,
		desc,
		website,
		stats,
	), "")
}

// UserList prints a listing of the users
func UserList(user []*gitea.User, output string, fields []string) {
	var printables = make([]printable, len(user))
	for i, u := range user {
		printables[i] = &printableUser{u}
	}
	t := tableFromItems(fields, printables, isMachineReadable(output))
	t.print(output)
}

// UserFields are the available fields to print with UserList()
var UserFields = []string{
	"id",
	"login",
	"full_name",
	"email",
	"avatar_url",
	"language",
	"is_admin",
	"restricted",
	"prohibit_login",
	"location",
	"website",
	"description",
	"visibility",
	"activated",
	"lastlogin_at",
	"created_at",
}

type printableUser struct{ *gitea.User }

func (x printableUser) FormatField(field string, machineReadable bool) string {
	switch field {
	case "id":
		return fmt.Sprintf("%d", x.ID)
	case "login":
		if x.IsAdmin {
			return fmt.Sprintf("%s (admin)", x.UserName)
		}
		if !x.IsActive {
			return fmt.Sprintf("%s (disabled)", x.UserName)
		}
		if x.Restricted {
			return fmt.Sprintf("%s (restricted)", x.UserName)
		}
		if x.ProhibitLogin {
			return fmt.Sprintf("%s (login prohibited)", x.UserName)
		}
		return x.UserName
	case "full_name":
		return x.FullName
	case "email":
		return x.Email
	case "avatar_url":
		return x.AvatarURL
	case "language":
		return x.Language
	case "is_admin":
		return formatBoolean(x.IsAdmin, !machineReadable)
	case "restricted":
		return formatBoolean(x.Restricted, !machineReadable)
	case "prohibit_login":
		return formatBoolean(x.ProhibitLogin, !machineReadable)
	case "activated":
		return formatBoolean(x.IsActive, !machineReadable)
	case "location":
		return x.Location
	case "website":
		return x.Website
	case "description":
		return x.Description
	case "visibility":
		return string(x.Visibility)
	case "created_at":
		return FormatTime(x.Created, machineReadable)
	case "lastlogin_at":
		return FormatTime(x.LastLogin, machineReadable)
	}
	return ""
}