Add repos rm/delete command

This fixes https://gitea.com/gitea/tea/issues/239

Signed-off-by: Dan Čermák <dcermak@suse.com>
This commit is contained in:
Dan Čermák 2023-11-30 09:44:32 +01:00
parent 649b0b1272
commit 236f3bde67
No known key found for this signature in database
GPG Key ID: 96B29BCAD2AE5A44
2 changed files with 87 additions and 0 deletions

View File

@ -29,6 +29,7 @@ var CmdRepos = cli.Command{
&repos.CmdRepoCreateFromTemplate,
&repos.CmdRepoFork,
&repos.CmdRepoMigrate,
&repos.CmdRepoRm,
},
Flags: repos.CmdReposListFlags,
}

86
cmd/repos/delete.go Normal file
View File

@ -0,0 +1,86 @@
// 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/urfave/cli/v2"
"github.com/AlecAivazis/survey/v2"
)
// CmdRepoFork represents a sub command of repos to fork 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
}
repo_name := ctx.String("name")
repo_slug := fmt.Sprintf("%s/%s", owner, repo_name)
if !ctx.Bool("force") {
var entered_repo_slug string
promptRepoName := &survey.Input{
Message: fmt.Sprintf("Confirm the deletion of the repository '%s' by typing its name: ", repo_slug),
}
if err := survey.AskOne(promptRepoName, &entered_repo_slug, survey.WithValidator(survey.Required)); err != nil {
return err
}
if entered_repo_slug != repo_slug {
return fmt.Errorf("Entered wrong repository name '%s', expected '%s'", entered_repo_slug, repo_slug)
}
}
_, err := client.DeleteRepo(owner, repo_name)
if err != nil {
return err
}
fmt.Printf("Successfully deleted %s/%s\n", owner, repo_name)
return nil
}