|
| 1 | +package oauth |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/pterm/pterm" |
| 7 | + "github.com/urfave/cli/v2" |
| 8 | + |
| 9 | + "github.com/NodeOps-app/createos-cli/internal/terminal" |
| 10 | +) |
| 11 | + |
| 12 | +func newDeleteCommand() *cli.Command { |
| 13 | + return &cli.Command{ |
| 14 | + Name: "delete", |
| 15 | + Usage: "Delete an OAuth client", |
| 16 | + ArgsUsage: "[client-id]", |
| 17 | + Flags: []cli.Flag{ |
| 18 | + &cli.StringFlag{Name: "client", Usage: "OAuth client ID"}, |
| 19 | + &cli.BoolFlag{Name: "force", Usage: "Skip confirmation prompt (required in non-interactive mode)"}, |
| 20 | + }, |
| 21 | + Action: func(c *cli.Context) error { |
| 22 | + apiClient, err := getClient(c) |
| 23 | + if err != nil { |
| 24 | + return err |
| 25 | + } |
| 26 | + |
| 27 | + clientID, err := resolveOAuthClientID(c, apiClient) |
| 28 | + if err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + |
| 32 | + if !c.Bool("force") { |
| 33 | + if !terminal.IsInteractive() { |
| 34 | + return fmt.Errorf("use --force to confirm deletion in non-interactive mode\n\n Example:\n createos oauth-clients delete --client %s --force", clientID) |
| 35 | + } |
| 36 | + confirm, err := pterm.DefaultInteractiveConfirm. |
| 37 | + WithDefaultText(fmt.Sprintf("Permanently delete OAuth client %q?", clientID)). |
| 38 | + WithDefaultValue(false). |
| 39 | + Show() |
| 40 | + if err != nil { |
| 41 | + return fmt.Errorf("could not read confirmation: %w", err) |
| 42 | + } |
| 43 | + if !confirm { |
| 44 | + fmt.Println("Cancelled. Your OAuth client was not deleted.") |
| 45 | + return nil |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + if err := apiClient.DeleteOAuthClient(clientID); err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + pterm.Success.Println("OAuth client deleted.") |
| 54 | + return nil |
| 55 | + }, |
| 56 | + } |
| 57 | +} |
0 commit comments