From 4ffd994549c985fe761df01cc61be9ed913c90c1 Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Tue, 5 Oct 2021 01:41:59 +0800 Subject: [PATCH] Add `tea whoami` command (#426) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The User print will be used in future for list of users for admin Co-authored-by: Matti R Reviewed-on: https://gitea.com/gitea/tea/pulls/426 Reviewed-by: Norwin Reviewed-by: Alexey 〒erentyev Co-authored-by: techknowlogick Co-committed-by: techknowlogick --- cmd/whoami.go | 27 +++++++++ main.go | 1 + modules/print/user.go | 126 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 cmd/whoami.go create mode 100644 modules/print/user.go diff --git a/cmd/whoami.go b/cmd/whoami.go new file mode 100644 index 0000000..1cd8b83 --- /dev/null +++ b/cmd/whoami.go @@ -0,0 +1,27 @@ +// 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 cmd + +import ( + "code.gitea.io/tea/modules/context" + "code.gitea.io/tea/modules/print" + + "github.com/urfave/cli/v2" +) + +// CmdWhoami represents the command to show current logged in user +var CmdWhoami = cli.Command{ + Name: "whoami", + Category: catSetup, + Description: `For debugging purposes, show the user that is currently logged in.`, + Usage: "Show current logged in user", + Action: func(cmd *cli.Context) error { + ctx := context.InitCommand(cmd) + client := ctx.Login.Client() + user, _, _ := client.GetMyUserInfo() + print.UserDetails(user) + return nil + }, +} diff --git a/main.go b/main.go index cd68456..52735cf 100644 --- a/main.go +++ b/main.go @@ -35,6 +35,7 @@ func main() { &cmd.CmdLogin, &cmd.CmdLogout, &cmd.CmdAutocomplete, + &cmd.CmdWhoami, &cmd.CmdIssues, &cmd.CmdPulls, diff --git a/modules/print/user.go b/modules/print/user.go new file mode 100644 index 0000000..301685a --- /dev/null +++ b/modules/print/user.go @@ -0,0 +1,126 @@ +// 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", +} + +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 "location": + return x.Location + case "website": + return x.Website + case "description": + return x.Description + case "visibility": + return string(x.Visibility) + } + return "" +}