From 1e59dee685f8b5a2c18b3ee8fbebbec1de9bdba3 Mon Sep 17 00:00:00 2001 From: Norwin Date: Wed, 29 Sep 2021 04:32:16 +0800 Subject: [PATCH] Add `tea org create ` (#420) fixes #287, fixes #363 Co-authored-by: Norwin Reviewed-on: https://gitea.com/gitea/tea/pulls/420 Reviewed-by: techknowlogick Reviewed-by: 6543 <6543@obermui.de> Co-authored-by: Norwin Co-committed-by: Norwin --- cmd/organizations.go | 22 ++++++--- cmd/organizations/create.go | 90 +++++++++++++++++++++++++++++++++++ modules/print/organization.go | 12 +++++ 3 files changed, 117 insertions(+), 7 deletions(-) create mode 100644 cmd/organizations/create.go diff --git a/cmd/organizations.go b/cmd/organizations.go index b879a13..c541fda 100644 --- a/cmd/organizations.go +++ b/cmd/organizations.go @@ -5,9 +5,9 @@ package cmd import ( - "fmt" - "code.gitea.io/tea/cmd/organizations" + "code.gitea.io/tea/modules/context" + "code.gitea.io/tea/modules/print" "github.com/urfave/cli/v2" ) @@ -23,18 +23,26 @@ var CmdOrgs = cli.Command{ Action: runOrganizations, Subcommands: []*cli.Command{ &organizations.CmdOrganizationList, + &organizations.CmdOrganizationCreate, &organizations.CmdOrganizationDelete, }, Flags: organizations.CmdOrganizationList.Flags, } -func runOrganizations(ctx *cli.Context) error { +func runOrganizations(cmd *cli.Context) error { + ctx := context.InitCommand(cmd) if ctx.Args().Len() == 1 { - return runOrganizationDetail(ctx.Args().First()) + return runOrganizationDetail(ctx) } - return organizations.RunOrganizationList(ctx) + return organizations.RunOrganizationList(cmd) } -func runOrganizationDetail(path string) error { - return fmt.Errorf("Not yet implemented") +func runOrganizationDetail(ctx *context.TeaContext) error { + org, _, err := ctx.Login.Client().GetOrg(ctx.Args().First()) + if err != nil { + return err + } + + print.OrganizationDetails(org) + return nil } diff --git a/cmd/organizations/create.go b/cmd/organizations/create.go new file mode 100644 index 0000000..e5fbfcf --- /dev/null +++ b/cmd/organizations/create.go @@ -0,0 +1,90 @@ +// 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 organizations + +import ( + "fmt" + + "code.gitea.io/tea/cmd/flags" + "code.gitea.io/tea/modules/context" + "code.gitea.io/tea/modules/print" + + "code.gitea.io/sdk/gitea" + "github.com/urfave/cli/v2" +) + +// CmdOrganizationCreate represents a sub command of organizations to delete a given user organization +var CmdOrganizationCreate = cli.Command{ + Name: "create", + Aliases: []string{"c"}, + Usage: "Create an organization", + Description: "Create an organization", + Action: RunOrganizationCreate, + ArgsUsage: "", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "name", + Aliases: []string{"n"}, + }, + &cli.StringFlag{ + Name: "description", + Aliases: []string{"d"}, + }, + &cli.StringFlag{ + Name: "website", + Aliases: []string{"w"}, + }, + &cli.StringFlag{ + Name: "location", + Aliases: []string{"L"}, + }, + &cli.StringFlag{ + Name: "visibility", + Aliases: []string{"v"}, + }, + &cli.BoolFlag{ + Name: "repo-admins-can-change-team-access", + }, + &flags.LoginFlag, + }, +} + +// RunOrganizationCreate sets up a new organization +func RunOrganizationCreate(cmd *cli.Context) error { + ctx := context.InitCommand(cmd) + + if ctx.Args().Len() < 1 { + return fmt.Errorf("You have to specify the organization name you want to create") + } + + var visibility gitea.VisibleType + switch ctx.String("visibility") { + case "", "public": + visibility = gitea.VisibleTypePublic + case "private": + visibility = gitea.VisibleTypePrivate + case "limited": + visibility = gitea.VisibleTypeLimited + default: + return fmt.Errorf("unknown visibility '%s'", ctx.String("visibility")) + } + + org, _, err := ctx.Login.Client().CreateOrg(gitea.CreateOrgOption{ + Name: ctx.Args().First(), + // FullName: , // not really meaningful for orgs (not displayed in webui, use description instead?) + Description: ctx.String("description"), + Website: ctx.String("website"), + Location: ctx.String("location"), + RepoAdminChangeTeamAccess: ctx.Bool("repo-admins-can-change-team-access"), + Visibility: visibility, + }) + if err != nil { + return err + } + + print.OrganizationDetails(org) + + return err +} diff --git a/modules/print/organization.go b/modules/print/organization.go index f073d90..a9e10c6 100644 --- a/modules/print/organization.go +++ b/modules/print/organization.go @@ -10,6 +10,18 @@ import ( "code.gitea.io/sdk/gitea" ) +// OrganizationDetails prints details of an org with formatting +func OrganizationDetails(org *gitea.Organization) { + outputMarkdown(fmt.Sprintf( + "# %s\n%s\n\n- Visibility: %s\n- Location: %s\n- Website: %s\n", + org.UserName, + org.Description, + org.Visibility, + org.Location, + org.Website, + ), "") +} + // OrganizationsList prints a listing of the organizations func OrganizationsList(organizations []*gitea.Organization, output string) { if len(organizations) == 0 {