From 723b0553c555e0d0519f3d4fb8c8cedd13e272a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20=C4=8Cerm=C3=A1k?= Date: Fri, 26 Jul 2024 16:18:58 +0000 Subject: [PATCH] Add repos rm/delete command (#609) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes https://gitea.com/gitea/tea/issues/239 Co-authored-by: techknowlogick Reviewed-on: https://gitea.com/gitea/tea/pulls/609 Co-authored-by: Dan Čermák Co-committed-by: Dan Čermák --- cmd/repos.go | 1 + cmd/repos/delete.go | 85 +++++++++++++++++++++++++++++++++++++++++++++ docs/CLI.md | 14 ++++++++ 3 files changed, 100 insertions(+) create mode 100644 cmd/repos/delete.go diff --git a/cmd/repos.go b/cmd/repos.go index 8bd274a..1482c47 100644 --- a/cmd/repos.go +++ b/cmd/repos.go @@ -29,6 +29,7 @@ var CmdRepos = cli.Command{ &repos.CmdRepoCreateFromTemplate, &repos.CmdRepoFork, &repos.CmdRepoMigrate, + &repos.CmdRepoRm, }, Flags: repos.CmdReposListFlags, } diff --git a/cmd/repos/delete.go b/cmd/repos/delete.go new file mode 100644 index 0000000..93b5bae --- /dev/null +++ b/cmd/repos/delete.go @@ -0,0 +1,85 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package repos + +import ( + "fmt" + + "code.gitea.io/tea/cmd/flags" + "code.gitea.io/tea/modules/context" + + "github.com/AlecAivazis/survey/v2" + "github.com/urfave/cli/v2" +) + +// CmdRepoRm represents a sub command of repos to delete an existing repo +var CmdRepoRm = cli.Command{ + Name: "delete", + Aliases: []string{"rm"}, + Usage: "Delete an existing repository", + Description: "Removes a repository from Create a repository from an existing repo", + ArgsUsage: " ", // command does not accept arguments + Action: runRepoDelete, + Flags: append([]cli.Flag{ + &cli.StringFlag{ + Name: "name", + Aliases: []string{""}, + Required: true, + Usage: "name of the repo", + }, + &cli.StringFlag{ + Name: "owner", + Aliases: []string{"O"}, + Required: false, + Usage: "owner of the repo", + }, + &cli.BoolFlag{ + Name: "force", + Aliases: []string{"f"}, + Required: false, + Value: false, + Usage: "Force the deletion and don't ask for confirmation", + }, + }, flags.LoginOutputFlags...), +} + +func runRepoDelete(cmd *cli.Context) error { + ctx := context.InitCommand(cmd) + + client := ctx.Login.Client() + + var owner string + if ctx.IsSet("owner") { + owner = ctx.String("owner") + + } else { + owner = ctx.Login.User + } + + repoName := ctx.String("name") + + repoSlug := fmt.Sprintf("%s/%s", owner, repoName) + + if !ctx.Bool("force") { + var enteredRepoSlug string + promptRepoName := &survey.Input{ + Message: fmt.Sprintf("Confirm the deletion of the repository '%s' by typing its name: ", repoSlug), + } + if err := survey.AskOne(promptRepoName, &enteredRepoSlug, survey.WithValidator(survey.Required)); err != nil { + return err + } + + if enteredRepoSlug != repoSlug { + return fmt.Errorf("Entered wrong repository name '%s', expected '%s'", enteredRepoSlug, repoSlug) + } + } + + _, err := client.DeleteRepo(owner, repoName) + if err != nil { + return err + } + + fmt.Printf("Successfully deleted %s/%s\n", owner, repoName) + return nil +} diff --git a/docs/CLI.md b/docs/CLI.md index b0f88eb..49d87cd 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -1173,6 +1173,20 @@ Migrate a repository **--wiki**: Copy the wiki +### delete, rm + +Delete an existing repository + +**--force, -f**: Force the deletion and don't ask for confirmation + +**--login, -l**="": Use a different Gitea Login. Optional + +**--name, -**="": name of the repo + +**--output, -o**="": Output format. (simple, table, csv, tsv, yaml, json) + +**--owner, -O**="": owner of the repo + ## branches, branch, b Consult branches