From 7d486c2ec6ee3aefd2e619dc309c674426b50d85 Mon Sep 17 00:00:00 2001 From: khmarbaise Date: Mon, 7 Dec 2020 06:02:50 +0800 Subject: [PATCH] tea organizations list command (#264) Introduce tea organizations list command (#263) Fix #263 Add missing pagination options missing as suggest by reviewers. (#263) Signed-off-by: Karl Heinz Marbaise Co-authored-by: Karl Heinz Marbaise Reviewed-on: https://gitea.com/gitea/tea/pulls/264 Reviewed-by: Norwin Reviewed-by: 6543 <6543@obermui.de> Co-Authored-By: khmarbaise Co-Committed-By: khmarbaise --- README.md | 2 ++ cmd/organizations.go | 39 +++++++++++++++++++++++++++++ cmd/organizations/list.go | 46 +++++++++++++++++++++++++++++++++++ main.go | 1 + modules/print/organization.go | 45 ++++++++++++++++++++++++++++++++++ 5 files changed, 133 insertions(+) create mode 100644 cmd/organizations.go create mode 100644 cmd/organizations/list.go create mode 100644 modules/print/organization.go diff --git a/README.md b/README.md index d1826a1..184c72f 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,8 @@ times Operate on tracked times of a repositorys issues and pulls open Open something of the repository on web browser notifications Show notifications milestones List and create milestones +organizations List, create, delete organizations +help, h Shows a list of commands or help for one command ``` To fetch issues from different repos, use the `--remote` flag (when inside a gitea repository directory) or `--login` & `--repo` flags. diff --git a/cmd/organizations.go b/cmd/organizations.go new file mode 100644 index 0000000..0b7a8b6 --- /dev/null +++ b/cmd/organizations.go @@ -0,0 +1,39 @@ +// Copyright 2019 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 ( + "log" + + "code.gitea.io/tea/cmd/organizations" + + "github.com/urfave/cli/v2" +) + +// CmdOrgs represents handle organization +var CmdOrgs = cli.Command{ + Name: "organizations", + Aliases: []string{"organization", "org"}, + Usage: "List, create, delete organizations", + Description: "Show organization details", + ArgsUsage: "[]", + Action: runOrganizations, + Subcommands: []*cli.Command{ + &organizations.CmdOrganizationList, + }, +} + +func runOrganizations(ctx *cli.Context) error { + if ctx.Args().Len() == 1 { + return runOrganizationDetail(ctx.Args().First()) + } + return organizations.RunOrganizationList(ctx) +} + +func runOrganizationDetail(path string) error { + + log.Fatal("Not yet implemented.") + return nil +} diff --git a/cmd/organizations/list.go b/cmd/organizations/list.go new file mode 100644 index 0000000..de2fe0f --- /dev/null +++ b/cmd/organizations/list.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 organizations + +import ( + "log" + + "code.gitea.io/tea/cmd/flags" + "code.gitea.io/tea/modules/config" + "code.gitea.io/tea/modules/print" + + "code.gitea.io/sdk/gitea" + "github.com/urfave/cli/v2" +) + +// CmdOrganizationList represents a sub command of organizations to list users organizations +var CmdOrganizationList = cli.Command{ + Name: "ls", + Aliases: []string{"list"}, + Usage: "List Organizations", + Description: "List users organizations", + Action: RunOrganizationList, + Flags: append([]cli.Flag{ + &flags.PaginationPageFlag, + &flags.PaginationLimitFlag, + }, flags.AllDefaultFlags...), +} + +// RunOrganizationList list user organizations +func RunOrganizationList(ctx *cli.Context) error { + //TODO: Reconsider the usage InitCommandLoginOnly related to #200 + login := config.InitCommandLoginOnly(flags.GlobalLoginValue) + + client := login.Client() + + userOrganizations, _, err := client.ListUserOrgs(login.User, gitea.ListOrgsOptions{ListOptions: flags.GetListOptions(ctx)}) + if err != nil { + log.Fatal(err) + } + + print.OrganizationsList(userOrganizations) + + return nil +} diff --git a/main.go b/main.go index 5f5d2b7..f1253d8 100644 --- a/main.go +++ b/main.go @@ -39,6 +39,7 @@ func main() { &cmd.CmdOpen, &cmd.CmdNotifications, &cmd.CmdMilestones, + &cmd.CmdOrgs, } app.EnableBashCompletion = true err := app.Run(os.Args) diff --git a/modules/print/organization.go b/modules/print/organization.go new file mode 100644 index 0000000..35d5db8 --- /dev/null +++ b/modules/print/organization.go @@ -0,0 +1,45 @@ +// 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" + "code.gitea.io/tea/cmd/flags" +) + +// OrganizationsList prints a listing of the organizations +func OrganizationsList(organizations []*gitea.Organization) { + if len(organizations) == 0 { + fmt.Println("No organizations found") + return + } + + headers := []string{ + "Name", + "FullName", + "Website", + "Location", + "Description", + } + + var values [][]string + + for _, org := range organizations { + values = append( + values, + []string{ + org.UserName, + org.FullName, + org.Website, + org.Location, + org.Description, + }, + ) + } + + OutputList(flags.GlobalOutputValue, headers, values) +}