| 47 | } |
| 48 | |
| 49 | func listRun(opts *ListOptions) error { |
| 50 | apiClient, err := opts.HTTPClient() |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | |
| 55 | cfg, err := opts.Config() |
| 56 | if err != nil { |
| 57 | return err |
| 58 | } |
| 59 | |
| 60 | host, _ := cfg.Authentication().DefaultHost() |
| 61 | sshAuthKeys, authKeyErr := shared.UserKeys(apiClient, host, "") |
| 62 | if authKeyErr != nil { |
| 63 | printError(opts.IO.ErrOut, authKeyErr) |
| 64 | } |
| 65 | |
| 66 | sshSigningKeys, signKeyErr := shared.UserSigningKeys(apiClient, host, "") |
| 67 | if signKeyErr != nil { |
| 68 | printError(opts.IO.ErrOut, signKeyErr) |
| 69 | } |
| 70 | |
| 71 | if authKeyErr != nil && signKeyErr != nil { |
| 72 | return cmdutil.SilentError |
| 73 | } |
| 74 | |
| 75 | sshKeys := append(sshAuthKeys, sshSigningKeys...) |
| 76 | |
| 77 | if len(sshKeys) == 0 { |
| 78 | return cmdutil.NewNoResultsError("no SSH keys present in the GitHub account") |
| 79 | } |
| 80 | |
| 81 | t := tableprinter.New(opts.IO, tableprinter.WithHeader("TITLE", "ID", "KEY", "TYPE", "ADDED")) |
| 82 | cs := opts.IO.ColorScheme() |
| 83 | now := time.Now() |
| 84 | |
| 85 | for _, sshKey := range sshKeys { |
| 86 | id := strconv.Itoa(sshKey.ID) |
| 87 | if t.IsTTY() { |
| 88 | t.AddField(sshKey.Title) |
| 89 | t.AddField(id) |
| 90 | t.AddField(sshKey.Key, tableprinter.WithTruncate(truncateMiddle)) |
| 91 | t.AddField(sshKey.Type) |
| 92 | t.AddTimeField(now, sshKey.CreatedAt, cs.Muted) |
| 93 | } else { |
| 94 | t.AddField(sshKey.Title) |
| 95 | t.AddField(sshKey.Key) |
| 96 | t.AddTimeField(now, sshKey.CreatedAt, cs.Muted) |
| 97 | t.AddField(id) |
| 98 | t.AddField(sshKey.Type) |
| 99 | } |
| 100 | t.EndRow() |
| 101 | } |
| 102 | |
| 103 | return t.Render() |
| 104 | } |
| 105 | |
| 106 | func truncateMiddle(maxWidth int, t string) string { |