repo mirror list: single source of truth for column names · Entire

repo mirror list: single source of truth for column names

424965f→main·

gtrrz-victor·1w ago·1 file·+28 added/-14 removed

The column header strings were hardcoded twice: uppercase in the header arrays and lowercase in the --sort key switches. Define them once as col* constants and have parseSortColumn return the canonical header it matched, so both arrays and both switches reference the constants directly.

Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com

Sessions

01KX0BEG51ES5KMV0Z2MN32DH2View transcript

[?
Refactor Repo Mirror List Sorting and FilteringClaude Code·Opus 4.8[1m]·3 steps](/content/gh/entireio/cli/session/89c0eaf0-fefd-4eda-ab50-0c9edb353537#timeline-01KX0BEG51ES5KMV0Z2MN32DH2/index.html)

Changes

1

17 unmodified lines

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2 unmodified lines

38
39
40
29
41
42
43
44
10 unmodified lines

55
56
57
46
47
48
49
50
58
59
60
61
62
63
64
65
66
67
54
68
69
56
70
71
72
73
60
74
75
76
77
12 unmodified lines

90
91
92
79
93
94
81
95
96
97
98
27 unmodified lines

126
127
128
115
129
130
117
131
132
133
134
39 unmodified lines

174
175
176
163
177
178
179
180

17 unmodified lines
    "github.com/entireio/cli/internal/coreapi"
// Column header names, the single source of truth for both the table headers
// (mirrorColumns/availableMirrorColumns) and the --sort key switches. parseSort-
// Column returns the canonical header it matched, so the sort switches compare
// against these constants directly.
const (
    colRepo     = "REPO"
    colCloneURL = "CLONE URL"
    colPrivate  = "PRIVATE"
    colAccess   = "ACCESS"
    colStatus   = "STATUS"
)

// mirrorColumns is the human table/field view of a mirror: the scannable
// owner/repo name, the clone URL you'd copy, and whether the upstream is
// private. Owner, provider, and cluster aren't columns of their own — they're
// only; owner/provider/cluster stay server-side filters, and the wire model's
// internal ids are dropped. The clone URL is synthesised from the mirror's
// coords (the form `git clone` accepts), since the list API doesn't return it.
var mirrorColumns = []string{"REPO", "CLONE URL", "PRIVATE"}
var mirrorColumns = []string{colRepo, colCloneURL, colPrivate}

// mirrorPrivate renders the PRIVATE column ("yes"/"no"), shared by the table
// row and the --sort private key so both agree on the cell value.
return []string{repo, cloneURL, mirrorPrivate(m)}

// parseSortColumn resolves a --sort spec to a lowercased column name and
// direction against columns. It trims first, then reads the '-' prefix, so
// leading/trailing whitespace is handled identically on every path (the
// direction and the column name never disagree). An empty spec selects the
// first column. An unknown name errors naming the valid columns.
// parseSortColumn resolves a --sort spec to the canonical column header it
// names (one of the columns entries) and a direction. It trims first, then
// reads the '-' prefix, so leading/trailing whitespace is handled identically
// on every path (the direction and the column name never disagree). An empty
// spec selects the first column. An unknown name errors naming the valid
// columns. Returning the matched header lets callers switch on the col*
// constants directly.
func parseSortColumn(spec string, columns []string) (col string, desc bool, err error) {
    spec = strings.TrimSpace(spec)
    desc = strings.HasPrefix(spec, "-")
    name := strings.ToLower(strings.TrimSpace(strings.TrimPrefix(spec, "-")))
    if name == "" {
        return strings.ToLower(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, ", ")))  
}

key := func(m coreapi.Mirror) string {
    switch col {
    case "clone url":
    case colCloneURL:
        return strings.ToLower(mirrorCloneURL(m.ClusterHost, m.Owner, m.Repo))
    case "private":
    case colPrivate:
        return mirrorPrivate(m)
    default: // repo -> tiebreak alone
        return ""
    }
}

key := func(m coreapi.AvailableMirror) string {
    switch col {
    case "access":
    case colAccess:
        return strings.ToLower(string(m.Access))
    case "status":
    case colStatus:
        return strings.ToLower(string(m.Status))
    default: // repo -> tiebreak alone
        return ""
    }
}

// clone URL), or "owner-only" (a personal repo of another user; only its
// owner may mirror it). No clone URL column: an un-onboarded repo doesn't
// have one yet.
var availableMirrorColumns = []string{"REPO", "ACCESS", "STATUS"}
var availableMirrorColumns = []string{colRepo, colAccess, colStatus}

func availableMirrorRow(m coreapi.AvailableMirror) []string {
    return []string{m.Owner + "/" + m.Repo, string(m.Access), string(m.Status)}
}

Mcmd/entire/cli/repo_mirror.go+28/-14