From 4475b64f7c5a6bd91466c30a9a3eef00dc39c34f Mon Sep 17 00:00:00 2001 From: Matti R Date: Mon, 4 Oct 2021 00:08:45 -0400 Subject: [PATCH 1/5] add whoami cli option --- 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..63c2c6a --- /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" +) + +// CmdTrackedTimes represents the command to operate repositories' times. +var CmdWhoami = cli.Command{ + Name: "whoami", + Category: catSetup, + Description: `.`, + 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..e75b97f --- /dev/null +++ b/modules/print/user.go @@ -0,0 +1,126 @@ +// 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" + + "code.gitea.io/sdk/gitea" +) + +// RepoDetails print an repo formatted 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, + ), "") +} + +// ReposList prints a listing of the repos +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 fmt.Sprintf("%t", x.IsAdmin) + case "restricted": + return fmt.Sprintf("%t", x.Restricted) + case "prohibit_login": + return fmt.Sprintf("%t", x.ProhibitLogin) + case "location": + return x.Location + case "website": + return x.Website + case "description": + return x.Description + case "visibility": + return string(x.Visibility) + } + return "" +} -- 2.40.1 From 3729b83b70154dbbaefcb7bec19f4279d6e4fa52 Mon Sep 17 00:00:00 2001 From: Matti R Date: Mon, 4 Oct 2021 11:22:03 -0400 Subject: [PATCH 2/5] update per feedback --- cmd/whoami.go | 4 ++-- modules/print/user.go | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cmd/whoami.go b/cmd/whoami.go index 63c2c6a..1cd8b83 100644 --- a/cmd/whoami.go +++ b/cmd/whoami.go @@ -11,11 +11,11 @@ import ( "github.com/urfave/cli/v2" ) -// CmdTrackedTimes represents the command to operate repositories' times. +// CmdWhoami represents the command to show current logged in user var CmdWhoami = cli.Command{ Name: "whoami", Category: catSetup, - Description: `.`, + 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) diff --git a/modules/print/user.go b/modules/print/user.go index e75b97f..651fede 100644 --- a/modules/print/user.go +++ b/modules/print/user.go @@ -10,7 +10,7 @@ import ( "code.gitea.io/sdk/gitea" ) -// RepoDetails print an repo formatted to stdout +// UserDetails print a formatted user to stdout func UserDetails(user *gitea.User) { title := "# " + user.UserName if user.IsAdmin { @@ -52,7 +52,7 @@ func UserDetails(user *gitea.User) { ), "") } -// ReposList prints a listing of the repos +// 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 { @@ -108,11 +108,11 @@ func (x printableUser) FormatField(field string, machineReadable bool) string { case "language": return x.Language case "is_admin": - return fmt.Sprintf("%t", x.IsAdmin) + return formatBoolean(x.IsAdmin, true) case "restricted": - return fmt.Sprintf("%t", x.Restricted) + return formatBoolean(x.Restricted, true) case "prohibit_login": - return fmt.Sprintf("%t", x.ProhibitLogin) + return formatBoolean(x.ProhibitLogin, true) case "location": return x.Location case "website": -- 2.40.1 From 9cc9a328134b75408ca1080b818f6b6b976e8fc0 Mon Sep 17 00:00:00 2001 From: Matti R Date: Mon, 4 Oct 2021 13:20:17 -0400 Subject: [PATCH 4/5] update date --- modules/print/user.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/print/user.go b/modules/print/user.go index 651fede..0927bdd 100644 --- a/modules/print/user.go +++ b/modules/print/user.go @@ -1,4 +1,4 @@ -// Copyright 2020 The Gitea Authors. All rights reserved. +// 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. -- 2.40.1 From 74850739aacb22f3c00dacb0a9eaa8b0da8d7876 Mon Sep 17 00:00:00 2001 From: Matti R Date: Mon, 4 Oct 2021 13:29:11 -0400 Subject: [PATCH 5/5] update per feedback --- modules/print/user.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/print/user.go b/modules/print/user.go index 0927bdd..301685a 100644 --- a/modules/print/user.go +++ b/modules/print/user.go @@ -108,11 +108,11 @@ func (x printableUser) FormatField(field string, machineReadable bool) string { case "language": return x.Language case "is_admin": - return formatBoolean(x.IsAdmin, true) + return formatBoolean(x.IsAdmin, !machineReadable) case "restricted": - return formatBoolean(x.Restricted, true) + return formatBoolean(x.Restricted, !machineReadable) case "prohibit_login": - return formatBoolean(x.ProhibitLogin, true) + return formatBoolean(x.ProhibitLogin, !machineReadable) case "location": return x.Location case "website": -- 2.40.1