| 55 | } |
| 56 | |
| 57 | func deleteRun(opts *DeleteOptions) error { |
| 58 | httpClient, err := opts.HttpClient() |
| 59 | if err != nil { |
| 60 | return err |
| 61 | } |
| 62 | |
| 63 | cfg, err := opts.Config() |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | |
| 68 | host, _ := cfg.Authentication().DefaultHost() |
| 69 | gpgKeys, err := getGPGKeys(httpClient, host) |
| 70 | if err != nil { |
| 71 | return err |
| 72 | } |
| 73 | |
| 74 | id := "" |
| 75 | for _, gpgKey := range gpgKeys { |
| 76 | if gpgKey.KeyID == opts.KeyID { |
| 77 | id = strconv.FormatInt(gpgKey.ID, 10) |
| 78 | break |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if id == "" { |
| 83 | return fmt.Errorf("unable to delete GPG key %s: either the GPG key is not found or it is not owned by you", opts.KeyID) |
| 84 | } |
| 85 | |
| 86 | if !opts.Confirmed { |
| 87 | if err := opts.Prompter.ConfirmDeletion(opts.KeyID); err != nil { |
| 88 | return err |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | err = deleteGPGKey(httpClient, host, id) |
| 93 | if err != nil { |
| 94 | return err |
| 95 | } |
| 96 | |
| 97 | if opts.IO.IsStdoutTTY() { |
| 98 | cs := opts.IO.ColorScheme() |
| 99 | fmt.Fprintf(opts.IO.Out, "%s GPG key %s deleted from your account\n", cs.SuccessIcon(), opts.KeyID) |
| 100 | } |
| 101 | |
| 102 | return nil |
| 103 | } |