# Merge pull request #1665 from entireio/feat/mirror-list-filter-sort

`fce2956`→[main](/content/gh/entireio/cli/commits/main/index.html)

## Changes

- cmd/entire/cli
  - Mrepo_mirror.go+175/-14
  - Mrepo_mirror_test.go+314/-7

```
1
2
3
4
5
6
7
1 unmodified line

9
10
11
12
13
14
15
2 unmodified lines

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
20
21
22
23
24
25
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
30
31
32
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
34
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
3 unmodified lines

176
177
178
44
179
180
181
182
298 unmodified lines

481
482
483
349
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
12 unmodified lines

517
518
519
373
520
521
522
523
524
525
526
527
10 unmodified lines

538
539
540
390
541
542
543
544
13 unmodified lines

558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579

package cli

import (
	"cmp"
	"context"
	"errors"
	"fmt"
	"net"
	"net/url"
	"regexp"
	"slices"
	"strings"
	"time"
	"github.com/entireio/cli/internal/coreapi"
)

const (
	colRepo     = "REPO"
	colCloneURL = "CLONE URL"
	colPrivate  = "PRIVATE"
	colAccess   = "ACCESS"
	colStatus   = "STATUS"
)

var mirrorColumns = []string{"REPO", "CLONE URL", "PRIVATE"}

func mirrorPrivate(m coreapi.Mirror) string {
	if m.IsPrivate.Or(false) {
		return "yes"
	}
	return "no"
}

func mirrorRow(m coreapi.Mirror) []string {
	repo := m.Owner + "/" + m.Repo
	cloneURL := mirrorCloneURL(m.ClusterHost, m.Owner, m.Repo)
	private := "no"
	if m.IsPrivate.Or(false) {
		private = "yes"
	}
	return []string{repo, cloneURL, mirrorPrivate(m)}
}

func parseSortColumn(spec string, columns []string) (col string, desc bool, err error) {
	spec = strings.TrimSpace(spec)
	desc = strings.HasPrefix(spec, "-")
	name := strings.TrimSpace(strings.TrimPrefix(spec, "-"))
	if name == "" {
		return columns[0], desc, nil
	}
	for _, h := range columns {
		if strings.EqualFold(h, name) {
			return h, desc, nil
		}
	}
	return "", false, fmt.Errorf("unknown sort column %q; valid columns: %s", name, strings.ToLower(strings.Join(columns, ", "))) 
}

func sortMirrors(mirrors []coreapi.Mirror, spec string) error {
	col, desc, err := parseSortColumn(spec, mirrorColumns)
	if err != nil {
		return err
	}
	key := func(m coreapi.Mirror) string {
		switch col {
		case colCloneURL:
			return strings.ToLower(mirrorCloneURL(m.ClusterHost, m.Owner, m.Repo))
		case colPrivate:
			return mirrorPrivate(m)
		default: // repo -> tiebreak alone
			return ""
		}
	}
	slices.SortStableFunc(mirrors, func(a, b coreapi.Mirror) int {
		c := cmp.Compare(key(a), key(b))
		if c == 0 {
			c = cmp.Compare(strings.ToLower(a.Owner+"/"+a.Repo), strings.ToLower(b.Owner+"/"+b.Repo))
		}
		if c == 0 {
			c = cmp.Compare(strings.ToLower(a.ClusterHost), strings.ToLower(b.ClusterHost))
		}
		if desc {
			return -c
		}
		return c
	})
	return nil
}

func newRepoMirrorListCmd() *cobra.Command {
	var cluster, provider, owner, repo string
	var sortSpec string
	var showAvailable bool
	cmd := &cobra.Command{
		Use:   "list",
		Short: "List mirrors you can see (or, with --show-available, repos you could mirror)",
		Args:  cobra.NoArgs,
		PreRunE: func(_ *cobra.Command, _ []string) error {
			cols := mirrorColumns
			if showAvailable {
				cols = availableMirrorColumns
			}
			_, _, err := parseSortColumn(sortSpec, cols)
			return err
		},
		RunE: func(cmd *cobra.Command, _ []string) error {
			if showAvailable {
				return runCoreList(cmd, "No repos available to mirror.", availableMirrorColumns, availableMirrorRow, func(ctx context.Context, c *coreapi.Client) ([]coreapi.AvailableMirror, error) {
					// Implementation here
				})
			}
			return runCoreList(cmd, "No mirrors found.", mirrorColumns, mirrorRow, func(ctx context.Context, c *coreapi.Client) ([]coreapi.Mirror, error) {
				// Implementation here
			})
		},
	}
	cmd.Flags().StringVar(&cluster, "cluster", "", "Filter by cluster public host")
	cmd.Flags().StringVar(&provider, "provider", "", "Filter by upstream provider (e.g. github)")
	cmd.Flags().StringVar(&owner, "owner", "", "Filter by upstream owner login")
	cmd.Flags().StringVar(&repo, "repo", "", "Filter by owner/repo substring, matching the REPO column (case-insensitive)")
	cmd.Flags().StringVar(&sortSpec, "sort", "", "Sort by column (header name; prefix '-' for descending). Default: repo name ascending")
	cmd.Flags().BoolVar(&showAvailable, "show-available", false, "Instead of existing mirrors, list GitHub repos you could onboard as mirrors (ignores --cluster/--provider)")
	return cmd
}
```
