github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/ssh-key/list/list.go (about)

     1  package list
     2  
     3  import (
     4  	"net/http"
     5  	"strconv"
     6  	"time"
     7  
     8  	"github.com/ungtb10d/cli/v2/internal/config"
     9  	"github.com/ungtb10d/cli/v2/internal/text"
    10  	"github.com/ungtb10d/cli/v2/pkg/cmdutil"
    11  	"github.com/ungtb10d/cli/v2/pkg/iostreams"
    12  	"github.com/ungtb10d/cli/v2/utils"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  type ListOptions struct {
    17  	IO         *iostreams.IOStreams
    18  	Config     func() (config.Config, error)
    19  	HTTPClient func() (*http.Client, error)
    20  }
    21  
    22  func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command {
    23  	opts := &ListOptions{
    24  		IO:         f.IOStreams,
    25  		Config:     f.Config,
    26  		HTTPClient: f.HttpClient,
    27  	}
    28  
    29  	cmd := &cobra.Command{
    30  		Use:     "list",
    31  		Short:   "Lists SSH keys in your GitHub account",
    32  		Aliases: []string{"ls"},
    33  		Args:    cobra.ExactArgs(0),
    34  		RunE: func(cmd *cobra.Command, args []string) error {
    35  			if runF != nil {
    36  				return runF(opts)
    37  			}
    38  			return listRun(opts)
    39  		},
    40  	}
    41  
    42  	return cmd
    43  }
    44  
    45  func listRun(opts *ListOptions) error {
    46  	apiClient, err := opts.HTTPClient()
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	cfg, err := opts.Config()
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	host, _ := cfg.DefaultHost()
    57  
    58  	sshKeys, err := userKeys(apiClient, host, "")
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	if len(sshKeys) == 0 {
    64  		return cmdutil.NewNoResultsError("no SSH keys present in the GitHub account")
    65  	}
    66  
    67  	//nolint:staticcheck // SA1019: utils.NewTablePrinter is deprecated: use internal/tableprinter
    68  	t := utils.NewTablePrinter(opts.IO)
    69  	cs := opts.IO.ColorScheme()
    70  	now := time.Now()
    71  
    72  	if t.IsTTY() {
    73  		t.AddField("TITLE", nil, nil)
    74  		t.AddField("ID", nil, nil)
    75  		t.AddField("KEY", nil, nil)
    76  		t.AddField("ADDED", nil, nil)
    77  		t.EndRow()
    78  	}
    79  
    80  	for _, sshKey := range sshKeys {
    81  		id := strconv.Itoa(sshKey.ID)
    82  		createdAt := sshKey.CreatedAt.Format(time.RFC3339)
    83  
    84  		if t.IsTTY() {
    85  			t.AddField(sshKey.Title, nil, nil)
    86  			t.AddField(id, nil, nil)
    87  			t.AddField(sshKey.Key, truncateMiddle, nil)
    88  			t.AddField(text.FuzzyAgoAbbr(now, sshKey.CreatedAt), nil, cs.Gray)
    89  		} else {
    90  			t.AddField(sshKey.Title, nil, nil)
    91  			t.AddField(sshKey.Key, nil, nil)
    92  			t.AddField(createdAt, nil, nil)
    93  			t.AddField(id, nil, nil)
    94  		}
    95  
    96  		t.EndRow()
    97  	}
    98  
    99  	return t.Render()
   100  }
   101  
   102  func truncateMiddle(maxWidth int, t string) string {
   103  	if len(t) <= maxWidth {
   104  		return t
   105  	}
   106  
   107  	ellipsis := "..."
   108  	if maxWidth < len(ellipsis)+2 {
   109  		return t[0:maxWidth]
   110  	}
   111  
   112  	halfWidth := (maxWidth - len(ellipsis)) / 2
   113  	remainder := (maxWidth - len(ellipsis)) % 2
   114  	return t[0:halfWidth+remainder] + ellipsis + t[len(t)-halfWidth:]
   115  }