gitea.tea/cmd/deploytokens/delete.go
Ross Golder 803aeab5db
feat: Add 'tea deploy-tokens' command for repository deploy token management
- Add deploy-tokens subcommand with list, create, delete
- Create deploy token print helpers with token display (shown once only)
- Add unit tests for all commands
- Update SDK dependency to use fork with deploy token support

Relates to gitea/go-sdk#846
2026-09-08 11:05:40 +07:00

78 lines
1.8 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package deploytokens
import (
stdctx "context"
"errors"
"fmt"
"gitea.dev/tea/cmd/flags"
"gitea.dev/tea/modules/context"
"gitea.dev/tea/modules/utils"
"github.com/urfave/cli/v3"
)
var CmdDeployTokensDelete = cli.Command{
Name: "delete",
Aliases: []string{"rm"},
Usage: "Delete a deploy token",
Description: "Delete a deploy token by ID from a repository",
ArgsUsage: "<token-id>",
Action: runDeployTokensDelete,
Flags: append([]cli.Flag{
&cli.BoolFlag{
Name: "confirm",
Aliases: []string{"y"},
Usage: "confirm deletion without prompting",
},
}, flags.AllDefaultFlags...),
}
func runDeployTokensDelete(ctx stdctx.Context, cmd *cli.Command) error {
if cmd.Args().Len() == 0 {
return errors.New("deploy token ID is required")
}
c, err := context.InitCommand(cmd)
if err != nil {
return err
}
if err := c.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
return err
}
client := c.Login.Client()
keyID, err := utils.ArgToIndex(cmd.Args().First())
if err != nil {
return fmt.Errorf("invalid deploy token ID: %w", err)
}
key, _, err := client.GetDeployKey(ctx, c.Owner, c.Repo, keyID)
if err != nil {
return err
}
if key.KeyType != "token" {
return fmt.Errorf("key %d is not a deploy token (type: %s)", keyID, key.KeyType)
}
if !cmd.Bool("confirm") {
fmt.Printf("Are you sure you want to delete deploy token %d (%s)? [y/N] ", key.ID, key.Title)
var response string
fmt.Scanln(&response)
if response != "y" && response != "Y" && response != "yes" {
fmt.Println("Deletion canceled.")
return nil
}
}
if _, err = client.DeleteDeployKey(ctx, c.Owner, c.Repo, keyID); err != nil {
return err
}
fmt.Printf("Deploy token %d deleted successfully\n", keyID)
return nil
}